Программа считает количество делителей введенного числа. Но при вводе зависает, а после кидает исключения Stack around the variable 'res' was corrupted #include <iostream> #include <string> #include <algorithm> const int max_len = 10000; int osn = 10; using namespace std; struct BigInt { int amount; //количество цифр в числе int digits[max_len]; //массив цифр в обратном порядке void input() { memset(digits, 0, sizeof(digits)); string str; cin >> str; int pos = 0; for (int i = str.size() - 1;i >= 0;i--) digits[pos++] = str[i] - '0'; amount = str.size(); } void Output() { for (int i = amount - 1;i >= 0;i--) cout << digits[i]; } }; BigInt operator - (const BigInt &a, const BigInt &b) { BigInt res = a; int r = 0; for (int i = 0;i<res.amount;i++) { res.digits[i] -= b.digits[i] + r; if (res.digits[i]<0) { res.digits[i] += osn; res.digits[i + 1]--; } } int pos = res.amount; while (pos && !res.digits[pos]) pos--; res.amount = pos + 1; return res; } BigInt operator / (const BigInt &a, const int &n) { BigInt res; res.amount = a.amount; int ost = 0; for (int i = res.amount - 1;i >= 0;i--) { int cur = ost * osn + a.digits[i]; res.digits[i] = cur / n; ost = cur % n; } if (!res.digits[res.amount - 1] && res.amount != 1) res.amount--; return res; } bool operator != (const BigInt &delitel, const int zero) { for (int i = 0; i < delitel.amount; i++) { if (delitel.digits[i] != zero)return true; } return false; } bool operator > (const BigInt & testDelimoe, const int zero) { for (int i = 0; i < testDelimoe.amount;i++) if (testDelimoe.digits[i] > 0)return true; return false; } bool checkDelitel(BigInt testDelimoe) { bool flag = false; for (int i = 0; i < testDelimoe.amount; i++) { if (testDelimoe.digits[i] != 0)flag = true; } return flag; } int main() { BigInt delimoe; BigInt delitel; delimoe.input(); delitel = delimoe / 2; int coldelitel = 0; int zero = 0; while (delitel / 2 != zero) { BigInt testDelimoe = delimoe; int arr[max_len]; for (int i = 0; i < testDelimoe.amount; i++) { arr[i] = testDelimoe.digits[i]; } while (testDelimoe > zero) { testDelimoe = testDelimoe - delitel; if (checkDelitel(testDelimoe))coldelitel++; } int pos = 0; delitel.digits[0] -= 1; while (delitel.digits[pos] < 0) { delitel.digits[pos + 1]--; delitel.digits[pos++] += 10; } } } |
max_len * sizeof(int)). If the problem persists, look for where you go beyond the bounds of the arrays. - PinkTuxint *digits = new int[max_length](and do not forget to release in the destructor, plus add a copy constructor). Well, after that - in the debugger, and walk at least on small values. - PinkTux