コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("homework3");
            Random ran = new Random();
            double shapeArea = 0, sumArea = 0;

            for (int i = 0; i < 10; i++)
            {
                int          RandKey      = ran.Next(0, 3);
                ShapeFactory shapeFactory = new ShapeFactory();
                Console.WriteLine(i + "号图形:");
                shapeArea = shapeFactory.GetShape(RandKey).GetArea();
                sumArea   = sumArea + shapeArea;
                Console.WriteLine("面积为" + shapeArea);
            }
            Console.WriteLine("" + sumArea);
        }
コード例 #2
0
        public IShape GetRandomShape()
        {
            int dice = rd.Next(ShapeFactory.Types);

            switch (dice)
            {
            case 0:
                return(ShapeFactory.GetSquare(RandomDouble()));

            case 1:
                return(ShapeFactory.GetRectangle(RandomDouble(), RandomDouble()));

            case 2:
                return(ShapeFactory.GetTriangle(RandomDouble(), RandomDouble(), RandomDouble()));

            default:
                throw new ArgumentOutOfRangeException("没有设置对应形状");
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            //三角形
            Shape myShip1 = ShapeFactory.CreatShape("Triangle");

            //打印出面积
            myShip1.GetS();

            //圆
            Shape myShip2 = ShapeFactory.CreatShape("Circle");

            myShip2.GetS();

            //正方形
            Shape myShip3 = ShapeFactory.CreatShape("Square");

            myShip3.GetS();

            //矩形
            Shape myShip4 = ShapeFactory.CreatShape("Rectangle");

            myShip4.GetS();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            try
            {
                ShapeFactory SF       = new ShapeFactory();
                Random       r        = new Random();
                double       Sum      = 0;
                Shape[]      Graphics = new Shape[10];
                for (int i = 0; i < 10; i++)
                {
                    switch (r.Next(1, 4))
                    {
                    case 1:
                        Graphics[i] = SF.Creat("triangle");
                        break;

                    case 2:
                        Graphics[i] = SF.Creat("rectangle");
                        break;

                    case 3:
                        Graphics[i] = SF.Creat("square");
                        break;
                    }
                }
                for (int i = 0; i < 10; i++)
                {
                    Graphics[i].Print();
                    Sum += Graphics[i].Area;
                }
                Console.WriteLine("AreaSum:" + Sum);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }