三角形能否构成,及其类型判断(C++)

#C++
首先跟大家来最简单的,给你三条边 让你判断是否能构成三角形,根据三角形的定义,我们知道能否构成三角形的条件是任意两边之和大于第三边。
故代码如下:

#include<iostream>
using namespace std;
int main(){
double a,b,c;
cout<<"Please enter the length of A, B and C:"; 
cin>>a>>b>>c;
if(a+b>c&&a+c>b&&b+c>a)
cout<<"this is a triangle.";
else cout<<"this is not a triangle。";
return 0;
}

运行示例结果如下:
在这里插入图片描述

三角形一般又分为锐角三角形,直角三角形,钝角三角形。
那我们如何来判断呢?
这里在我们知道边的情况下,我根据勾股定理来进行判断
假设c为最长边
若a^2 + b^2 =C^2,则它为直角三角形;
若a^2 + b^2 >C^2,则它为锐角三角形;
若a^2 + b^2 <C^2,则它为钝角三角形。
上述过程我并没有说明C是最大边,可以添加提示,或者自己随意写让其判断;
代码如下:

if(a>c){int t=0;t=a;c=a;a=c;}
else if(b>c){int t=b;b=c;c=t;};

让C为最大边之后,我们上面的代码是不是可以修改一下判断是否为三角形的条件了,只要a+b>c就行了。接下来我们就进行判断三角形类型了,根据上面勾股定理判断;
代码如下:

#include<iostream>
using namespace std;
int main(){
	int a,b,c;
	cout<<"Please enter the length of A, B and C:";
	cin>>a>>b>>c;
	if(a>c){int t=0;t=a;c=a;a=c;} 
	else if(b>c){int t=b;b=c;c=t;};
	if(a+b>c)
	{if(a*a+b*b>c*c)cout<<"This is an acute triangle.";//锐角三角形 
	else if(a*a+b*b==c*c)cout<<"This is a right triangle.";//直角三角形 
        	else cout<<"This is an obtuse triangle.";//钝角三角形 
	}
	else cout<<"this is not a trangle";
	
	return 0;
}

运行示例结果如下:
在这里插入图片描述

那我们又知道有特殊的三角形:等腰三角形,等边三角形,等腰直角三角形。
已知三边长,并且c为最大边,我们可通过如下进行判断:
等腰三角形:a=b
等边三角形:a=b=c
等腰直角三角形:a=b&&a^2 +b^2 =c^2 //把它放进判断直角三角形条件里面
在这里,我们先得把这些特殊的三角形搞定,若他们不是特殊的三角形,我们则在判断它是锐角,直角,钝角三角形。
代码如下:

#include<iostream>
using namespace std;
int main(){
	double a,b,c;
	cout<<"Please enter the length of A, B and C:";
	cin>>a>>b>>c;
	if(a>c){double t=0;t=a;c=a;a=c;} 
	else if(b>c){double t=b;b=c;c=t;};
	if(a+b>c)
	{if(a==b==c)cout<<"This is an equilateral triangle.";//等边三角形
	else  if(a==b)cout<<"This is an isosceles triangle.";//等腰三角形
	 else if(a*a+b*b==c*c&&a==b)cout<<"This is an isosceles right triangle.";//等腰直角三角形 
	  else
	if(a*a+b*b>c*c)cout<<"This is an acute triangle.";//锐角三角形 
	else if(a*a+b*b==c*c)cout<<"This is a right triangle.";//直角三角形 
        	else cout<<"This is an obtuse triangle.";//钝角三角形 
	}
	else cout<<"this is not a trangle";
	
	return 0;
}

运行示例结果如下:
在这里插入图片描述
今天就跟大家分享到这里,下次有机会和大家分享其它的啦,不喜勿喷。有好的建议可以私信给我哦。谢谢!小编也只是刚学不久的。

  • 10
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值