Advertisement
Lesnic

ITP exceptions 2 first

Apr 11th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Vector {
  6. private:
  7.     int dimension;
  8.     int* arr;
  9.  
  10. public:
  11.     Vector(int dimension) {
  12.         this->dimension = dimension;
  13.         arr = new int[dimension];
  14.     }
  15.  
  16.     friend istream& operator >> (istream& cin, Vector& vector) {
  17.         for (int i = 0; i < vector.dimension; i++)
  18.             cin >> vector.arr[i];
  19.         return cin;
  20.     }
  21.  
  22.     bool isEquale(Vector b) {
  23.         for (int i = 0; i < dimension; i++) {
  24.             if (arr[i] != b.arr[i])
  25.                 return false;
  26.         }
  27.  
  28.         return true;
  29.     }
  30.  
  31.     friend int get_distance(Vector& a, Vector& b) {
  32.         if (a.dimension != b.dimension)
  33.             throw invalid_argument("invalid_argument exception");
  34.  
  35.         if (a.isEquale(b))
  36.             throw domain_error("domain_error exception");
  37.  
  38.         int c = 0;
  39.         for (int i = 0; i < a.dimension; i++)
  40.             c += (a.arr[i] - b.arr[i]) * (a.arr[i] - b.arr[i]);
  41.         c = sqrt(c);
  42.        
  43.         if (c >= 100)
  44.             throw length_error("length_error exception");
  45.        
  46.         return c;
  47.     }
  48. };
  49.  
  50. int main()
  51. {
  52.     int dimension;
  53.     cin >> dimension;
  54.     Vector a(dimension);
  55.     cin >> a;
  56.  
  57.     cin >> dimension;
  58.     Vector b(dimension);
  59.     cin >> b;
  60.  
  61.     try {
  62.         cout << get_distance(a, b);
  63.     }
  64.     catch (invalid_argument & exception) {
  65.         cout << exception.what();
  66.     }
  67.     catch (length_error & exception) {
  68.         cout << exception.what();
  69.     }
  70.     catch (domain_error & exception) {
  71.         cout << exception.what();
  72.     }
  73.     catch (exception & exception) {
  74.         cout << exception.what();
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement