@@ -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
89HugeInt::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+
1622HugeInt::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
5057void 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
5664string 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+
167197HugeInt 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+
317371void 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
326381void HugeInt::normalize () {
0 commit comments