?? 5.3.cpp
字號:
#include <iostream.h>
#include "math.h"
class Cpoint
{
private:
float x,y;
public:
friend class Triangle;
float GetX(){return x;}
float GetY(){return y;}
Cpoint(float a,float b)
{
x=a;
y=b;
}
~Cpoint(){};
};
class Triangle
{
private:
Cpoint X,Y,Z;
double l1,l2,l3;
public:
Triangle(Cpoint X1,Cpoint Y1,Cpoint Z1);
~Triangle(){};
friend double length(Triangle &);
friend double area(Triangle &);
};
int top=0;
Triangle::Triangle(Cpoint X1,Cpoint Y1,Cpoint Z1):X(X1),Y(Y1),Z(Z1)
{
l1=sqrt((X.x-Y.x)*(X.x-Y.x)+(X.y-Y.y)*(X.y-Y.y));
l2=sqrt((Z.x-Y.x)*(Z.x-Y.x)+(Z.y-Y.y)*(Z.y-Y.y));
l3=sqrt((X.x-Z.x)*(X.x-Z.x)+(X.y-Z.y)*(X.y-Z.y));
cout<<"A點到B點距離l1為:"<<l1<<endl;
cout<<"C點到B點距離l2為:"<<l2<<endl;
cout<<"A點到C點距離l3為:"<<l3<<endl;
if((l1+l2)<=l3||(l1+l3)<=l2||(l3+l2)<=l1)
{
top=1;
cout<<"此三點不能構(gòu)成三角形"<<endl;
}
}
double length(Triangle &s)
{
return (s.l1+s.l2+s.l3);
}
double area(Triangle &f)
{ double d,s;
d=(f.l1+f.l2+f.l3)/2;
s=sqrt(d*(d-f.l1)*(d-f.l2)*(d-f.l3));
return s;
}
void main()
{ float c,d;
cout<<"輸入一個點A"<<endl;
cin>>c>>d;
Cpoint A(c,d);
cout<<"輸入一個點B"<<endl;
cin>>c>>d;
Cpoint B(c,d);
cout<<"輸入一個點C"<<endl;
cin>>c>>d;
Cpoint C(c,d);
Triangle D(A,B,C);
if(top==0)
{ cout<<"三角形面積為:"<<area(D)<<endl;
cout<<"三角形周長為:"<<length(D)<<endl;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -