从键盘输入三个整数a、b、c,(1<=a、b、c<=100)
判断是否构成三角形,若能构成三角形,指出构成的是
等边三角形?等腰三角形?不等边三角形?
判断能否组成三角形的条件为:是否三边都满足两边之和大于第三边。
#include <iostream>
using namespace std;
class triangle{
private:
float edge_a;
float edge_b;
float edge_c;
bool compare();
public:
triangle(){
edge_a = 0.0;
edge_b = 0.0;
edge_c = 0.0;
}
triangle(float a, float b, float c){
edge_a = a;
edge_b = b;
edge_c = c;
}
int isTriangle();
int whatTriangle();
};
bool triangle::compare(){
if (edge_a+edge_b>edge_c && edge_b+edge_c>edge_a && edge_c+edge_a>edge_b)
return true;
else
return false;
}
int triangle::isTriangle(){
if (this->compare()){
return this->whatTriangle();
}
else
return 0;
}
int triangle::whatTriangle(){
if (edge_a == edge_b && edge_b == edge_c && edge_c == edge_a)
return 1;
else if (edge_a == edge_b || edge_b == edge_c || edge_c == edge_a)
return 2;
else
return 3;
}
int main(){
int a,b,c;
int choice=1;
while (choice)
{
system("cls");
cout<<"请输入三角形的三边边长。"<<endl;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<"c=";
cin>>c;
triangle tri(a,b,c);
int result = tri.isTriangle();
switch (result)
{
case 1:
cout<<"可以组成等边三角形。";
break;
case 2:
cout<<"可以组成等腰三角形。";
break;
case 3:
cout<<"可以组成不等边三角形。";
break;
default:
cout<<"无法组成三角形。";
}
cout<<endl;
cout<<"要继续请输入1,要退出请输出0."<<endl;
cin>>choice;
}
cout<<"退出程序."<<endl;
return 0;
}
想要源代码的请点击。
本文出自 “淡定的dreamer” 博客,请务必保留此出处http://idiotxl1020.blog.51cto.com/6419277/1290434
该博客介绍如何根据用户输入的三个整数a、b、c来判断是否可以构成三角形。条件是任意两边之和必须大于第三边。如果可以构成三角形,还会进一步区分是等边、等腰还是不等边三角形。完整源代码可供参考。
3375

被折叠的 条评论
为什么被折叠?



