static void Main(string[] args) { double r = 3.0, h = 5.0; var circle = new Circle(r); var sphere = new Sphere(r); var cylinder = new Cylinder(r, h); // Display results. Console.WriteLine("Area of Circle = {0:F2}", circle.Area()); Console.WriteLine("Area of Sphere = {0:F2}", sphere.Area()); Console.WriteLine("Area of Cylinder = {0:F2}", cylinder.Area()); Console.ReadLine(); var manyShapes = new List <Shape> { circle, cylinder, sphere }; foreach (var shape in manyShapes) { var definitelyACircle = shape as Circle; definitelyACircle?.GrowCircle(2.5); Console.WriteLine($"Area of shape = {shape.Area()}"); } Console.ReadLine(); }
// method to display a Circle object private void Display(Circle c) { ListViewItem lvi = null; if (c is Cone) { // cast C to a cone object Cone co = (Cone)c; // create a listviewItem (one row in a listview) string[] items = { co.GetType().Name, co.Radius.ToString("f3"), co.Height.ToString("f3"), co.Perimeter().ToString("f3"), co.Area().ToString("f3"), co.Volume().ToString("f3") }; lvi = new ListViewItem(items); } else if (c is Cylinder) { // cast C to a cylinder object Cylinder cy = (Cylinder)c; // create a listviewItem (one row in a listview) string[] items = { cy.GetType().Name, cy.Radius.ToString("f3"), cy.Height.ToString("f3"), cy.Perimeter().ToString("f3"), cy.Area().ToString("f3"), cy.Volume().ToString("f3") }; lvi = new ListViewItem(items); } else if (c is Sphere) { // castc to a Sphere object Sphere sp = (Sphere)c; // create a listviewItem string[] items = { sp.GetType().Name, sp.Radius.ToString("f3"), "0", sp.Perimeter().ToString("f3"), sp.Area().ToString("f3"), sp.Volume().ToString("f3") }; lvi = new ListViewItem(items); } else { // create a listviewItem (one row in a listview) string[] items = { c.GetType().Name, c.Radius.ToString("f3"), "0", c.Perimeter().ToString("f3"), c.Area().ToString("f3"), c.Volume().ToString("f3") }; lvi = new ListViewItem(items); } // add the row to the listview listView1.Items.Add(lvi); // making the last item visible listView1.EnsureVisible(listView1.Items.Count - 1); }
public void AreaTestMethod() { Cylinder figure = new Cylinder(Color.black, Color.blue, 0.1, 1, 1); double expected = 3.14; double actual = figure.Area(); Assert.AreEqual(expected, Math.Round(actual, 2)); }
public void AreaTest() { Cylinder cyl = new Cylinder(Color.Red, Color.Yellow, 2, 3, 6); double expected = 169.64600329384882; double actual = cyl.Area(); Assert.AreEqual(expected, actual); }
public static void Main(string[] args) { Point point = new Point(5, 7); Console.WriteLine("点的坐标为{0}", point.ToString()); Circle circle = new Circle(point, 2.5); Console.WriteLine(circle.ToString()); Console.WriteLine("圆周长为{0},面积为{1}", circle.Perimeter(), circle.Area()); Cylinder cylinder = new Cylinder(point, 3, 6); Console.WriteLine(cylinder.ToString()); Console.WriteLine("圆柱体面积为{0},体积为{1}", cylinder.Area(), cylinder.Volume()); Point point1 = new Point(0, 0); Point point2 = new Point(0, 2); Point point3 = new Point(3, 0); Triangle triangle = new Triangle(point1, point2, point3); Console.WriteLine(triangle.ToString()); Console.WriteLine("三角形周长为{0},面积为{1}", triangle.Perimeter(), triangle.Area()); string s1 = triangle.IsEquicrural() ? "是" : "不是"; string s2 = triangle.IsEquilateral() ? "是" : "不是"; Console.WriteLine("三角形" + s1 + "等腰三角形," + s2 + "等边三角形"); Point point4 = new Point(3, 4); Trapaezoid trapaezoid = new Trapaezoid(point1, point2, point4, point3); Console.WriteLine(trapaezoid.ToString()); Console.WriteLine("梯形周长为{0},面积为{1},高为{2}", trapaezoid.Perimeter(), trapaezoid.Area(), trapaezoid.H); Console.WriteLine(trapaezoid.WhichParallel()); Point point5 = new Point(3, 2); Parallelogram parallelogram = new Parallelogram(point1, point2, point5, point3); Console.WriteLine(parallelogram.ToString()); Console.WriteLine("平行四边形周长为{0},面积为{1},两个高为{2},{3}", parallelogram.Perimeter(), parallelogram.Area(), parallelogram.H1, parallelogram.H2); Point point6 = new Point(1, 0); Point point7 = new Point(0, 5); Point point8 = new Point(1, 10); Point point9 = new Point(2, 5); Rhombus rhombus = new Rhombus(point6, point7, point8, point9); Console.WriteLine(rhombus.ToString()); Console.WriteLine("菱形周长为{0},面积为{1},两个高为{2},{3}", rhombus.Perimeter(), rhombus.Area(), rhombus.H1, rhombus.H2); Rectangle rectangle = new Rectangle(point1, point2, point5, point3); Console.WriteLine(rectangle.ToString()); Console.WriteLine("长方形周长为{0},面积为{1}", rectangle.Perimeter(), rectangle.Area()); Point point10 = new Point(2, 2); Point point11 = new Point(2, 0); Quadrate quadrate = new Quadrate(point1, point2, point10, point11); Console.WriteLine(quadrate.ToString()); Console.WriteLine("正方形周长为{0},面积为{1}", quadrate.Perimeter(), quadrate.Area()); Console.ReadLine(); }
static void Main() { double r = 3.0, h = 5.0; Shape c = new Circle(r); Shape s = new Sphere(r); Shape l = new Cylinder(r, h); // Display results. Console.WriteLine("Area of Circle = {0:F2}", c.Area()); Console.WriteLine("Area of Sphere = {0:F2}", s.Area()); Console.WriteLine("Area of Cylinder = {0:F2}", l.Area()); }
static void Main() { double r = 3.0, h = 5.0; Shape c = new Circle(r); Shape s = new Sphere(r); Shape l = new Cylinder(r, h); // Display results: Console.WriteLine("Area of Circle = {0:F2}", c.Area()); Console.WriteLine("Area of Sphere = {0:F2}", s.Area()); Console.WriteLine("Area of Cylinder = {0:F2}", l.Area()); }
static void Main() { double r = 3.0, h = 5.0; Dimensions c = new Circle(r); Dimensions s = new Sphere(r); Dimensions l = new Cylinder(r, h); // Display results: Console.Write(((int)c.Area()).ToString() + " "); Console.Write(((int)s.Area()).ToString() + " "); Console.Write(((int)l.Area()).ToString()); }
public static void Main() { double r = 3.0, h = 5.0; Dimensions c = new Circle(r); Dimensions s = new Sphere(r); Dimensions l = new Cylinder(r, h); // Display results: Console.WriteLine("Area of Circle = {0:F2}", c.Area()); Console.WriteLine("Area of Sphere = {0:F2}", s.Area()); Console.WriteLine("Area of Cylinder = {0:F2}", l.Area()); }
public static void Main() { double r = 3.0, h = 5.0; Dimensions c = new Circle(r); Dimensions s = new Sphere(r); Dimensions l = new Cylinder(r, h); Console.WriteLine("Area of Circle = {0}", string.Format("{0:0.00}", c.Area())); Console.WriteLine("Area of Sphere = {0}", string.Format("{0:0.00}", s.Area())); Console.WriteLine("Area of Cylinder = {0}", string.Format("{0:0.00}", l.Area())); Console.ReadLine(); }
public static void Main(string[] args) { //注意所有几何体的点按照逆时针输入 Circle circle = new Circle(0, 0, 1); Console.WriteLine("半径为1的圆的面积为:" + circle.Area().ToString()); Console.WriteLine("半径为1的圆的周长为:" + circle.CircumFerence().ToString()); Cylinder cylinder = new Cylinder(); cylinder.R = 1; cylinder.h = 1; Console.WriteLine("半径为1,高为1的圆柱体的表面积为:" + cylinder.Area().ToString()); Console.WriteLine("半径为1,高为1的圆柱体的体积为:" + cylinder.GetV().ToString()); Quadrilateral quadrilateral = new Quadrilateral(1, 2, 2, 3, 3, 4, 1, 4); Console.WriteLine("普通四边形的周长:" + quadrilateral.Perimeter().ToString()); Console.WriteLine("普通四边形的面积:" + quadrilateral.Area().ToString()); Trapaezoid trapaezoid = new Trapaezoid(0, 0, 2, 0, 2, 1, 1, 1); Console.WriteLine("梯形的周长:" + trapaezoid.Perimeter().ToString()); Console.WriteLine("梯形的面积:" + trapaezoid.Area().ToString()); Parallelogram parallelogram = new Parallelogram(0, 0, 2, 0, 3, 1, 1, 1); Console.WriteLine("平行四边形的周长:" + parallelogram.Perimeter().ToString()); Console.WriteLine("平行四边形的面积:" + parallelogram.Area().ToString()); Rectangle rectangle = new Rectangle(0, 0, 2, 0, 2, 1, 0, 1); Console.WriteLine("长为2,宽为1的矩形面积为:" + rectangle.Area().ToString()); Console.WriteLine("长为2,宽为1的矩形周长为:" + rectangle.Perimeter().ToString()); Rhombus rhombus = new Rhombus(0, 0, 2, 1, 3, 3, 1, 2); Console.WriteLine("菱形的周长:" + rhombus.Perimeter().ToString()); Console.WriteLine("菱形的面积:" + rhombus.Area().ToString()); Quadrate quadrate = new Quadrate(0, 0, 1, 0, 1, 1, 0, 1); Console.WriteLine("正方形的周长:" + quadrate.Perimeter().ToString()); Console.WriteLine("正方形的面积:" + quadrate.Area().ToString()); Triangle triangle = new Triangle(new Point(0, 0), new Point(1, 0), new Point(0, 1)); Console.WriteLine("三角形的面积为: " + triangle.Area().ToString()); Console.WriteLine("三角形的周长为: " + triangle.Perimeter().ToString()); }
static void Main() { double radius = 2.5; double height = 3.0; var ring = new Circle(radius); Console.WriteLine($"Area of the circle = {ring.Area():F2}"); // Output: Area of the circle = 19.63 var tube = new Cylinder(radius, height); Console.WriteLine($"Area of the cylinder = {tube.Area():F2}"); // Output: Area of the cylinder = 86.39 }
static void Main() { double radius = 2.5; double height = 3.0; Circle ring = new Circle(radius); Cylinder tube = new Cylinder(radius, height); Console.WriteLine("Area of the circle = {0:F2}", ring.Area()); Console.WriteLine("Area of the cylinder = {0:F2}", tube.Area()); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); }
private void Display(Circle c) { richTextBox1.AppendText(c.GetType().Name + ": "); if (c is Cylinder) { Cylinder cy = (Cylinder)c; double perimeter = cy.Perimeter(); double area = cy.Area(); double volume = cy.Volume(); richTextBox1.AppendText("radius:" + cy.Radius + " perimeter:" + perimeter.ToString("n") + " Area:" + area.ToString("n") + " Volume:" + volume.ToString("n") + "\n"); // scroll down richTextBox1.ScrollToCaret(); } else if (c is Sphere) { // Cast c to a sphere, then display sphere Sphere s = (Sphere)c; double perimeter = s.Perimeter(); double area = s.Area(); double volume = s.Volume(); richTextBox1.AppendText("radius:" + s.Radius + " perimeter:" + perimeter.ToString("n") + " Area:" + area.ToString("n") + " Volume:" + volume.ToString("n") + "\n"); // scroll down richTextBox1.ScrollToCaret(); } else { double perimeter = c.Perimeter(); double area = c.Area(); double volume = c.Volume(); richTextBox1.AppendText("radius:" + c.Radius + " perimeter:" + perimeter.ToString("n") + " Area:" + area.ToString("n") + " Volume:" + volume.ToString("n") + "\n"); // scroll down richTextBox1.ScrollToCaret(); } }
static void Main(string[] args) { double r = 3.0, h = 5.0; Circle c = new Circle(r); Sphere s = new Sphere(r); Cylinder l = new Cylinder(r, h); // Display results. Console.WriteLine("Area of Circle = {0:F2}", c.Area()); Console.WriteLine("Area of Sphere = {0:F2}", s.Area()); Console.WriteLine("Area of Cylinder = {0:F2}", l.Area()); //Geometrie_Library.AbstractSample.Shape shape = new Geometrie_Library.AbstractSample.Shape(); Console.WriteLine(c.ToString()); Console.ReadKey(); }
static void Main() { double radius = 2.5; double height = 3.0; double side = 4.0; double length = 4.0; double breadth = 5.0; Circle ring = new Circle(radius); Cylinder tube = new Cylinder(radius, height); Square sq = new Square(side); Rectangle rect = new Rectangle(length, breadth); System.Console.WriteLine("Area of the circle = {0:F2}", ring.Area()); System.Console.WriteLine("Area of the cylinder = {0:F2}", tube.Area()); System.Console.WriteLine("Area of the Square = {0:F2}", sq.Area()); System.Console.WriteLine("Area of the Rectangle = {0:F2}", rect.Area()); }
// 类 public void testClass() { MyClass p1 = new MyClass(); MyClass p2 = new MyClass(6, 7); Console.WriteLine("CoOrds #1 at {0}", p1); Console.WriteLine("CoOrds #2 at {0}", p2); Circle ring = new Circle(2); Cylinder tube = new Cylinder(2, 3); Console.WriteLine("Area of the circle = {0:F2}", ring.Area()); Console.WriteLine("Area of the cylinder = {0:F2}", tube.Area()); TimePeriod ti = new TimePeriod(3600*5); Console.WriteLine("Setid time is {0}", ti.hours); ShapeStatic.TestSe(); }
public static void Main(string[] args) { Circle circle = new Circle(37, 43, 2.5); Console.WriteLine("该圆的" + circle.ToString()); circle.X = 20; circle.Y = 20; Console.WriteLine("该圆的" + circle.ToString()); Console.WriteLine("-------------------------------------------------"); Cylinder cylinder = new Cylinder(32, 34, 4.5, 8); Console.WriteLine(cylinder.ToString()); Console.WriteLine("该圆柱体的底面积是:" + cylinder.Area()); Console.WriteLine("该圆柱体的体积是:" + cylinder.Volume()); Console.WriteLine("-------------------------------------------------"); Point p1 = new Point(3, 5); Point p2 = new Point(7, 5); Point p3 = new Point(5, 0); Triangle triangle = new Triangle(p1, p2, p3); Console.WriteLine(triangle.ToString()); Console.WriteLine("该三角形的周长是:" + triangle.Perimeter()); Console.WriteLine("该三角形的面积是:" + triangle.Area()); Console.WriteLine("-------------------------------------------------"); Point p4 = new Point(1, 0); Quadrilateral quadrilateral = new Quadrilateral(p4, p1, p2, p3); Console.WriteLine("该四边形的" + quadrilateral.ToString()); Console.WriteLine("该四边形的周长是:" + quadrilateral.Perimeter()); Console.WriteLine("该四边形的面积是:" + quadrilateral.Area()); Console.WriteLine("-------------------------------------------------"); Point p5 = new Point(9, 0); Trapaezoid trapaezoid = new Trapaezoid(p4, p1, p2, p5); Console.WriteLine("该梯形的" + trapaezoid.ToString()); Console.WriteLine("该梯形的周长是:" + trapaezoid.Perimeter()); Console.WriteLine("该梯形的高是:" + trapaezoid.GetHeight()); Console.WriteLine("该梯形的面积是:" + trapaezoid.Area()); Console.WriteLine("-------------------------------------------------"); Parallelogram parallelogram = new Parallelogram(p3, p1, p2, p5); Console.WriteLine("该平行四边形的" + parallelogram.ToString()); Console.WriteLine("该平行四边形的周长是:" + parallelogram.Perimeter()); Console.WriteLine("该平行四边形的高是:" + parallelogram.GetHeight()); Console.WriteLine("该平行四边形的面积是:" + parallelogram.Area()); Console.WriteLine("-------------------------------------------------"); Point p6 = new Point(5, 10); Rhombus rhombus = new Rhombus(p1, p6, p2, p3); Console.WriteLine("该菱形的" + rhombus.ToString()); Console.WriteLine("该菱形的周长是:" + rhombus.Perimeter()); Console.WriteLine("该菱形的面积是:" + rhombus.Area()); Console.WriteLine("-------------------------------------------------"); Point p7 = new Point(3, 10); Point p8 = new Point(7, 10); Quadrate quadrate = new Quadrate(p1, p7, p8, p2); Console.WriteLine("该正方形的" + quadrate.ToString()); Console.WriteLine("该正方形的周长是:" + quadrate.Perimeter()); Console.WriteLine("该正方形的面积是:" + quadrate.Area()); Console.WriteLine("-------------------------------------------------"); Point p9 = new Point(3, 0); Point p10 = new Point(7, 0); Rectangle rectangle = new Rectangle(p9, p7, p8, p10); Console.WriteLine("该长方形的" + rectangle.ToString()); Console.WriteLine("该长方形的周长是:" + rectangle.Perimeter()); Console.WriteLine("该长方形的面积是:" + rectangle.Area()); Console.WriteLine("-------------------------------------------------"); Console.ReadLine(); }
public static void Main(string[] args) { double perimeter; //周长 double area; //面积 double volume; //体积 //圆类 Circle circle = new Circle(37, 43, 2.5); Console.WriteLine(circle.ToString()); perimeter = circle.CircumFerence(); area = circle.Area(); Console.WriteLine("该圆的周长是:{0}", perimeter); Console.WriteLine("该圆的面积是:{0}", area); circle.X = 20; circle.Y = 20; Console.WriteLine("该圆的新圆心坐标是:{0},{1}", circle.X, circle.Y); Console.WriteLine(); //圆柱体 double high = 10; Cylinder cylinder = new Cylinder(circle, high); Console.WriteLine(cylinder.ToString()); area = cylinder.Area(); volume = cylinder.Volume(); Console.WriteLine("该圆柱体的面积是:{0}", area); Console.WriteLine("该圆柱体的体积是:{0}", volume); Console.WriteLine(); //三角形 Point point1 = new Point(1, 1); Point point2 = new Point(4, 5); Point point3 = new Point(3, 8); Line line1 = new Line(point1, point2); Line line2 = new Line(point2, point3); Line line3 = new Line(point1, point3); Triangle triangle1 = new Triangle(point1, point2, point3); Console.WriteLine(triangle1.ToString()); Triangle triangle2 = new Triangle(line1, line2, line3); perimeter = triangle2.Perimeter(); area = triangle2.Area(); Console.WriteLine("该三角形的周长是:{0}", perimeter); Console.WriteLine("该三角形的面积是:{0}", area); if (triangle2.IsEquicrural()) { Console.WriteLine("该三角形是一个等腰三角形。"); if (triangle2.IsEquilateral()) { Console.WriteLine("该三角形是一个等边三角形。"); } } else { Console.WriteLine("该三角形不是等腰三角形。"); } Console.WriteLine(); //四边形 Point point4 = new Point(7, 3); Line line4 = new Line(point1, point4); Line line5 = new Line(point3, point4); Quadrilateral quadrilateral1 = new Quadrilateral(point1, point2, point3, point4); Console.WriteLine(quadrilateral1.ToString()); Quadrilateral quadrilateral2 = new Quadrilateral(line1, line2, line4, line5); perimeter = quadrilateral2.Perimeter(); area = quadrilateral2.Area(); Console.WriteLine("该四边形的周长是:{0}", perimeter); Console.WriteLine("该四边形的面积是:{0}", area); Console.WriteLine(); //梯形 Point point5 = new Point(2, 1); Point point6 = new Point(9, 1); Point point7 = new Point(7, 4); Point point8 = new Point(4, 4); Line line6 = new Line(point5, point6); Line line7 = new Line(point6, point7); Line line8 = new Line(point7, point8); Line line9 = new Line(point8, point5); Trapaezoid trapaezoid1 = new Trapaezoid(point5, point6, point7, point8); Trapaezoid trapaezoid2 = new Trapaezoid(line6, line7, line8, line9); Console.WriteLine(trapaezoid2.ToString()); perimeter = trapaezoid2.Perimeter(); area = trapaezoid2.Area(); Console.WriteLine("该梯形的周长是:{0}", perimeter); Console.WriteLine("该梯形的面积是:{0}", area); if (trapaezoid2.IsIsosceles()) { Console.WriteLine("这是一个等腰梯形。"); } Console.WriteLine(); //平行四边形 Parallelogram parallelogram1 = new Parallelogram(point1, point2, point3, point4); Console.WriteLine(parallelogram1.ToString()); Point point9 = new Point(5, 2); Point point10 = new Point(7, 5); Line line10 = new Line(point5, point9); Line line11 = new Line(point9, point10); Line line12 = new Line(point10, point8); Parallelogram parallelogram2 = new Parallelogram(point5, point9, point10, point8); Console.WriteLine(parallelogram2.ToString()); Parallelogram parallelogram3 = new Parallelogram(line10, line11, line12, line9); perimeter = parallelogram3.Perimeter(); area = parallelogram3.Area(); Console.WriteLine("该平形四边形的周长是:{0}", perimeter); Console.WriteLine("该平行四边形的面积是:{0}", area); Console.WriteLine(); //长方形 Rectangle rectangle1 = new Rectangle(line10, line11, line12, line9); Console.WriteLine(rectangle1.ToString()); Point point13 = new Point(1, 5); Point point14 = new Point(9, 5); Line line13 = new Line(point1, point6); Line line14 = new Line(point6, point14); Line line15 = new Line(point14, point13); Line line16 = new Line(point13, point1); Rectangle rectangle2 = new Rectangle(line13, line14, line15, line16); Console.WriteLine(rectangle2.ToString()); perimeter = rectangle2.Perimeter(); area = rectangle2.Area(); Console.WriteLine("该长方形的周长是:{0}", perimeter); Console.WriteLine("该长方形的面积是:{0}", area); Console.WriteLine(); //菱形 Point point15 = new Point(7, 6); Point point16 = new Point(4, 8); Point point17 = new Point(1, 6); Line line17 = new Line(point8, point15); Line line18 = new Line(point15, point16); Line line19 = new Line(point16, point17); Line line20 = new Line(point17, point8); Rhombus rhombus = new Rhombus(line17, line18, line19, line20); Console.WriteLine(rhombus.ToString()); perimeter = rhombus.Perimeter(); area = rhombus.Area(); Console.WriteLine("该菱形的周长是:{0}", perimeter); Console.WriteLine("该菱形的面积是:{0}", area); Console.WriteLine(); //正方形 Point point18 = new Point(2, 6); Point point19 = new Point(6, 6); Line line21 = new Line(point8, point19); Line line22 = new Line(point19, point16); Line line23 = new Line(point16, point18); Line line24 = new Line(point18, point8); Quadrate quadrate = new Quadrate(line21, line22, line23, line24); Console.WriteLine(quadrate.ToString()); perimeter = quadrate.Perimeter(); area = quadrate.Area(); Console.WriteLine("该正方形的周长是:{0}", perimeter); Console.WriteLine("该正方形的面积是:{0}", area); Console.WriteLine(); }