Skip to content

Commit 9275cd1

Browse files
committed
trying to optimize code in Lab 12 var 6
1 parent 8f2ad83 commit 9275cd1

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

Variants/Lab_12/var_6/HugeInt.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ HugeInt& HugeInt::operator++() {
176176
++(*this);
177177
return *this;
178178
}
179-
179+
180180
HugeInt& HugeInt::operator--() {
181181
--(*this);
182182
return *this;
@@ -209,6 +209,7 @@ HugeInt HugeInt::simple_sum(HugeInt &b) {
209209
}
210210
if (result[max]) max++;
211211
HugeInt res(result, max);
212+
res.shrink_to_fit();
212213
return res;
213214
}
214215

@@ -228,6 +229,7 @@ HugeInt HugeInt::simple_dif(HugeInt &b) {
228229
}
229230
}
230231
HugeInt res(result, depth);
232+
res.shrink_to_fit();
231233
return res;
232234
}
233235

@@ -344,34 +346,23 @@ HugeInt operator*(HugeInt &n1, HugeInt &n2) {
344346
return temp;
345347
}
346348

347-
HugeInt operator%(HugeInt &n1, HugeInt &n2) {
348-
// максимально неоптимальная реализация
349-
// для оптимизации нужно сначала реализовать оператор деления
350-
// TODO: когда-нибудь исправить!!
349+
HugeInt operator%(HugeInt &n1, HugeInt &n2) {
350+
HugeInt cn1(n1), cn2(n2);
351351

352-
if (n1 < n2) return n1;
353-
HugeInt a(n1), b(n2), main_sum("1");
354-
if (b.negative) {
355-
b = -b;
356-
a = -a;
357-
}
358-
for (HugeInt i("0"); i <= a; i = i + b) {
359-
main_sum++;
360-
}
361-
HugeInt last_sum = b * main_sum;
362-
if (!a.negative) {
363-
return a - last_sum;
364-
} else {
365-
return a + last_sum;
352+
while (cn2 < cn1) {
353+
cn1 = cn1.dif(cn2);
354+
cn1.shrink_to_fit();
366355
}
356+
cn1.shrink_to_fit();
357+
return cn1;
367358
}
368359

369360
void HugeInt::shrink_to_fit() {
370361
for (int i = 39; i > -1; i--) {
371362
if (number[i] != 0) {
372363
depth = i + 1;
373364
break;
374-
}
365+
}
375366
}
376367
if (depth == 0) depth++;
377368
}

Variants/Lab_12/var_6/HugeInt.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ class HugeInt {
4242

4343
HugeInt operator-() const;
4444

45-
friend bool operator== (HugeInt &n1, HugeInt &n2);
46-
friend bool operator!= (HugeInt &n1, HugeInt &n2);
45+
friend bool operator==(HugeInt &n1, HugeInt &n2);
46+
friend bool operator!=(HugeInt &n1, HugeInt &n2);
4747

48-
friend bool operator> (HugeInt &d1, HugeInt &d2);
49-
friend bool operator<= (HugeInt &d1, HugeInt &d2);
48+
friend bool operator>(HugeInt &d1, HugeInt &d2);
49+
friend bool operator<=(HugeInt &d1, HugeInt &d2);
5050

51-
friend bool operator< (HugeInt &n1, HugeInt &n2);
52-
friend bool operator>= (HugeInt &n1, HugeInt &n2);
51+
friend bool operator<(HugeInt &n1, HugeInt &n2);
52+
friend bool operator>=(HugeInt &n1, HugeInt &n2);
5353

5454
HugeInt& operator++(); // версия префикс
5555
HugeInt& operator--(); // версия префикс
@@ -60,6 +60,7 @@ class HugeInt {
6060
friend HugeInt operator+(HugeInt &n1, HugeInt &n2);
6161
friend HugeInt operator-(HugeInt &n1, HugeInt &n2);
6262
friend HugeInt operator*(HugeInt &n1, HugeInt &n2);
63+
friend HugeInt operator/(HugeInt &n1, HugeInt &n2);
6364
friend HugeInt operator%(HugeInt &n1, HugeInt &n2);
6465

6566
friend std::ostream& operator<<(std::ostream &out, const HugeInt &n);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@
2323
using namespace std;
2424

2525
int main() {
26+
HugeInt a, b, c, d;
27+
a.set("618"); b.set("42"); c.set("123"); d.set("98765");
28+
unsigned int at(618), bt(42), ct(123), dt(98765);
29+
30+
cout << a << "\t% " << b << "\t= " << (a % b) << "\t(" << (at % bt) << ")" << endl;
31+
cout << a << "\t% " << c << "\t= " << (a % c) << "\t(" << (at % ct) << ")" << endl;
32+
cout << a << "\t% " << d << "\t= " << (a % d) << "\t(" << (at % dt) << ")" << endl;
33+
cout << b << "\t% " << a << "\t= " << (b % a) << "\t(" << (bt % at) << ")" << endl;
34+
cout << b << "\t% " << c << "\t= " << (b % c) << "\t(" << (bt % ct) << ")" << endl;
35+
cout << b << "\t% " << d << "\t= " << (b % d) << "\t(" << (bt % dt) << ")" << endl;
36+
cout << c << "\t% " << a << "\t= " << (c % a) << "\t(" << (ct % at) << ")" << endl;
37+
cout << c << "\t% " << b << "\t= " << (c % b) << "\t(" << (ct % bt) << ")" << endl;
38+
cout << c << "\t% " << d << "\t= " << (c % d) << "\t(" << (ct % dt) << ")" << endl;
39+
cout << d << "\t% " << a << "\t= " << (d % a) << "\t(" << (dt % at) << ")" << endl;
40+
cout << d << "\t% " << b << "\t= " << (d % b) << "\t(" << (dt % bt) << ")" << endl;
41+
cout << d << "\t% " << c << "\t= " << (d % c) << "\t(" << (dt % ct) << ")" << endl;
42+
}
43+
44+
int main_test() {
2645
setlocale(LC_ALL, "Russian");
2746

2847
HugeInt a, b("7"), s;

0 commit comments

Comments
 (0)