题目描述
定义一个三角形类CTriangle,属性包括三条边和三角形类型,其中用字符串保存三角形类型。三角形类型如下:
等腰三角形: isosceles triange
直角三角形:right triangle
等腰直角三角形:isosceles right triangle
等边三角形:equilateral triangle
普通三角形:general triangle
不能构成三角形:no triangle
其中判断直角三角形的条件是:三角形中两边长度的平方和等于另一条边的长度
类行为包括构造、析构、计算面积(可以根据实际需要添加)。其中构造函数将设置三条边长度,并且判断三条边能否构成三角形、并设置三角形类型。析构函数将三条边长度清零,并将三角形类型设置为none
三角形面积计算如下
输入
测试数据的组数
第一组边1 第一组边2 第一组边3
第二组边1 第二组边2 第三组边3
……
输出
第一个三角形的类型,面积
第二个三角形的类型,面积
……
如果构不成三角形,则不用输出样例
面积精确到小数点后一位
样例
输入样例
3
3.0 2.0 6.0
3.0 4.0 5.0
1.0 1.0 1.414
输出样例
no triangle
right triangle, 6.0
isosceles right triangle, 0.5
参考代码
#include<iostream>
#include<string>
#include<math.h>
#include<iomanip>
using namespace std;
class CTriangle
{
private:
float x1, x2, x3;
string a = { "isosceles triange" };//等腰三角形
string b = { "right triangle" };//直角三角形
string c = { "isosceles right triangle" };//等腰直角三角形
string d = { "equilateral triangle" };//等边三角形
string e = { "general triangle" };//普通三角形
string f = { "no triangle" };//不能构成三角形
public:
CTriangle(float y1, float y2, float y3) :x1(y1), x2(y2), x3(y3) {};
~CTriangle() {
x1 = 0, x2 = 0, x3 = 0;
a = { "none" };
b = { "none" };
c = { "none" };
d = { "none" };
e = { "none" };
f = { "none" };
}
int goucheng() {
if (x1 + x2 > x3 && x2 + x3 > x1 && x1 + x3 > x2) { return 1;}//先判断能否构成三角形
else {
cout << f << endl;
return 0; }
}
float p() {
return((x1 + x2 + x3) / 2);//计算半周长
}
void S() {
float s = sqrt(p() * (p() - x1) * (p() - x2) * (p() - x3));
cout<<fixed<<setprecision(1) << s << endl;
}
void leixing() {
int flag1 = 0, flag2 = 0,flag3=0;//1记录是否等腰,2记录是否是直角
if ((x1 == x2 )|| (x1 == x3) ||( x1 == x3)) {
if (x1 == x2 && x1 == x3) {
cout << d << ", " ;
flag3 = 1;
}
else { flag1 = 1; }//记录等腰
}
if ((x1 * x1 + x2 * x2 == x3 * x3) || x2 * x2 + x3 * x3 == x1 * x1 || x1 * x1 + x3 * x3 == x2 * x2) {
flag2 = 1;
}
if (flag1 == 1 && flag2 == 1&&flag3==0) { cout << c << ", "; }
else if (flag1 == 1 && flag2 == 0&&flag3 == 0) { cout << a << ", "; }
else if (flag1 == 0 && flag2 == 1&& flag3 == 0) { cout << b << ", "; }
else if (flag1 == 0 && flag2 == 0 && flag3 == 0) {cout << e << ", "; }
}
};
int main()
{
int t;
cin >> t;
while (t--) {
float xx1, xx2, xx3;
cin >> xx1 >> xx2 >> xx3;
CTriangle san(xx1, xx2, xx3);
if (san.goucheng() == 0) { continue; }
else {
san.leixing();
san.S();
}
}
return 0;
}