一道简单的c++编程题?

2024-12-01 01:02:29
推荐回答(2个)
回答(1):

知道三条边求面积,高中时候的数学应该学过

回答(2):

不知道你是要三维的还是二维的

一点提示:
三角形的面积等于三角形任意两边向量叉乘所得成绩的一半 有关叉乘你应该自己查资料了吧

#include
#include

using namespace std;

class Cpoint
{
public:
Cpoint()
{
x=y=0;
}
Cpoint(double x0, double y0):x(x0), y(y0)
{
}
double getx();
double gety();
private:
double x,
y;
};
double Cpoint::getx()
{
return x;
}
double Cpoint::gety()
{
return y;
}
class tria
{
public:
tria():a(),b(),c(),S(0)
{}
tria(double x0,double y0,double x1,double y1,
double x2,double y2):a(x0,y0),b(x1,y1),c(x2,y2)
{
S=fabs(((x1-x0)*(y2-y0)-(x2-x0)*(y1-y0))/2);
}
double getS();
void seta(double,double);
void setb(double,double);
void setc(double,double);
private:
Cpoint a,b,c;
double S;
};
void tria::seta(double x0,double y0)
{
a=Cpoint(x0,y0);
}
void tria::setb(double x0,double y0)
{
b=Cpoint(x0,y0);
}
void tria::setc(double x0,double y0)
{
c=Cpoint(x0,y0);
}
double tria::getS()
{
if(S==0)
{
S=fabs(((b.getx()-a.getx())*(c.gety()-a.gety())-
(c.getx()-a.getx())*(b.gety()-a.gety()))/2);
}
return S;
}
int main()
{
tria triangle(0,0,0,3,4,0);
cout< tria trgle;
trgle.seta(0,0);
trgle.setb(0,3);
trgle.setc(4,0);
cout< return 0;
}