Skip to content

Commit 74c3d5c

Browse files
committed
comparison operators are overloaded
1 parent 6b0fdf7 commit 74c3d5c

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

HugeInt.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ int HugeInt::get_digit(int index) const {
9797
return number[index];
9898
}
9999

100-
101-
102100
int HugeInt::compare(HugeInt &b) const {
103101
if (negative && !b.negative) {
104102
return -1;
@@ -137,6 +135,30 @@ int HugeInt::compare(HugeInt &b) const {
137135
}
138136
}
139137

138+
bool operator==(HugeInt &n1, HugeInt &n2) {
139+
return (n1.compare(n2) == 0);
140+
}
141+
142+
bool operator!=(HugeInt &n1, HugeInt &n2) {
143+
return (n1.compare(n2) != 0);
144+
}
145+
146+
bool operator>(HugeInt &n1, HugeInt &n2) {
147+
return (n1.compare(n2) == 1);
148+
}
149+
150+
bool operator<=(HugeInt &n1, HugeInt &n2) {
151+
return (n1 < n2 || n1 == n2);
152+
}
153+
154+
bool operator<(HugeInt &n1, HugeInt &n2) {
155+
return (n1.compare(n2) == -1);
156+
}
157+
158+
bool operator>=(HugeInt &n1, HugeInt &n2) {
159+
return (n1 > n2 || n1 == n2);
160+
}
161+
140162
HugeInt HugeInt::simple_sum(HugeInt &b) {
141163
int max = 0;
142164
if (depth > b.depth) {

HugeInt.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ class HugeInt {
2727
void set_digit(int index, int digit); // set для разряда числа
2828
int get_digit(int index) const; // get для разряда числа
2929

30+
friend bool operator== (HugeInt &n1, HugeInt &n2);
31+
friend bool operator!= (HugeInt &n1, HugeInt &n2);
32+
33+
friend bool operator> (HugeInt &d1, HugeInt &d2);
34+
friend bool operator<= (HugeInt &d1, HugeInt &d2);
35+
36+
friend bool operator< (HugeInt &d1, HugeInt &d2);
37+
friend bool operator>= (HugeInt &d1, HugeInt &d2);
38+
3039
bool check_zero() const;
3140
int compare(HugeInt &b) const;
3241
HugeInt simple_sum(HugeInt &b);
@@ -37,7 +46,8 @@ class HugeInt {
3746
// операторы
3847
HugeInt operator-() const;
3948

40-
void shrink_to_fit();
49+
void shrink_to_fit(); // обрезаем ненужные нули
50+
4151
void print_full(); // print для теста
4252
void print(); // print для теста
4353
};

L12_6.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
//* урезать размер числа, чтобы не было нулей
1414
//? ДОП:
1515
// перегрузка операторов
16+
//* - операторы сравнения
17+
// - математические операторы
1618
// умножение
1719
// нахождение остатка от деления
1820

@@ -43,11 +45,11 @@ int main() {
4345
cout << b.get() << " - not zero " << endl;
4446
}
4547

46-
if (a.compare(b) == 1) {
48+
if (a > b) {
4749
cout << a.get() << " > " << b.get() << endl;
48-
} else if (a.compare(b) == -1) {
50+
} else if (a < b) {
4951
cout << a.get() << " < " << b.get() << endl;
50-
} else if (a.compare(b) == 0) {
52+
} else if (a == b) {
5153
cout << a.get() << " = " << b.get() << endl;
5254
}
5355

0 commit comments

Comments
 (0)