Не могу найти ошибку в подсчете периметра - C#
Формулировка задачи:
Программа должна считать периметр и длину стороны
Сторону считает а периметр нет
Листинг программы
- class Point
- {
- double x, y;
- string name;
- public Point(double x, double y, string name)
- {
- this.x = x;
- this.y = y;
- this.name = name;
- }
- public double X
- {
- get
- {
- return x;
- }
- }
- public double Y
- {
- get
- {
- return y;
- }
- }
- public string Name
- {
- get
- {
- return name;
- }
- }
- }
- class Figure
- {
- Point[] Points;
- string name;
- public Figure(Point p1,Point p2,Point p3)
- {
- Points = new Point[3];
- Points[0] = p1;
- Points[1] = p2;
- Points[2] = p3;
- this.name = "Triangle";
- }
- public Figure(Point p4) : base(){
- Points = new Point[4];
- Points[3] = p4;
- this.name = "Rectangle";
- }
- public Figure(Point p4,Point p5) : base()
- {
- Points = new Point[5];
- Points[3] = p4;
- Points[4] = p5;
- this.name = "Pentageon";
- }
- public double LengthSide(Point A,Point B)
- {
- double result= Math.Sqrt(Math.Pow(B.X - A.X, 2) + Math.Pow(B.Y - A.Y, 2));
- Console.WriteLine(result);
- return result;
- }
- public double PerimeterCalculator()
- {
- double length = 0;
- for(int i = 0; i < Points.Length; i++)
- {
- double len = LengthSide(Points[i], Points[i++]);
- length += len;
- }
- Console.WriteLine(length);
- Console.WriteLine(name);
- return length;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Point p1 = new Point(0, 5,"first");
- Point p2 = new Point(5, 0, "second");
- Point p3 = new Point(-5, 0, "third");
- Figure figure = new Figure(p1, p2, p3);
- figure.LengthSide(p1, p2);
- Console.WriteLine();
- figure.PerimeterCalculator();
- }
- }
Разобрался
Решение задачи: «Не могу найти ошибку в подсчете периметра»
textual
Листинг программы
- public double PerimeterCalculator()
- {
- double length = 0;
- double len;
- for (int i = 0; i < Points.Length - 1; i++) // // обратите внмание Points.Length - 1
- {
- len = LengthSide(Points[i], Points[i + 1]); // i не увеличивается
- length += len;
- }
- len = LengthSide(Points[0], Points[Points.Length - 1]); // здест расчитыается длина последней стороный, т.е Points[0] и Points[2]
- length += len;
- Console.WriteLine("Length = " + length);
- return length;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д