Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- class Vector {
- private:
- int dimension;
- int* array;
- public:
- Vector() {
- dimension = 0;
- array = NULL;
- }
- friend istream& operator >> (istream& cin, Vector& now) {
- cin >> now.dimension;
- now.array = new int[now.dimension];
- for (int i = 0; i < now.dimension; i++)
- cin >> now.array[i];
- return cin;
- }
- friend ostream& operator << (ostream& cout, Vector now) {
- for (int i = 0; i < now.dimension; i++)
- cout << now.array[i];
- return cout;
- }
- friend int get_distance(Vector, Vector);
- };
- int get_distance(Vector first, Vector second) {
- int result = 0;
- if (first.dimension != second.dimension)
- throw invalid_argument("invalid_argument exception");
- bool check = true;
- for (int i = 0; i < first.dimension; i++) {
- check = check && first.array[i] == second.array[i];
- result += pow(first.array[i] - second.array[i], 2);
- }
- result = sqrt(result);
- if (check)
- throw domain_error("domain_error exception");
- if (result >= 100)
- throw length_error("length_error exception");
- return result;
- }
- int main() {
- Vector first, second;
- cin >> first >> second;
- try {
- cout << get_distance(first, second);
- }
- catch (invalid_argument & exception) {
- cout << exception.what();
- }
- catch (length_error & exception) {
- cout << exception.what();
- }
- catch (domain_error & exception) {
- cout << exception.what();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement