用C#编写三角形面积计算程序(附源码)
生活随笔
收集整理的這篇文章主要介紹了
用C#编写三角形面积计算程序(附源码)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.程序要求
求三角形的面積,要求用“方法的參數數組”、“接口”、“構造函數”、“類繼承”、至少四種方法實現三角形的面積的方法。
二.代碼部分
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和接口)
?
總結
以上是生活随笔為你收集整理的用C#编写三角形面积计算程序(附源码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3.5怎么打开_Python
- 下一篇: plc通讯的握手信号_基于Modbus协