Цитата(feodorv @ 16.9.2013, 04:17 ) | Может, столбиком? smile
На самом деле, задача с большой бородой, один алгоритм приведён у Кнута и реализован в питоне (можно покопаться в его исходниках) и в библиотеках bigint и GMP, если я не ошибаюсь. Много ссылок на различные ресурсы есть здесь. |
Спасибо за ссылки! Класс BigUnsigned по последней ссылке пока мне непонятен (до шаблонов еще не дошел)!
Цитата(akizelokro @ 16.9.2013, 01:35 ) | Честно сказать, ни у кого этой книги нет. Приводи код, и не стесняйся, в чём проблемы. |
hugeint.h:
| Код | #ifndef HUGEINT1_H #define HUGEINT1_H #include <iostream> using namespace std; const int MAX_LENGTH_INT = 29; class HugeInt { friend ostream &operator<<(ostream &, HugeInt &); public: HugeInt(long = 0); HugeInt(const char *); HugeInt operator+(HugeInt &);
// код, добавленный мной
friend HugeInt operator+(HugeInt &, int); friend HugeInt operator+(HugeInt &, const char *); HugeInt operator+=(HugeInt &); HugeInt operator*(HugeInt &); friend HugeInt operator*(HugeInt &, int); bool operator==(HugeInt &); bool operator==(int); bool operator!=(int); HugeInt operator/(HugeInt &); private: short intval[MAX_LENGTH_INT + 1]; };
#endif
|
hugeint.cpp:
| Код | #define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <assert.h> #include "hugeint.h" using namespace std; HugeInt::HugeInt(long val) { for(int i = 0; i <= MAX_LENGTH_INT; i++) intval[i] = 0; for(int i = MAX_LENGTH_INT; val != 0 && i >= 0; i--) { intval[i] = val % 10; val /= 10; } } HugeInt::HugeInt(const char *string) { int i, j; for(i = 0; i <= MAX_LENGTH_INT; i++) intval[i] = 0; for(i = MAX_LENGTH_INT + 1 - strlen(string), j = 0; i <= MAX_LENGTH_INT; i++, j++) intval[i] = string[j] - '0'; } HugeInt HugeInt::operator+(HugeInt &op2) { HugeInt temp; int carry = 0; for(int i = MAX_LENGTH_INT; i >= 0; i--) { temp.intval[i] = intval[i] + op2.intval[i] + carry; if(temp.intval[i] > 9) { temp.intval[i] %= 10; carry = 1; } else carry = 0; } return temp; } ostream &operator<<(ostream &out, HugeInt &rhs) { int i; for(i = 0; (rhs.intval[i] == 0); i++); if(i == (MAX_LENGTH_INT + 1)) out << "0"; else for(; i <= MAX_LENGTH_INT; i++) out << rhs.intval[i]; return out; }
// код, добавленный мной
HugeInt operator+(HugeInt &op1, int num) { HugeInt temp, temp2(num); temp = op1 + temp2; return temp; } HugeInt operator+(HugeInt &op1, const char *string) { HugeInt temp, temp2(string); temp = op1 + temp2; return temp; }
HugeInt HugeInt::operator +=(HugeInt &op2) { *this = *this + op2; return *this; } HugeInt operator*(HugeInt &op1, int k) { HugeInt sum; for(int i = 0; i < k; i++) sum += op1; return sum; } HugeInt HugeInt::operator *(HugeInt &op2) { HugeInt temp, temp2; int carry = 0, n = 1, i; for(i = 0; (op2.intval[i] == 0); i++); if(i == (MAX_LENGTH_INT + 1)) return temp2; for(int t = MAX_LENGTH_INT; t >= i; t--) { // j - первый операнд for(int j = MAX_LENGTH_INT; j >= 0; j--) { temp.intval[j] = intval[j] * op2.intval[t] + carry; if(temp.intval[j] > 9) { carry = temp.intval[j] / 10; temp.intval[j] %= 10; } else carry = 0; } temp2 += temp * n; n *= 10; } return temp2; } bool HugeInt::operator ==(HugeInt &op2) { for(int i = MAX_LENGTH_INT; i >= 0; i--) if(intval[i] != op2.intval[i]) return false; return true; } bool HugeInt::operator ==(int val) { for(int i = MAX_LENGTH_INT; i >= 0; i--) if(intval[i] != val) return false; return true; } bool HugeInt::operator !=(int val) { for(int i = MAX_LENGTH_INT; i >= 0; i--) if(intval[i] != val) return true; return false; } HugeInt HugeInt::operator /(HugeInt &op2) { assert(op2 != 0); HugeInt temp; for(int i = MAX_LENGTH_INT; i >= 0; i--) { if(op2.intval[i] == 0) continue; temp.intval[i] = 1 / op2.intval[i]; } cout << temp << endl; temp = (*this) * temp; return temp; }
|
driver.cpp:
| Код | #define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <Windows.h> #include "hugeint.h" using namespace std; int main() { setlocale(LC_ALL, ""); SetConsoleCP( 1251 ); SetConsoleOutputCP( 1251 ); HugeInt n1(200), n2(20), n3("99999999999999999999999999999"), n4("1"); HugeInt n5; cout << "n5 = " << n5 << endl; cout << "n1 = " << n1 << endl << "n2 = " << n2 << endl << "n3 = " << n3 << endl << "n4 = " << n4 << endl; n5 = n1 + n2; cout << n1 << " + " << n2 << " = " << n5 << endl; cout << n3 << " + " << n4 << "\n= " << (n3 + n4) << endl; n5 = n1 + 9; cout << n1 << " + " << 9 << " = " << n5 << endl; n5 = n2 + "10000"; cout << n2 << " + " << 10000 << " = " << n5 << endl; n5 = n1 * n2; cout << n1 << " * " << n2 << " = " << n5 << endl; if(n1 == n2) cout << "Числа равны\n"; else cout << "Числа не равны\n"; return 0; }
|
|