10_86.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:1k
- #include<iostream.h>
- #include<math.h>
- #include<iomanip.h>
- class Triangle
- {
- int x,y,z;
- double area;
- public:
- Triangle();
- double getarea()
- {
- return area;
- }
- void dispArea()
- {
- cout<<setiosflags(ios::fixed)<<setprecision(2)<<area<<endl;
- }
- friend double operator+(Triangle t1,Triangle t2)
- {
- return t1.area+t2.area;
- }
- friend double operator+(double a,Triangle t)
- {
- return a+t.area;
- }
- friend bool operator>(Triangle t1,Triangle t2)
- {
- return t1.area>t2.area ?true:false;
- }
- };
- Triangle::Triangle()
- {
- int i,j,k;
- cout<<"请输入三角形的三边长:";
- cin>>i>>j>>k;
- if(i+j>k && i+k>j && j+k>i)
- {
- double s;
- x=i; y=j; z=k;
- s=(x+y+z)/2.0;
- area=sqrt(s*(s-x)*(s-y)*(s-z));
- }
- else
- {
- cout<<"错误!输入的三边长度不能构成三角形!"<<endl;
- area=x=y=z=0;
- }
- }
- void main()
- {
- Triangle t1,t2,t3;
- cout<<"三角形1面积:";t1.dispArea();
- cout<<"三角形2面积:";t2.dispArea();
- cout<<"三角形3面积:";t3.dispArea();
- if(t1>t2)
- cout<<"三角形1面积比三角形2大:"
- <<t1.getarea()-t2.getarea()<<endl;
- else
- cout<<"三角形2面积比三角形1大:"
- <<t2.getarea()-t1.getarea()<<endl;
- cout<<"总面积:"<<t1+t2+t3<<endl;
- }