输入三边判断是否能组成三角形
从键盘输入三个整数a、b、c,(1<=a、b、c<=100)判断是否构成三角形,若能构成三角形,指出构成的是等边三角形?等腰三角形?不等边三角形?判断能否组成三角形的条件为:是否三边都满足两边之和大于第三边。#include <iostream>using namespace std;class triangle{private:...
·
从键盘输入三个整数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;
}
想要源代码的请点击。
转载于:https://blog.51cto.com/idiotxl1020/1290434

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)