早教吧 育儿知识 作业答案 考试题库 百科 知识分享

c++求三角形面积定义一个三角形类triangle,数据成员包含三角形的三条边,成员函数有:构造函数;判断是否能构成三角形的函数;求三角形面积的函数.编写主函数:输入三角形的三条边,判断能

题目详情
c++ 求三角形面积
定义一个三角形类triangle,数据成员包含三角形的三条边,成员函数有:构造函数;判断是否能构成三角形的函数;求三角形面积的函数.
编写主函数:输入三角形的三条边,判断能否构成三角形,如果能构成,则打印出面积(保留小数点后4位小数);否则打印出“no triangle”.
输入格式:测试输入包含若干测试用例,每个测试用例占一行.当读入0 0 0时输入结束,相应的结果不要输出.
Sample Input
3 4 5
6 3 5
20 13 0
0 0 0
Sample Output:
6.0000
7.4833
no triangle
Sample Input:
1 2 3
5 3 6
0 0 0
Sample Input
no triangle
7.4833
#include
#include
#include
using namespace std;
class triangle{
public:
\x05 float a[3];
\x05 int judge();
\x05 float s();
\x05 triangle(){}
private:
int i;
\x05 float t,p;
}tr;
int triangle ::judge(){
\x05 for(i=0;ia[i+1]){
\x05\x05\x05 t=a[i];
\x05\x05\x05 a[i]=a[i+1];
\x05\x05\x05 a[i+1]=t;
\x05\x05 }
\x05 }
\x05 if(a[0]+a[1]>a[2])
\x05\x05 return 1;
\x05 else return 0;
}
float triangle ::s(){
\x05 p=(a[0]+a[1]+a[2])/2;
\x05 return sqrt(p*(p-a[0])*(p-a[1])*(p-a[2]));
}
int main(){
\x05 while(1){
\x05\x05 cin>>tr.a[0]>>tr.a[1]>>tr.a[2];
\x05\x05 if(tr.a[0]==0&&tr.a[1]==0&&tr.a[2]==0)
\x05\x05\x05 return 0;
\x05\x05 if(tr.a[0]!=0&&tr.a[1]!=0&&tr.a[2]!=0){
\x05\x05\x05 if(tr.judge()==1)
\x05\x05\x05\x05 cout
▼优质解答
答案和解析
#include
#include
using namespace std;
int main()
{
double a,b,c,m,s;
coutb>>c;
m=(a+b+c)/2;
if((a+b>c)||(a+c>b)||(b+c>a))
{
s=sqrt(m*(m-a)*(m-b)*(m-c));
cout