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"); } 
  • The debugger is your friend. How to guess what and how - look in the debugger. - Vladimir Martyanov
  • @ Vladimir Martiyanov is just the thing that has already passed the debugger. I know where the mistake is. but I do not know why - IWProgrammer
  • And where is the error? Specify a specific string. - Vladimir Martyanov
  • @ Vladimir Martianov if (digits [amount]) amount ++; // 35 - 40 lines - IWProgrammer
  • one
    Of course, I am showered with pissing rags, but: signed for an index, which in principle cannot be negative - IMHO is not the best idea. Further: look towards the amount initialization, judging by 0xCCCCC ... it is not initialized. Then look at the call stack from where it came out. - Vladimir Martyanov

0