static void Main(string[] args) { try { ShapeFactory sf = new ShapeFactory(); ArrayList shapes = new ArrayList(); shapes.Add(sf.createTriangle(3, 4, 5)); shapes.Add(sf.createCircle(10)); shapes.Add(sf.createSquare(10)); shapes.Add(sf.createRectangle(6, 8)); foreach (Shape shape in shapes) { Console.WriteLine(shape.Info + $"area={shape.GetArea()}"); } } catch (Exception e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { try { List <Shape> shapes = new List <Shape>(); //创建线性表图形类型s for (int i = 0; i < 20; i++) //循环创建10个随机图形 { shapes.Add(ShapeFactory.CreateRandomShape()); } foreach (Shape shape in shapes) //循环遍历每个图形打印图形信息和面积 { Console.WriteLine(shape.Info + $", area={shape.Area}"); //{shape.Info} } } catch (Exception e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { try { List <Shape> shapes = new List <Shape>(); for (int i = 0; i < 10; i++) { shapes.Add(ShapeFactory.CreateRandomShape()); } foreach (Shape shape in shapes) { Console.WriteLine(shape.Info + $"{shape.Info}, area={shape.Area}"); } } catch (Exception e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { Random rd = new Random(); Shape[] shapes = new Shape[10]; double totalArea = 0; for (int i = 0; i < 10; i++) { shapes[i] = ShapeFactory.CreatRandomShape(rd.Next(1, 3)); } foreach (Shape shape in shapes) { totalArea += shape.GetArea(); } Console.WriteLine(totalArea); }
static void Main(string[] args) { String[] type = { "rectangle", "square", "triangle", "circle" }; //表示可用的形状 int[] parameterNumbers = { 2, 1, 3, 1 }; //创建对应形状所需要的参数 Shape shape; double sumArea = 0.0; for (int i = 0; i < 10; i++) { int randomType = new Random().Next(0, type.Length); double[] parameters = new double[parameterNumbers[randomType]]; //参数 do { for (int j = 0; j < parameters.Length; j++) { Random random = new Random(Guid.NewGuid().GetHashCode()); parameters[j] = random.NextDouble() + random.Next(0, 100); } shape = ShapeFactory.Product(type[randomType], parameters); } while (shape == null); sumArea += shape.Area; } Console.WriteLine($"The total area is {sumArea}."); }
static void Main(string[] args) { try { //实例化各种图形 double[] T = new double[2]; Console.Write("Please enter the bottom margin of the triangle: "); T[0] = Convert.ToDouble(Console.ReadLine()); Console.Write("Please enter the height of the triangle: "); T[1] = Convert.ToDouble(Console.ReadLine()); Shape triangle = ShapeFactory.createShape("Triangle", T); Console.WriteLine(); double[] C = new double[1]; Console.Write("Please enter the radius of the circle: "); C[0] = Convert.ToDouble(Console.ReadLine()); Shape circle = ShapeFactory.createShape("Circle", C); Console.WriteLine(); double[] S = new double[1]; Console.Write("Please enter the length of a side of the square: "); S[0] = Convert.ToDouble(Console.ReadLine()); Shape square = ShapeFactory.createShape("Square", S); Console.WriteLine(); double[] R = new double[1]; Console.Write("Please enter the width of the rectangle: "); T[0] = Convert.ToDouble(Console.ReadLine()); Console.Write("Please enter the height of the rectangle: "); T[1] = Convert.ToDouble(Console.ReadLine()); Shape rectangle = ShapeFactory.createShape("Rectangle", T); Console.WriteLine(); //获取图形 if (triangle != null) { triangle.Draw(); Console.WriteLine("The area of the triangle is " + triangle.ShapeAreaShow()); Console.WriteLine(); } if (circle != null) { circle.Draw(); Console.WriteLine("The area of the circle is " + circle.ShapeAreaShow()); Console.WriteLine(); } if (square != null) { square.Draw(); Console.WriteLine("The area of the square is " + square.ShapeAreaShow()); Console.WriteLine(); } if (rectangle != null) { rectangle.Draw(); Console.WriteLine("The area of the rectangle is " + rectangle.ShapeAreaShow()); Console.WriteLine(); } } catch (Exception e) { Console.WriteLine("input error." + e.Message); } }