-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex04.cpp
More file actions
83 lines (68 loc) · 1.37 KB
/
ex04.cpp
File metadata and controls
83 lines (68 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
using namespace std;
class Int {
private:
int value;
public:
Int():value(0) {
}
Int(int num):value(num) {
}
operator int() {return value;}
Int operator+(Int n2);
Int operator-(Int n2);
Int operator*(Int n2);
Int operator/(Int n2);
void display();
Int check(long double result);
};
void Int::display() {
cout << value;
}
Int Int::check(long double result) {
if (result > 2147483647.0L || result < -2147483647.0L) {
cout << "Ошибка!";
exit(1);
}
return Int(int(result));
}
Int Int::operator+(Int n2) {
return check(static_cast<long double>(value) + n2.value);
}
Int Int::operator-(Int n2) {
return check(static_cast<long double>(value) - n2.value);
}
Int Int::operator*(Int n2) {
return check(static_cast<long double>(value) * n2.value);
}
Int Int::operator/(Int n2) {
return check(static_cast<long double>(value) / n2.value);
}
int main() {
Int n1(30);
Int n2(5);
Int n3;
cout << "n1 = ";
n1.display();
cout << endl;
cout << "n2 = ";
n2.display();
cout << endl;
n3 = n1 + n2;
cout << "n1 + n2 = ";
n3.display();
cout << endl;
n3 = n1 - n2;
cout << "n1 - n2 = ";
n3.display();
cout << endl;
n3 = n1 * n2;
cout << "n1 * n2 = ";
n3.display();
cout << endl;
n3 = n1 / n2;
cout << "n1 / n2 = ";
n3.display();
cout << endl;
return 0;
}