Skip to content

Commit ef72288

Browse files
committed
inc / dec and mod operators overloaded
1 parent 9fa1a67 commit ef72288

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

HugeInt.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using namespace std;
44
// статьи в помощь, если захочешь разобраться:
55
// https://habr.com/ru/post/172285/ (перегрузка оперторов)
66
// https://habr.com/ru/post/124258/ (умножение)
7+
// https://www.cyberforum.ru/cpp-beginners/thread1955526.html (остаток от деления)
78

89
HugeInt::HugeInt() {
910
for (int i = 0; i < 40; i++) {
@@ -13,6 +14,11 @@ HugeInt::HugeInt() {
1314
negative = false;
1415
}
1516

17+
HugeInt::HugeInt(string str) : HugeInt(str.size()) {
18+
set(str);
19+
shrink_to_fit();
20+
}
21+
1622
HugeInt::HugeInt(int d) {
1723
for (int i = 0; i < 40; i++) {
1824
number[i] = 0;
@@ -45,12 +51,14 @@ void HugeInt::set(string str) {
4551
number[j++] = str[i] - 48;
4652
}
4753
}
54+
shrink_to_fit();
4855
}
4956

5057
void HugeInt::set(int num[40]) {
5158
for (int i = 0; i < 40; i++) {
5259
number[i] = num[i];
5360
}
61+
shrink_to_fit();
5462
}
5563

5664
string HugeInt::to_str() const {
@@ -164,6 +172,28 @@ bool operator>=(HugeInt &n1, HugeInt &n2) {
164172
return (n1 > n2 || n1 == n2);
165173
}
166174

175+
HugeInt& HugeInt::operator++() {
176+
++(*this);
177+
return *this;
178+
}
179+
180+
HugeInt& HugeInt::operator--() {
181+
--(*this);
182+
return *this;
183+
}
184+
185+
HugeInt HugeInt::operator++(int) {
186+
HugeInt temp(*this), c1("1");
187+
*this = *this + c1;
188+
return temp;
189+
}
190+
191+
HugeInt HugeInt::operator--(int) {
192+
HugeInt temp(*this), c1("1");
193+
*this = *this - c1;
194+
return temp;
195+
}
196+
167197
HugeInt HugeInt::simple_sum(HugeInt &b) {
168198
int max = 0;
169199
if (depth > b.depth) {
@@ -314,13 +344,38 @@ HugeInt operator*(HugeInt &n1, HugeInt &n2) {
314344
return temp;
315345
}
316346

347+
HugeInt operator%(HugeInt &n1, HugeInt &n2) {
348+
if (n1 < n2) return n1;
349+
HugeInt a(n1), b(n2), main_sum("1");
350+
if (b.negative) {
351+
b = -b;
352+
a = -a;
353+
}
354+
for (HugeInt i("0"); i <= a; i = i + b) {
355+
main_sum++;
356+
}
357+
cout << " main sum: " << main_sum << endl;
358+
HugeInt last_sum = b * main_sum;
359+
cout << " last sum: " << last_sum << endl;
360+
361+
if (!a.negative) {
362+
// last_sum = a - last_sum;
363+
return a - last_sum;
364+
} else {
365+
// last_sum = b * main_sum;
366+
// last_sum = a + last_sum;
367+
return a + last_sum;
368+
}
369+
}
370+
317371
void HugeInt::shrink_to_fit() {
318372
for (int i = 39; i > -1; i--) {
319373
if (number[i] != 0) {
320374
depth = i + 1;
321375
break;
322376
}
323377
}
378+
if (depth == 0) depth++;
324379
}
325380

326381
void HugeInt::normalize() {

HugeInt.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class HugeInt {
2121

2222
public:
2323
HugeInt(); // конструктор по умолчанию
24+
HugeInt(std::string str);
2425
HugeInt(int depth);
2526
HugeInt(int numbers[40], int depth);
2627

@@ -50,11 +51,16 @@ class HugeInt {
5051
friend bool operator< (HugeInt &n1, HugeInt &n2);
5152
friend bool operator>= (HugeInt &n1, HugeInt &n2);
5253

54+
HugeInt& operator++(); // версия префикс
55+
HugeInt& operator--(); // версия префикс
56+
57+
HugeInt operator++(int); // версия постфикс
58+
HugeInt operator--(int); // версия постфикс
59+
5360
friend HugeInt operator+(HugeInt &n1, HugeInt &n2);
5461
friend HugeInt operator-(HugeInt &n1, HugeInt &n2);
5562
friend HugeInt operator*(HugeInt &n1, HugeInt &n2);
56-
57-
63+
friend HugeInt operator%(HugeInt &n1, HugeInt &n2);
5864

5965
friend std::ostream& operator<<(std::ostream &out, const HugeInt &n);
6066
friend std::istream& operator>>(std::istream &in, HugeInt &n);

L12_6.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
//* менять позицию на разряде
1313
//* урезать размер числа, чтобы не было нулей
1414
//? ДОП:
15-
// перегрузка операторов
16-
//* - операторы сравнения
17-
//* - операторы вывода на экран
18-
//* - математические операторы
19-
// умножение
20-
// нахождение остатка от деления
15+
//* перегрузка операторов
16+
//* |- операторы сравнения
17+
//* |- операторы вывода на экран
18+
//* |- математические операторы
19+
//* умножение
20+
//* нахождение остатка от деления
2121

2222
#include "HugeInt.h"
2323
using namespace std;
@@ -60,10 +60,13 @@ int main() {
6060
// проверка умножения
6161
HugeInt test1, test2;
6262
test1.set("123");
63-
test2.set("-42");
63+
test2.set("42");
6464

6565
HugeInt test_mult = test1 * test2;
6666
cout << "123 * 42 = " << test_mult << endl;
6767

68+
HugeInt test_mod = test1 % test2;
69+
cout << "123 % 42 = " << test_mod << endl;
70+
6871
return 0;
6972
}

0 commit comments

Comments
 (0)