从键盘输入三个整数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; }
想要源代码的请点击。