Skip to content

Commit 324e3c9

Browse files
committed
plus & minus operators overloaded
1 parent ae54d71 commit 324e3c9

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

HugeInt.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void HugeInt::set(int num[40]) {
4949
}
5050
}
5151

52-
string HugeInt::get() const {
52+
string HugeInt::to_str() const {
5353
string str;
5454
if (negative) {
5555
str += '-';
@@ -133,6 +133,7 @@ int HugeInt::compare(HugeInt &b) const {
133133
return 0;
134134
}
135135
}
136+
return 0;
136137
}
137138

138139
bool operator==(HugeInt &n1, HugeInt &n2) {
@@ -235,6 +236,7 @@ HugeInt HugeInt::sum(HugeInt &b) {
235236
return result;
236237
}
237238
}
239+
return *this;
238240
}
239241

240242
HugeInt HugeInt::dif(HugeInt &b) {
@@ -272,6 +274,7 @@ HugeInt HugeInt::dif(HugeInt &b) {
272274
} else if (!negative && b.negative) {
273275
return this->simple_sum(b);
274276
}
277+
return *this;
275278
}
276279

277280
// операторы
@@ -282,6 +285,14 @@ HugeInt HugeInt::operator-() const {
282285
return temp;
283286
}
284287

288+
HugeInt operator+(HugeInt &n1, HugeInt &n2) {
289+
return n1.sum(n2);
290+
}
291+
292+
HugeInt operator-(HugeInt &n1, HugeInt &n2) {
293+
return n1.dif(n2);
294+
}
295+
285296
void HugeInt::shrink_to_fit() {
286297
for (int i = 39; i > -1; i--) {
287298
if (number[i] != 0) {
@@ -292,7 +303,7 @@ void HugeInt::shrink_to_fit() {
292303
}
293304

294305
std::ostream& operator<<(std::ostream &out, const HugeInt &n) {
295-
out << n.get();
306+
out << n.to_str();
296307
return out;
297308
}
298309

HugeInt.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ class HugeInt {
1010
int depth;
1111
bool negative;
1212

13+
int compare(HugeInt &b) const;
14+
HugeInt simple_sum(HugeInt &b);
15+
HugeInt simple_dif(HugeInt &b);
16+
HugeInt sum(HugeInt &b);
17+
HugeInt dif(HugeInt &b);
18+
1319
public:
1420
HugeInt(); // конструктор по умолчанию
1521
HugeInt(int depth);
1622
HugeInt(int numbers[40], int depth);
1723

1824
void set(std::string str); // set для массива цифр
1925
void set(int numbers[40]); // set для массива цифр
20-
std::string get() const; // get для массива цифр
26+
std::string to_str() const; // get для массива цифр
2127

2228
void set_minus(bool state); // set для знака
2329
bool has_minus() const; // get для знака
@@ -33,15 +39,13 @@ class HugeInt {
3339
friend bool operator> (HugeInt &d1, HugeInt &d2);
3440
friend bool operator<= (HugeInt &d1, HugeInt &d2);
3541

36-
friend bool operator< (HugeInt &d1, HugeInt &d2);
37-
friend bool operator>= (HugeInt &d1, HugeInt &d2);
42+
friend bool operator< (HugeInt &n1, HugeInt &n2);
43+
friend bool operator>= (HugeInt &n1, HugeInt &n2);
44+
45+
friend HugeInt operator+(HugeInt &n1, HugeInt &n2);
46+
friend HugeInt operator-(HugeInt &n1, HugeInt &n2);
3847

3948
bool check_zero() const;
40-
int compare(HugeInt &b) const;
41-
HugeInt simple_sum(HugeInt &b);
42-
HugeInt simple_dif(HugeInt &b);
43-
HugeInt sum(HugeInt &b);
44-
HugeInt dif(HugeInt &b);
4549

4650
// операторы
4751
HugeInt operator-() const;

L12_6.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// перегрузка операторов
1616
//* - операторы сравнения
1717
//* - операторы вывода на экран
18-
// - математические операторы
18+
//* - математические операторы
1919
// умножение
2020
// нахождение остатка от деления
2121

@@ -52,14 +52,14 @@ int main() {
5252
cout << a << " = " << b << endl;
5353
}
5454

55-
s = a.dif(b);
55+
s = a - b;
5656
cout << "Difference = " << s << endl;
5757
s.print_full();
5858
s.print();
5959
s.shrink_to_fit();
6060
s.print();
6161

62-
s = a.sum(b);
62+
s = a + b;
6363
cout << "Summ = " << s << endl;
6464

6565
s = -s;

0 commit comments

Comments
 (0)