-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBai61.cpp
74 lines (64 loc) · 1.25 KB
/
Bai61.cpp
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
/*
Chồng toán tử: + - * / hai đa thức
Những toán tử nào không thể nạp chồng?
. :: ?: sizeof
*/
#include <iostream>
using namespace std;
class PhanSo {
int tu;
int mau;
public:
PhanSo() {
tu = 0;
mau = 1;
}
PhanSo(int a, int b) {
tu = a;
mau = (b == 0) ? 1 : b;
}
void showInfo() {
cout << tu << "/" << mau << endl;
}
PhanSo operator + (PhanSo const&);
PhanSo operator * (PhanSo const&);
friend PhanSo operator - (PhanSo const&, PhanSo const&);
friend PhanSo operator / (PhanSo const&, PhanSo const&);
};
PhanSo PhanSo::operator+(PhanSo const& b) {
PhanSo s;
s.tu = tu * b.mau + mau * b.tu;
s.mau = mau * b.mau;
return s;
}
PhanSo PhanSo::operator*(PhanSo const& b) {
PhanSo s;
s.tu = tu * b.tu;
s.mau = mau * b.mau;
return s;
}
PhanSo operator-(PhanSo const& a, PhanSo const& b) {
PhanSo s;
s.tu = a.tu * b.mau - a.mau * b.tu;
s.mau = a.mau * b.mau;
return s;
}
PhanSo operator/(PhanSo const& a, PhanSo const& b) {
PhanSo s;
s.tu = a.tu * b.mau;
s.mau = a.mau * b.tu;
return s;
}
int main() {
PhanSo a(10, 3);
PhanSo b(6, 9);
PhanSo tong = a + b;
PhanSo hieu = a - b;
PhanSo tich = a * b;
PhanSo thuong = a / b;
tong.showInfo();
hieu.showInfo();
tich.showInfo();
thuong.showInfo();
return 0;
}