输入三角形三条边 a , b , c 的值,根据其数据,判断能否构成三角形。若能构成三角形,还要显示三角形的性质:等边三角形、等腰三角形、直角三角形、任意3三角形。编写判定三角形的函数
时间: 2024-06-19 09:03:25 浏览: 38
判断一个三角形是否能够构成,需要满足两个条件:
1. 任意两边之和大于第三边
2. 任意两边之差小于第三边
根据以上条件,可以编写如下的判断三角形函数:
```c++
#include <iostream>
using namespace std;
enum TriangleType {Equilateral, Isosceles, Right, Arbitrary};
TriangleType GetTypeOfTriangle(double a, double b, double c)
{
if (a + b <= c || a + c <= b || b + c <= a) {
return Arbitrary;
}
if (a == b && b == c) {
return Equilateral;
}
if (a == b || a == c || b == c) {
return Isosceles;
}
if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) {
return Right;
}
return Arbitrary;
}
int main()
{
double a, b, c;
cout << "请输入三角形的三条边长:" << endl;
cin >> a >> b >> c;
TriangleType type = GetTypeOfTriangle(a, b, c);
switch (type) {
case Equilateral:
cout << "这是一个等边三角形。" << endl;
break;
case Isosceles:
cout << "这是一个等腰三角形。" << endl;
break;
case Right:
cout << "这是一个直角三角形。" << endl;
break;
case Arbitrary:
cout << "这是一个任意三角形。" << endl;
break;
default:
break;
}
return 0;
}
```
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)