每日博客

C#

编写一个控制台应用程序,输入正方形边长或者半径,计算其周长和面积并输出。

(1) 编写两个接口,接口 IShape 包含三个方法:initialize, getPerimeter, getArea。分

别进行初始化、获取边长和面积,其返回值均为 decimal。接口 IDisplayresult 显示计算结果。

(2) 编写两个类,Square(正方形)和 Circle(圆形),实现 IShape 和 IDisplayresult

接口。

using System;

namespace test

{

public interface IShape

    {

        void initialize();

        decimal getPerimeter();

        decimal getArea();

    }

    public interface IDisplayresult

    {

        void show();

    }

    public class Square : IShape, IDisplayresult  

    {

        public decimal sidelength;         

        public void initialize()

        {

 

            Console.WriteLine("请输入正方形边长:");

            do

            {

                sidelength = decimal.Parse(Console.ReadLine());

                if (sidelength <= 0)

                    Console.WriteLine("输入数据错误,请重新输入:");

            }

            while (sidelength <= 0);

        }

        public decimal getPerimeter()   

        {

            return 4 * sidelength;

        }

        public decimal getArea()      

        {

            return sidelength * sidelength;

        }

        public void show()            

        {

            Console.WriteLine("正方形周长:{0}", getPerimeter());

            Console.WriteLine("正方形面积:{0}", getArea());

        }

    }                              

    public class Circle : IShape, IDisplayresult  

    {

        decimal radius; const decimal pai = 3.14M;

        public void initialize()

        {

            Console.WriteLine("请输入半径:");

            do

            {

                radius = decimal.Parse(Console.ReadLine());

                if (radius <= 0)

                    Console.WriteLine("输入数据错误,请重新输入:");

            }

            while (radius <= 0);

        }

        public decimal getPerimeter()   

        {

 

            return 2 * pai * radius;       

        }

        public decimal getArea()       

        {

            return pai * radius * radius;

        }

        public void show()            

        {

            Console.WriteLine("圆形周长:{0}", getPerimeter());

            Console.WriteLine("圆形面积:{0}", getArea());

        }

    }

    class main

    {

        static void Main(string[] args)

        {

            Square sq = new Square(); Circle cir = new Circle();

            int i=0;

            while (i!=3)

            {

                Console.WriteLine("            1.正方形  2.圆形   3.退出");

                i = int.Parse(Console.ReadLine());

                switch (i)

                {

                    case 1:

                        sq.initialize();         

                        sq.show();              

                        break;

                    case 2:

                        cir.initialize();       

                        cir.show();              

                        break;

                    default: Console.WriteLine("输入错误"); break;

                }

            }

        }

    }

}

 

posted @ 2021-10-09 22:09  谦寻  阅读(43)  评论(0编辑  收藏  举报