示例#1
0
        static void Main(string[] args)
        {
            Shape        shape;
            ShapeFactory shapeFactory = new ShapeFactory();

            string[] shapes    = { "RECTANGLE", "SQUARE", "TRIANGLE" };
            Random   rand      = new Random();
            double   totalArea = 0; //有效的总面积
            double   tempArea;

            for (int i = 0; i < 10; i++)
            {
                shape    = shapeFactory.GetShape(shapes[rand.Next(3)]);
                tempArea = shape.AreaCalculate();
                if (tempArea != -1)
                {
                    totalArea += tempArea;
                }
                Console.WriteLine(shape.ToString());
            }

            Console.WriteLine("The total area of the legal shapes above are " + totalArea);
        }
示例#2
0
        private static void Main()
        {
            double totalArea = 0;

            for (var i = 1; i <= 10; i++)
            {
                var randomShapeNum = new Random().Next(0, 3);
                var randomShape    = randomShapeNum switch
                {
                    0 => "Rectangle",
                    1 => "Square",
                    2 => "Triangle",
                    _ => null
                };
                double[] sides = { new Random().NextDouble() * 10, new Random().NextDouble() * 10, new Random().NextDouble() * 10 };
                var      shape = ShapeFactory.GetShape(randomShape, sides);
                if (shape != null && shape.IsValid())
                {
                    totalArea += shape.GetArea();
                }
            }
            Console.WriteLine($"Total area is {totalArea}");
        }
    }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("选择模式:1、判断形状,计算面积;2、随机生成十个形状,计算其面积之和");
            string choose;

            choose = Console.ReadLine();
            switch (choose)
            {
            case "1":


            {
                Console.WriteLine("请输入点集 输入方式为    a,b c,d ...  ");
                string dotstr;
                dotstr = Console.ReadLine();
                dot[]    Dots = new dot[dotstr.Length - dotstr.Replace(",", "").Length];
                string[] dots = dotstr.Split(" ");
                for (int i = 0; i < dots.Length; i++)
                {
                    string[] a = dots[i].Split(",");
                    Dots[i] = new dot(float.Parse(a[0]), float.Parse(a[1]));
                }

                Console.WriteLine("需要判断的形状");
                Console.WriteLine("1、矩形;2、正方形;3、三角形");

                string choice;
                choice = Console.ReadLine();
                switch (choice)
                {
                case "1":
                    Shapes shape1 = ShapeFactory.GetShapes(ShapeTypes.Rectangle, Dots);
                    Console.WriteLine("形状判断结果为:" + shape1.judgeShape());
                    if (shape1.judgeShape())
                    {
                        Console.WriteLine("面积为:" + shape1.getArea());
                    }
                    break;

                case "2":
                    Shapes shape2 = ShapeFactory.GetShapes(ShapeTypes.Square, Dots);
                    Console.WriteLine("形状判断结果为:" + shape2.judgeShape());
                    if (shape2.judgeShape())
                    {
                        Console.WriteLine("面积为:" + shape2.getArea());
                    }
                    break;

                case "3":
                    Shapes shape3 = ShapeFactory.GetShapes(ShapeTypes.Triangle, Dots);
                    Console.WriteLine("形状判断结果为:" + shape3.judgeShape());
                    if (shape3.judgeShape())
                    {
                        Console.WriteLine("面积为:" + shape3.getArea());
                    }
                    break;

                default: Console.WriteLine("无该选项"); break;
                }
                break;
            }

            case "2":
            {
                Shapes[] shapes = new Shapes[10];
                float    x, y;
                dot[]    Dots1 = new dot[4];
                dot[]    Dots2 = new dot[3];
                Console.WriteLine("形状生成中...");
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine((i + 1) + "...");
                    Random ra  = new Random();
                    int    num = ra.Next(1, 4);
                    switch (num)
                    {
                    case 1:
                        x         = ra.Next(1, 40);
                        y         = ra.Next(1, 40);
                        Dots1[0]  = new dot(x, y);
                        x         = ra.Next(1, 40);
                        y         = ra.Next(1, 40);
                        Dots1[2]  = new dot(x, y);
                        Dots1[1]  = new dot(Dots1[0].y, Dots1[2].x);
                        Dots1[3]  = new dot(Dots1[0].x, Dots1[2].y);
                        shapes[i] = ShapeFactory.GetShapes(ShapeTypes.Rectangle, Dots1);
                        break;

                    case 2:
                        x         = ra.Next(1, 40);
                        y         = ra.Next(1, 40);
                        Dots1[0]  = new dot(x, y);
                        x         = ra.Next(1, 40);
                        Dots1[2]  = new dot(x, y + x - Dots1[0].x);
                        Dots1[1]  = new dot(Dots1[0].y, Dots1[2].x);
                        Dots1[3]  = new dot(Dots1[0].x, Dots1[2].y);
                        shapes[i] = ShapeFactory.GetShapes(ShapeTypes.Square, Dots1);
                        break;

                    default:
                        x         = ra.Next(1, 40);
                        y         = ra.Next(1, 40);
                        Dots2[0]  = new dot(x, y);
                        x         = ra.Next(1, 40);
                        y         = ra.Next(1, 40);
                        Dots2[1]  = new dot(x, y);
                        x         = ra.Next(1, 40);
                        y         = ra.Next(1, 40);
                        Dots2[2]  = new dot(x, y);
                        shapes[i] = ShapeFactory.GetShapes(ShapeTypes.Triangle, Dots2);
                        break;
                    }
                    //for (int j = 0; j < 4; j++)
                    //    Console.WriteLine(Dots2[1].x);
                }
                Console.WriteLine("生成完成,计算面积总和...");
                float sum = 0;
                for (int i = 0; i < 10; i++)
                {
                    sum += shapes[i].getArea();
                }
                Console.WriteLine("面积总和为:" + sum);
                break;
            }

            default: Console.WriteLine("无该选项"); break;
            }
        }
示例#4
0
 static void Main(string[] args)
 {
     Console.WriteLine("Hello World!");
     ShapeFactory.run();
 }