Arkanium77

Triangle

Jun 26th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.57 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <iostream>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. /*
  10.  *
  11.  * “Си позволяет легко выстрелить себе в ногу;
  12.  * с C++ это сделать сложнее, но, когда вы это делаете,
  13.  * вы отстреливаете себе ногу целиком.”
  14.  *                                      - Бьерн Страуструп
  15.  *
  16.  */
  17.  
  18. class NegativeException {
  19.     string exc;
  20.  
  21.     friend ostream& operator<<(ostream& os, const NegativeException& vi) {
  22.         os << "исключение " << vi.exc;
  23.         return os;
  24.     }
  25. public:
  26.  
  27.     NegativeException() {
  28.         this->exc = "NegativeException";
  29.     }
  30. };
  31.  
  32. class SidesException {
  33.     string exc;
  34.  
  35.     friend ostream& operator<<(ostream& os, const SidesException& vi) {
  36.         os << "исключение " << vi.exc;
  37.         return os;
  38.     }
  39. public:
  40.  
  41.     SidesException() {
  42.         this->exc = "SidesException";
  43.     }
  44. };
  45.  
  46. class Triangle {
  47. private:
  48.  
  49.     double a, b, c;
  50.  
  51. public:
  52.  
  53.     Triangle(double a, double b, double c) {
  54.         if (a <= 0 || b <= 0 || c <= 0) {
  55.             throw NegativeException();
  56.         }
  57.         if (test(a, b, c)) {
  58.             throw SidesException();
  59.         }
  60.         this->a = a;
  61.         this->b = b;
  62.         this->c = c;
  63.     };
  64.  
  65.     Triangle(double ab, double c) {
  66.         if (ab <= 0 || c <= 0) {
  67.             throw NegativeException();
  68.         }
  69.         if (test(ab, ab, c)) {
  70.             throw SidesException();
  71.         }
  72.         this->a = ab;
  73.         this->b = ab;
  74.         this->c = c;
  75.     };
  76.  
  77.     Triangle(double a) {
  78.         if (a <= 0) {
  79.             throw NegativeException();
  80.         }
  81.         if (test(a, a, a)) {
  82.             throw SidesException();
  83.         }
  84.         this->a = a;
  85.         this->b = a;
  86.         this->c = a;
  87.     };
  88.  
  89.     Triangle() {
  90.         this->a = 1;
  91.         this->b = 1;
  92.         this->c = 1;
  93.     };
  94.  
  95.     Triangle(const Triangle &orig) {
  96.         this->a = orig.a;
  97.         this->b = orig.b;
  98.         this->c = orig.c;
  99.     };
  100.    
  101.     //Переопределённый вывод
  102.     friend ostream& operator<<(ostream& os, const Triangle& at) {
  103.         os << "a=" << at.a << ", b=" << at.b << ", c=" << at.c;
  104.         return os;
  105.     };
  106.  
  107.     ~Triangle() {
  108.     };
  109.  
  110.     double s() {
  111.         double P = p() / 2;
  112.         return sqrt(P * (P - a) * (P - b) * (P - c));
  113.     };
  114.  
  115.     double p() {
  116.         return a + b + c;
  117.     };
  118.  
  119.     /**
  120.      * <u>Радиус <b>вписанной</b> окружности</u><br>
  121.      *
  122.      * @return Радиус вписанной окружности
  123.      */
  124.     double r() {
  125.         return ((2 * s()) / (a + b + c));
  126.     };
  127.  
  128.     /**
  129.      * <u>Радиус <b>описанной</b> окружности</u><br>
  130.      *
  131.      * @return Радиус описанной окружности
  132.      */
  133.     double R() {
  134.         return (a * b * c) / (4 * s());
  135.     };
  136.  
  137.     bool operator==(Triangle a) {
  138.         return this->equals(a);
  139.     };
  140.  
  141.     bool operator!=(Triangle a) {
  142.         return !(this->equals(a));
  143.     };
  144.  
  145.     void operator=(Triangle b) {
  146.         this->a = b.a;
  147.         this->b = b.b;
  148.         this->c = b.c;
  149.     };
  150. protected:
  151.     //Проверка неравенства треугольника
  152.     static bool test(double a, double b, double c) {
  153.         if (a + b > c && a + c > b && c + b > a) {
  154.             return false;
  155.         }
  156.         return true;
  157.     };
  158.  
  159.     bool equals(const Triangle &two) {
  160.         if (this->a == two.a) {
  161.             if (this->b == two.b) {
  162.                 if (this->c == two.c) {
  163.                     return true;
  164.                 }
  165.             }
  166.             if (this->b == two.c) {
  167.                 if (this->c == two.b) {
  168.                     return true;
  169.                 }
  170.             }
  171.         }
  172.         if (this->b == two.a) {
  173.             if (this->c == two.b) {
  174.                 if (this->a == two.c) {
  175.                     return true;
  176.                 }
  177.             }
  178.             if (this->c == two.c) {
  179.                 if (this->a == two.b) {
  180.                     return true;
  181.                 }
  182.             }
  183.         }
  184.         if (this->c == two.a) {
  185.             if (this->a == two.b) {
  186.                 if (this->b == two.c) {
  187.                     return true;
  188.                 }
  189.             }
  190.             if (this->a == two.c) {
  191.                 if (this->b == two.b) {
  192.                     return true;
  193.                 }
  194.             }
  195.         }
  196.         return false;
  197.     };
  198. };
Advertisement
Add Comment
Please, Sign In to add comment