程序先读入三个浮点数作为三角形的三边长。然后计算出它的半周长。再利用海伦公式计算出三角形的面积,最后输出这个面积值。
double a,b,c,p,s
cin>>a>>b>>c
p=(a+b+c)/2
s=sqrt(p*(p-a)*(p-b)*(p-c))
cout<<s<<endl
using Systemusing System.Collections.Generic
using System.Linq
using System.Text
namespace AreaDemo
{
class Program
{
const double PI = 3.1415926
static void Main(string[] args)
{
//求三角形面积例子,另外两个求法类同
Console.WriteLine("请输入三角形的相应数据:")
Console.Write("底边长:")
string bases = Console.ReadLine()
Console.Write("高:")
string height = Console.ReadLine()
Area method = new Area()
double area = method.GetTriangleArea(Convert.ToDouble(bases), Convert.ToDouble(height))
Console.WriteLine("三角形面积为:" + area.ToString())
Console.Read()
}
class Area
{
/// <summary>
/// 求三角形面积
/// </summary>
/// <param name="bases">底边</param>
/// <param name="height">高</param>
/// <returns>面积</returns>
public double GetTriangleArea(double bases, double height)
{
return bases * height / 2
}
/// <summary>
/// 求正方形面积
/// </summary>
/// <param name="side">边长</param>
/// <returns>面积</returns>
public double GetSquareArea(double side)
{
return side * side
}
/// <summary>
/// 求圆面积
/// </summary>
/// <param name="radius"></param>
/// <returns></returns>
public double GetCircleArea(double radius)
{
return PI * radius * radius
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)