從鍵盤輸入三個整數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