一.程序要求
求三角形的面积,要求用“方法的参数数组”、“接口”、“构造函数”、“类继承”、至少四种方法实现三角形的面积的方法。
二.代码部分
using System;
using System.Xml.Linq;
interface IPartOne
{
void GetArea(double[] Area);
}
public class Sharp
{
public double side1;
public double side2;
public double side3;
public double bottom;
public double top;
public double angle;
public double r;
public double p;
public double HalfPerimeter()
{
return (side1 + side2 + side3) / 2;
}
}
class Method1 : Sharp, IPartOne
{
public void GetArea(double[] Area)
{
Sharp sharp1 = new Sharp();
door:
Console.WriteLine("请输入三边:");
sharp1.side1 = Double.Parse(Console.ReadLine());
sharp1.side2 = Double.Parse(Console.ReadLine());
sharp1.side3 = Double.Parse(Console.ReadLine());
if (sharp1.side1 + sharp1.side2 > sharp1.side3 && sharp1.side1 + sharp1.side3 > sharp1.side2 && sharp1.side2 + sharp1.side3 > sharp1.side1)
{
double p = sharp1.HalfPerimeter();
Area[0] = Math.Sqrt((p - sharp1.side1) * (p - sharp1.side2) * (p - sharp1.side3) * p);
}
else
{
Console.WriteLine("不存在该三角形 请重新输入");
goto door;
}
}
}
class Method2 : Sharp, IPartOne
{
public void GetArea(double[] Area)
{
Console.WriteLine("请输入三角形的底和高:");
Sharp sharp2 = new Sharp();
sharp2.bottom = Double.Parse(Console.ReadLine());
sharp2.top = Double.Parse(Console.ReadLine());
Area[0] = (sharp2.top * sharp2.bottom) / 2;
}
}
class Method3 : Sharp, IPartOne
{
public void GetArea(double[] Area)
{
Console.WriteLine("请输入三角形的两边和夹角:");
Sharp sharp3 = new Sharp();
sharp3.side1 = Double.Parse(Console.ReadLine());
sharp3.side2 = Double.Parse(Console.ReadLine());
sharp3.angle = Double.Parse(Console.ReadLine());
Area[0] = (sharp3.side1 * sharp3.side2 * Math.Sin((Math.PI / 180.0) * sharp3.angle)) / 2;
}
}
class Method4 : Sharp, IPartOne
{
public void GetArea(double[] Area)
{
Console.WriteLine("请输入三角形周长和内切圆半径:");
Sharp sharp4 = new Sharp();
sharp4.p = Double.Parse(Console.ReadLine());
sharp4.r = Double.Parse(Console.ReadLine());
Area[0] = (sharp4.r * sharp4.p) / 2;
}
}
class Init
{
public bool InitFunc()
{
double[] Area = new double[1];
Console.WriteLine("-----------------------------------------------------");
Console.WriteLine("1.已知三边");
Console.WriteLine("2.已知底和高");
Console.WriteLine("3.已知两边以及夹角(角度制)");
Console.WriteLine("4.已知三角形周长以及内切圆半径");
Console.WriteLine("0.Exit");
string s = Console.ReadLine();
int a = Convert.ToInt32(s);
switch (a)
{
case 1:
Method1 method1 = new Method1();
method1.GetArea(Area);
break;
case 2:
Method2 method2 = new Method2();
method2.GetArea(Area);
break;
case 3:
Method3 method3 = new Method3();
method3.GetArea(Area);
break;
case 4:
Method4 method4 = new Method4();
method4.GetArea(Area);
break;
case 0:
Console.WriteLine("程序已退出");
return false;
default:
Console.WriteLine("输入错误 请重新输入");
return true;
}
Console.WriteLine("面积是:{0}", Math.Round(Area[0], 3));
return true;
}
}
class Progremme
{
static void Main()
{
Init init = new Init();
bool flag = true;
while (flag)
{
flag = init.InitFunc();
}
}
}
三.程序说明
接口部分:IPartOne
类:形状Sharp、四种计算三角形的方法Method1-4(分别继承基类Sharp和接口)