辛苦编写的关于三角形面积的一个程序
#include<iostream.h>#include <math.h>
class tria
{
public:
void setsides(float a,float b,float c);
void print();
private:
float x,y,z;
float area;
};
void main()
{
tria tr1;
tr1.setsides(3,4,5);
tr1.print();
}
void tria::setsides(float a,float b,float c)
{
if (a+b>c&&a+c>b&&b+c>a)
{
x=a;y=b;z=c;
float t=(a+b+c)/2;
area=sqrt(t*(t-a)*(t-b)*(t-c));
}
else
x=y=z=area=0;
}
void tria::print()
{
cout<<"三角形的三条边长分别是:"<<endl;
cout<<x<<"\t"<<y<<"\t"<<z<<endl;
cout<<"三角形面积为:"<<endl;
cout<<area<<endl;
}