Recently I asked for help here in writing a program that would get the remainder from dividing two long numbers. I wrote a program, but here's the problem, when it comes to a shift to the right (division is implemented by a bar), it causes an exception "0x00052DEB". Googled, it is said that it is impossible to refer to the array in the same way, why I did not understand. An exception occurs when it comes to the SdvigRIght () function (described at the beginning of the program in the BigInt structure. Help, please, have the final touches, and what is the exception and how to fix it, there are no ideas at all. Can I somehow implement without exception handling? ?
The algorithm for taking the remainder was taken from the site http://cppalgo.blogspot.ru/2010/05/blog-post.html
#include "stdafx.h" #include <string> #include <iostream> using namespace std; const int max_len = 10000; //ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΈΡΡ Π² ΡΠΈΡΠ»Π΅ int osn = 10; //ΠΎΡΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠΈΡΡΠ΅ΠΌΡ ΡΡΠΈΡΠ»Π΅Π½ΠΈΡ struct BigInt { int amount; //ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΈΡΡ Π² ΡΠΈΡΠ»Π΅ int digits[max_len]; //ΠΌΠ°ΡΡΠΈΠ² ΡΠΈΡΡ Π² ΠΎΠ±ΡΠ°ΡΠ½ΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ void Input() { memset(digits, 0, sizeof(digits)); //Π·Π°ΠΏΠΎΠ»Π½ΡΠ΅ΠΌ Π½ΡΠ»ΡΠΌΠΈ digits string mainNumber; cin >> mainNumber; int index = 0; for (int i = mainNumber.size() - 1; i >= 0; i--) { digits[index++] = mainNumber[i] - '0'; } amount = mainNumber.size(); } void output() { for (int i = amount - 1;i >= 0;i--) cout << digits[i]; } void SdvigRight() { for (int i = amount;i >= 1;i--) digits[i] = digits[i - 1]; if (digits[amount]) amount++; } }; BigInt operator - (const BigInt &delitel, const int &n) { BigInt res = delitel; int index = 0; res.digits[0] -= n; while (res.digits[index] < 0) { res.digits[index + 1]--; res.digits[index++] += osn; } if (res.amount && !res.digits[res.amount - 1]) res.amount--; return res; } BigInt operator / (const BigInt &mainNumber, const int rezhuDelitel) { BigInt res; res.amount = mainNumber.amount; int ost = 0; for (int i = res.amount - 1;i >= 0;i--) { int cur = ost * osn + mainNumber.digits[i]; res.digits[i] = cur / rezhuDelitel; ost = cur % rezhuDelitel; } if (!res.digits[res.amount - 1] && res.amount != 1) res.amount--; return res; } bool operator <= (const BigInt &cur, const BigInt &curValue) { if (cur.amount != curValue.amount) return cur.amount<curValue.amount; for (int i = cur.amount - 1;i >= 0;i--) { if (cur.digits[i] != curValue.digits[i]) return cur.digits[i] < curValue.digits[i]; } return true; } BigInt operator * (const BigInt &delitel, const int &x) { BigInt res; res.amount = delitel.amount; int r = 0; for (int i = 0;i<res.amount | r;i++) { res.digits[i] = delitel.digits[i] * x + r; r = res.digits[i] / osn; res.digits[i] -= r*osn; } if (res.digits[res.amount]) res.amount++; return res; } BigInt operator - (const BigInt &curValue, const BigInt &delitel) { BigInt res = curValue; int r = 0; for (int i = 0;i<res.amount;i++) { res.digits[i] -= delitel.digits[i] + r; if (res.digits[i]<0) { res.digits[i] += osn; r = 1; } else r = 0; } if (!res.digits[res.amount - 1] && res.amount != 1) res.amount--; return res; } BigInt operator % (const BigInt &mainNumber, const BigInt &delitel) { BigInt res; BigInt curValue; for (int i = mainNumber.amount - 1; i >= 0; i--) { curValue.SdvigRight(); // * osn curValue.digits[0] = mainNumber.digits[i]; // ΠΏΠΎΠ΄Π±ΠΈΡΠ°Π΅ΠΌ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ x, ΡΠ°ΠΊΠΎΠ΅ ΡΡΠΎ b * x <= curValue int x = 0; int l = 0, r = osn; while (l <= r) { int m = (l + r) >> 1; BigInt cur = delitel * m; if (cur <= curValue) { x = m; l = m + 1; } else r = m - 1; } res.digits[i] = x; curValue = curValue - delitel * x; } return curValue; } int main() { BigInt mainNumber; mainNumber.Input(); cout << endl; BigInt delitel = mainNumber - 1; delitel.output(); cout << endl; BigInt ostatok = mainNumber % delitel; ostatok.output(); system("pause"); }