Advertisement
Lesnic

ITP exceptions 2 second

Apr 11th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 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() {
  12.         cin >> dimension;
  13.         arr = new int[dimension];
  14.         for (int i = 0; i < dimension; i++)
  15.             cin >> arr[i];
  16.     }
  17.  
  18.     friend int get_distance(Vector& a, Vector& b) {
  19.         if (a.dimension != b.dimension)
  20.             throw invalid_argument("invalid_argument");
  21.  
  22.         int c = 0, d = 0;
  23.         for (int i = 0; i < a.dimension; i++) {
  24.             d = abs(a.arr[i] - b.arr[i]);
  25.             c += (a.arr[i] - b.arr[i]) * (a.arr[i] - b.arr[i]);
  26.         }
  27.         if (d == 0)
  28.             throw domain_error("domain_error");
  29.  
  30.         c = sqrt(c);
  31.         if (c >= 100)
  32.             throw length_error("length_error");
  33.         return c;
  34.     }
  35. };
  36.  
  37. int main()
  38. {
  39.     Vector a, b;
  40.  
  41.     try {
  42.         cout << get_distance(a, b);
  43.     }
  44.     catch (invalid_argument & exception) {
  45.         cout << exception.what() << " exception";
  46.     }
  47.     catch (length_error & exception) {
  48.         cout << exception.what() << " exception";
  49.     }
  50.     catch (domain_error & exception) {
  51.         cout << exception.what() << " exception";
  52.     }
  53.     catch (exception & exception) {
  54.         cout << exception.what();
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement