Операторы для класса Triangle - C#
Формулировка задачи:
В класс Triangle добавить:
a. Индексатор, позволяющий по индексу 0 обращаться к полю a, по индексу 1 – к полю b, по
индексу 2 – к полю c, при других значениях индекса выдается сообщение об ошибке.
b. Перегрузку:
операции ++ (--): одновременно увеличивает (уменьшает) значение полей a, b и c на 1;
констант true и false: обращение к экземпляру класса дает значение true, если
треугольник с заданными длинами сторон существует, иначе false;
операции *: одновременно домножает поля a, b и c на скаляр;
преобразования типа Triangle в string (и наоборот)
Решение задачи: «Операторы для класса Triangle»
textual
Листинг программы
- using System;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- namespace ConsoleApplication1
- {
- classTriangle
- {
- int a, b, c;
- publicint A
- {
- get
- {
- return a;
- }
- set
- {
- a = value;
- }
- }
- publicint B
- {
- get
- {
- return b;
- }
- set
- {
- b = value;
- }
- }
- publicint C
- {
- get
- {
- return c;
- }
- set
- {
- c = value;
- }
- }
- publicboolisTriangle
- {
- get
- {
- if (a + b > c && a + c > b && b + c > a)
- {
- returntrue;
- }
- returnfalse;
- }
- }
- public Triangle(int a_, int b_, int c_)
- {
- isCorrect(a_, b_, c_);
- A = a_;
- B = b_;
- C = c_;
- }
- publicvoidPrintSides()
- {
- Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
- }
- publicintPerimetr()
- {
- return a + b + c;
- }
- publicdouble Square()
- {
- returnMath.Sqrt(halfper(this) * (halfper(this) - a) * (halfper(this) - b)
- * (halfper(this) - c));
- }
- staticdoublehalfper(Triangle Ob)
- {
- returnOb.Perimetr() / 2.0;
- }
- staticvoidisCorrect(int a, int b, int c)
- {
- if (a < 0 || b < 0 || c < 0)
- thrownewException("Sides of triangle can` t be negative");
- }
- publicstaticTriangleoperator ++(Triangle Ob)
- {
- returnnewTriangle(++Ob.a, ++Ob.b, ++Ob.c);
- }
- publicstaticTriangleoperator --(Triangle Ob)
- {
- returnnewTriangle(--Ob.a, --Ob.b, --Ob.c);
- }
- publicstaticTriangleoperator *(Triangle Ob, intmult)
- {
- returnnewTriangle(Ob.a * mult, Ob.b * mult, Ob.c * mult);
- }
- publicoverridestringToString()
- {
- return"Стороны: " + "a = " + A + " b = " + B + " c = " + C;
- }
- publicintthis[intidx]
- {
- get
- {
- if (idx == 1)
- return a;
- elseif (idx == 2)
- return b;
- elseif (idx == 3)
- return c;
- else
- thrownewException("idx can be only 1, 2 and 3");
- }
- set
- {
- if (idx == 1)
- a = value;
- elseif (idx == 2)
- b = value;
- elseif (idx == 3)
- c = value;
- else
- thrownewException("idx can be only 1, 2 and 3");
- }
- }
- publicstaticbooloperatortrue(Triangle t)
- {
- returnt.isTriangle;
- }
- publicstaticbooloperatorfalse(Triangle t)
- {
- returnt.isTriangle;
- }
- };
- classProgram
- {
- staticvoid Main(string[] args)
- {
- try
- {
- Triangle Ob = newTriangle(3, 3, 3);
- if (Ob)
- Ob.PrintSides();
- else
- thrownewException("There is no triangle with such sides");
- Console.WriteLine("Perimetr={0}", Ob.Perimetr());
- Console.WriteLine("Sqaure={0}", Ob.Square());
- Ob++;
- Ob.PrintSides();
- --Ob;
- Ob.PrintSides();
- Ob *= 5;
- Ob.PrintSides();
- Console.WriteLine(Ob);
- Console.WriteLine(Ob[1]);
- Ob[2] = 30;
- Console.WriteLine(Ob);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- finally
- {
- Console.ReadKey();
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д