65,180
社区成员




#include<iostream>
#include<cmath>
using namespace std;
class myPoint {
public:
friend class Triangle;
myPoint(double x0=0.0,double y0=0.0):x(x0),y(y0) {}
myPoint(myPoint &np):x(np.x),y(np.y) {}
double GetX() { return x;}
double GetY() {return y;}
void SetX(double x0) {x=x0;}
void SetY(double y0) {x=y0;}
void SetPoint(double x0,double y0) {x=x0;y=y0;}
void SetPoint(myPoint &np) { x=np.x; y=np.y;}
double GetLength(myPoint p) {
return sqrt((x-p.x)*(x-p.x) +(y-p.y)*(y-p.y));
}
void Printit() { cout<<" ("<<x<<","<<y<<") ";}
private:
double x ,y;
};
class Triangle{
private:
double X,Y,a,b,c;
myPoint p[3];
public:
friend class myPoint;
Triangle(){
for(int i=0;i<3;i++)
{
cout<<"请输入点P("<<i+1<<")的坐标:"<<endl;
cout<<endl;
cout<<"X坐标:";
cin>>X;
cout<<endl;
cout<<"Y坐标:";
cin>>Y;
p[i].SetPoint(X,Y);
cout<<"点P("<<i+1<<")的坐标为:";
p[i].Printit();
cout<<endl;
}
}
double TLength(){
a=p[1].GetLength(p[2]);
b=p[1].GetLength(p[3]);
c=p[2].GetLength(p[3]);
return a+b+c;
}
double Square(){
return sqrt(TLength()/2*(TLength()/2-a)*(TLength()/2-b)*(TLength()/2-c));
}
};
int main()
{
Triangle S;
cout<<"三角形周长:"<<S.TLength()<<endl;;
cout<<"三角形面积:"<<S.Square();
return 0;
}
myPoint p[3];
a=p[1].GetLength(p[2]);
b=p[1].GetLength(p[3]);
c=p[2].GetLength(p[3]);