C# 图形面积计算

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_34801639/article/details/51745430
   class Program
    {
        static void Main(string[] args)
        {
           Tuxing i;
            double a, b, c;
            while(true)
            {
                Console.WriteLine("********************************请选择*****************************************");
                Console.WriteLine("                         1.计算三角形面积");
                Console.WriteLine("                         2.计算圆的面积");
                Console.WriteLine("                         3.计算矩形形面积");
                Console.WriteLine("                         Q    退出");
                Console.WriteLine("*******************************************************************************");
                char ch = Convert.ToChar(Console.ReadLine());
                switch(ch)
                {
                    case '1': Sjx x = new Sjx(); i = x; Console.WriteLine("请输入第一条边:"); a = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入第二条边:"); b = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入第三条边:"); c = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("三角形的面积为:"); x.input(a, b, c); Console.WriteLine(i.getarea()); break;
                    case '2': Yuan t = new Yuan(); i = t; Console.WriteLine("请输入圆的半径:"); a = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("圆的面积为:"); t.input(a); Console.WriteLine( t.getarea()); break;
                    case '3': Jxing y = new Jxing(); i = y; Console.WriteLine("请输入长:"); a = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入宽:"); b = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("矩形的面积为:"); y.input(a, b); Console.WriteLine( y.getarea()); break;
                    case 'Q': Jxing n = new Jxing(); n.guanbi(); break;
                }            
               
            }
        }

    }






 abstract class Tuxing
    {
        protected double a;
        protected double b;
        protected double c;
        public virtual double getarea()
        { return 0; }
       
    }
    class Sjx : Tuxing
    {
        public void input(double a, double b, double c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
        }
        public override double getarea()
        {
            if (a + b > c && a + c > b && a - b < c && a - c < b)
            {
                double q = (a + b + c) / 2;
                return (double)Math.Sqrt(q * (q - a) * (q - b) * (q - c));
            }
            else
                return 0;
        }
    }
    class Yuan : Tuxing
    {
        public void input(double a)
        {
            this.a = a;
        }
        public override double getarea()
        {
            return (double)Math.PI * a * a;
        }
    }
    class Jxing : Tuxing
    {
        public void input(double a, double b)
        {
            this.a = a;
            this.b = b;
        }
        public override double getarea()
        {
            return a * b;
        }
        public void guanbi()
        {
            Environment.Exit(0);
        }
    }

展开阅读全文

没有更多推荐了,返回首页