Advertisement
amarek

OOP LV5 - Zadatak1

Nov 11th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class GeoException : public exception {
  6. private:
  7.     double mA, mB, mC;
  8. public:
  9.     GeoException(double, double, double);
  10.     double getA();
  11.     double getB();
  12.     double getC();
  13. };
  14.  
  15. GeoException::GeoException(double A, double B, double C) : mA(A), mB(B), mC(C) {}
  16.  
  17. double GeoException::getA() {
  18.     return mA;
  19. }
  20.  
  21. double GeoException::getB() {
  22.     return mB;
  23. }
  24.  
  25. double GeoException::getC() {
  26.     return mC;
  27. }
  28.  
  29. class Trokut {
  30. private:
  31.     double mA, mB, mC;
  32. public:
  33.     Trokut();
  34.     Trokut(double, double, double);
  35.     double povrsina();
  36.     double opseg();
  37.     bool pravokutan();
  38. };
  39.  
  40. Trokut::Trokut() : mA(0), mB(0), mC(0) {}
  41.  
  42. Trokut::Trokut(double A, double B, double C) : mA(A), mB(B), mC(C) {
  43.     if (mC >= mA + mB || mB >= mA + mC || mA >= mB + mC) {
  44.         throw GeoException(mA, mB, mC);
  45.     }
  46. }
  47.  
  48. double Trokut::povrsina() {
  49.     double s = (mA + mB + mC) / 2;
  50.     return sqrt(s*(s - mA)*(s - mB)*(s - mC));
  51. }
  52.  
  53. double Trokut::opseg() {
  54.     return mA + mB + mC;
  55. }
  56.  
  57. bool Trokut::pravokutan() {
  58.     if (mC == sqrt(mA * mA + mB * mB)) {
  59.         return true;
  60.     }
  61.     return false;
  62. }
  63.  
  64. int main() {
  65.     try {
  66.         Trokut T1, T2(7, 5, 6);
  67.         cout << "Prvi trokut: " << endl;
  68.         cout << "Opseg: " << T1.opseg() << "\nPovrsina: " << T1.povrsina() << endl;
  69.         cout << "Drugi trokut: " << endl;
  70.         cout << "Opseg: " << T2.opseg() << "\nPovrsina: " << T2.povrsina() << endl;
  71.  
  72.         if (T2.pravokutan()) {
  73.             cout << "Trokut je pravokutan." << endl;
  74.         }
  75.         else {
  76.             cout << "Trokut nije pravokutan." << endl;
  77.         }
  78.        
  79.     }
  80.     catch (GeoException& iznimka) {
  81.         cout << "Trokut sa stranicama: " << iznimka.getA() << ", " << iznimka.getB() << ", " << iznimka.getC() << " nije moguc." << endl;
  82.     }
  83.  
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement