用C#编写一个程序如何判断三角形形状

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 10:59:51
用C#编写一个程序如何判断三角形形状

用C#编写一个程序如何判断三角形形状
用C#编写一个程序如何判断三角形形状

用C#编写一个程序如何判断三角形形状
C#程序:
using System;
class Program
{
static void Main()
{
int a,b,c; //三角形的三边长
//接收输入
Console.WriteLine("请输入三角形的三边长");
Console.Write("a :");
a = int.Parse(Console.ReadLine());
Console.Write("b :");
b = int.Parse(Console.ReadLine());
Console.Write("c :");
c = int.Parse(Console.ReadLine());
//对a、b、c的值排序
if (a < b)
swap(ref a,ref b);
if (b < c)
swap(ref b,ref c);
if (a < b)
swap(ref a,ref b);
//判断是否构造三角形
if (a >= b + c)
{
Console.WriteLine("{0}、{1}、{2}不能构成三角形!",a,b,c);
return;
}
//判断是否是等边三角形
if (a == b && a == c)
{
Console.WriteLine("{0}、{1}、{2}构成等边三角形!",a,b,c);
return;
}
//判断是否是等腰直角三角形
if (b == c && a * a == b * b + c * c)
{
Console.WriteLine("{0}、{1}、{2}构成等腰直角三角形!",a,b,c);
return;
}
//判断是否是等腰三角形
if (a == b || a == c || b == c)
{
Console.WriteLine("{0}、{1}、{2}构成等腰三角形!",a,b,c);
}
//判断是否是直角三角形
if (a * a == b * b + c * c)
{
Console.WriteLine("{0}、{1}、{2}构成直角三角形!",a,b,c);
}
else if (a * a < b * b + c * c)
{
Console.WriteLine("{0}、{1}、{2}构成锐角三角形!",a,b,c);
}
else
{
Console.WriteLine("{0}、{1}、{2}构成钝角三角形!",a,b,c);
}
}
static void swap(ref int n1,ref int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
}
运行测试:
a:7
b:8
c:8
8、7、7构成等腰三角形
8、7、7构成锐角三角形