Skip to content

Commit 9fa1a67

Browse files
committed
multiplication is implemented
1 parent 324e3c9 commit 9fa1a67

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

HugeInt.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "HugeInt.h"
22
using namespace std;
33

4+
// статьи в помощь, если захочешь разобраться:
5+
// https://habr.com/ru/post/172285/ (перегрузка оперторов)
6+
// https://habr.com/ru/post/124258/ (умножение)
7+
48
HugeInt::HugeInt() {
59
for (int i = 0; i < 40; i++) {
610
number[i] = 0;
@@ -286,11 +290,28 @@ HugeInt HugeInt::operator-() const {
286290
}
287291

288292
HugeInt operator+(HugeInt &n1, HugeInt &n2) {
289-
return n1.sum(n2);
293+
HugeInt res = n1.sum(n2);
294+
res.shrink_to_fit();
295+
return res;
290296
}
291297

292298
HugeInt operator-(HugeInt &n1, HugeInt &n2) {
293-
return n1.dif(n2);
299+
HugeInt res = n1.dif(n2);
300+
res.shrink_to_fit();
301+
return res;
302+
}
303+
304+
HugeInt operator*(HugeInt &n1, HugeInt &n2) {
305+
HugeInt temp;
306+
temp.negative = n1.negative != n2.negative;
307+
for (int i = 0; i < n1.depth; i++) {
308+
for (int j = 0; j < n2.depth; j++) {
309+
temp.number[i + j] += n1.get_digit(i) * n2.get_digit(j);
310+
}
311+
}
312+
temp.normalize();
313+
temp.shrink_to_fit();
314+
return temp;
294315
}
295316

296317
void HugeInt::shrink_to_fit() {
@@ -302,6 +323,17 @@ void HugeInt::shrink_to_fit() {
302323
}
303324
}
304325

326+
void HugeInt::normalize() {
327+
for (int i = 0; i < 40; i++) {
328+
if (number[i] > 9) {
329+
if (i == 39) cout << " [HugeInt owerflow] ";
330+
int carry = number[i] / 10;
331+
number[i + 1] += carry;
332+
number[i] -= carry * 10;
333+
}
334+
}
335+
}
336+
305337
std::ostream& operator<<(std::ostream &out, const HugeInt &n) {
306338
out << n.to_str();
307339
return out;
@@ -316,7 +348,7 @@ std::istream& operator>>(std::istream &in, HugeInt &n) {
316348

317349
void HugeInt::print_full() {
318350
if (negative) cout << "-";
319-
for (int i = 39; i > -1; cout << number[i--]);
351+
for (int i = 39; i > -1; cout << number[i--] << " ");
320352
cout << endl;
321353
}
322354

HugeInt.h

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

13+
void normalize(); // приводим число к 10-чной системе счисления
14+
void shrink_to_fit(); // обрезаем ненужные нули
15+
1316
int compare(HugeInt &b) const;
1417
HugeInt simple_sum(HugeInt &b);
1518
HugeInt simple_dif(HugeInt &b);
@@ -21,18 +24,23 @@ class HugeInt {
2124
HugeInt(int depth);
2225
HugeInt(int numbers[40], int depth);
2326

24-
void set(std::string str); // set для массива цифр
25-
void set(int numbers[40]); // set для массива цифр
26-
std::string to_str() const; // get для массива цифр
27+
void set(std::string str); // set для массива цифр
28+
void set(int numbers[40]); // set для массива цифр
29+
std::string to_str() const; // get для массива цифр
2730

2831
void set_minus(bool state); // set для знака
2932
bool has_minus() const; // get для знака
3033
void set_depth(int dep); // set для длины числа
3134
int get_depth() const; // get для длины числа
35+
bool check_zero() const;
3236

3337
void set_digit(int index, int digit); // set для разряда числа
3438
int get_digit(int index) const; // get для разряда числа
3539

40+
// операторы
41+
42+
HugeInt operator-() const;
43+
3644
friend bool operator== (HugeInt &n1, HugeInt &n2);
3745
friend bool operator!= (HugeInt &n1, HugeInt &n2);
3846

@@ -44,17 +52,13 @@ class HugeInt {
4452

4553
friend HugeInt operator+(HugeInt &n1, HugeInt &n2);
4654
friend HugeInt operator-(HugeInt &n1, HugeInt &n2);
55+
friend HugeInt operator*(HugeInt &n1, HugeInt &n2);
4756

48-
bool check_zero() const;
49-
50-
// операторы
51-
HugeInt operator-() const;
52-
53-
void shrink_to_fit(); // обрезаем ненужные нули
57+
5458

5559
friend std::ostream& operator<<(std::ostream &out, const HugeInt &n);
5660
friend std::istream& operator>>(std::istream &in, HugeInt &n);
57-
//? тест
61+
5862
void print_full(); //? print для теста
5963
void print(); //? print для теста
6064
};

L12_6.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ using namespace std;
2424

2525
int main() {
2626
HugeInt a, b, s;
27-
a.print_full();
28-
a.print();
2927

3028
a.set("1000");
3129
b.set("7");
32-
a.print_full();
33-
a.print();
3430

3531
if (a.check_zero()) {
3632
cout << a << " - its zero " << endl;
@@ -54,18 +50,20 @@ int main() {
5450

5551
s = a - b;
5652
cout << "Difference = " << s << endl;
57-
s.print_full();
58-
s.print();
59-
s.shrink_to_fit();
60-
s.print();
6153

6254
s = a + b;
6355
cout << "Summ = " << s << endl;
6456

6557
s = -s;
6658
cout << s << endl;
67-
s.shrink_to_fit();
68-
s.print();
59+
60+
// проверка умножения
61+
HugeInt test1, test2;
62+
test1.set("123");
63+
test2.set("-42");
64+
65+
HugeInt test_mult = test1 * test2;
66+
cout << "123 * 42 = " << test_mult << endl;
6967

7068
return 0;
7169
}

0 commit comments

Comments
 (0)