用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和接口)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ukihio

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值