[MATH]Big Integer + - *

mac2022-06-30  147

#include <iostream> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <string> using std::string; using std::ostream; using std::cout; using std::cin; using std::endl; const int MAX = 220; //#define online class CHugeInt { private: bool positive; char s[MAX]; /** /* 计算大小关系,使得交换顺序来便于计算 ****/ int Compare(const CHugeInt & c) const// < -1, == 0, > 1 { int L1 = strlen(s); int L2 = strlen(c.s); if(L1 < L2) return -1; if(L1 > L2) return 1; // L1 == L2 for(int i = 0; i < L1; i++) { if(s[i] < c.s[i]) return -1; if(s[i] > c.s[i]) return 1; } return 0; } /*** /* 私有成员函数,专门计算 a + b,且满足a > b; ***/ CHugeInt Add(const CHugeInt &a, const CHugeInt & b) const { CHugeInt tp(a); tp.positive = true; int L1 = strlen(tp.s); int L2 = strlen(b.s); int Forward = 0; for(int i = L1-1, j = L2 - 1; i>=0; --i, --j) { int _n1 = tp.s[i] - '0'; int _n2 = 0; if(j >= 0) _n2 = b.s[j] - '0'; int n = _n1 + _n2 + Forward; Forward = 0; if(n>=10) { Forward = 1; n -= 10; } tp.s[i] = n + '0'; } if(Forward == 1) { for(int i = L1; i >=0 ; --i) tp.s[i+1] = tp.s[i]; tp.s[0] = '1'; } return tp; } /*** /*专门计算 a - b的私有成员函数,且满足a >= b; 计算结果符号始终为正 ****/ CHugeInt Sub(const CHugeInt & a, const CHugeInt & b) const { CHugeInt tp(a); int L1 = strlen(tp.s); int L2 = strlen(b.s); int Forward = 0; for(int i = L1 - 1, j = L2 - 1; i>=0; i--,j--) { int _n1 = tp.s[i] - '0'; int _n2 = 0; if(j >= 0) _n2 = b.s[j] - '0'; int n = _n1 - _n2 - Forward; Forward = 0; if(n < 0) { Forward = 1; n += 10; } tp.s[i] = n + '0'; } /** 去掉大整数前面的首0,通过搬运数字 **/ if(tp.s[0] != '0') return tp; int non_zero_index = -1;// ȫΪ0 for(int i = 0; i < L1; i++) { if(tp.s[i] != '0') { non_zero_index = i; break; } } if(non_zero_index == -1) { tp.s[1] = '\0'; } else { for(int i = 0; non_zero_index <= L1 ; i++) tp.s[i] = tp.s[non_zero_index++]; } return tp; } public: CHugeInt(){}; CHugeInt(int); CHugeInt(char *_s); CHugeInt(const CHugeInt&); CHugeInt operator+(const CHugeInt &) const; CHugeInt operator-(const CHugeInt &) const; CHugeInt operator*(const CHugeInt &) const; CHugeInt operator/(const CHugeInt &) const; CHugeInt & operator<<(int); friend CHugeInt operator+(const int x, CHugeInt & c); friend ostream & operator<<(ostream & os, const CHugeInt & c); const CHugeInt operator+=(const CHugeInt &x); const CHugeInt operator++(); const CHugeInt operator++(int); bool Positive(){return positive;} }; CHugeInt CHugeInt::operator/(const CHugeInt & x) const { char _s[MAX]; char ans[MAX]; memset(_s, 0, sizeof(_s)); memset(ans, 0, sizeof(ans)); int L1 = strlen(s); int L2 = strlen(x.s); if(this->Compare(x) == 0) return CHugeInt(1); if(this->Compare(x) == -1) return CHugeInt(0); int cnt = 0; for(int i = 0; i < L1; i++) { if(_s[0] == '0') _s[0] = s[i]; else _s[cnt++] = s[i]; _s[cnt] = '\0'; CHugeInt tp(_s); // cout << tp <<"here" << _s << endl; if(tp.Compare(x) == -1) { ans[i] = '0'; continue; } CHugeInt _x = x; _x.positive = true; int ans_num = 1; for(ans_num = 2; ans_num <= 10; ans_num++) { _x += x; if(tp.Compare(_x) == -1) break; } ans[i] = ans_num - 1 + '0'; tp = tp - (_x - x); cnt = strlen(tp.s); strcpy(_s, tp.s); // cout<<"_s " << _s << endl; } if(ans[0] != '0') return CHugeInt(ans); else { int non_zero_index = -1; for(int i = 0; i < L1; i++) { if(ans[i] != '0') { non_zero_index = i; break; } } if(non_zero_index == -1) return CHugeInt(0); for(int i = 0; i + non_zero_index <= L1;i++) { ans[i] = ans[i+non_zero_index]; } return CHugeInt(ans); } } CHugeInt & CHugeInt::operator<<(int x) { if(s[0] == '0') return *this; int L = strlen(this->s); for(int i = 0; i < x; i++) this->s[i+L] = '0'; this->s[x+L] = '\0'; return *this; } CHugeInt CHugeInt::operator*(const CHugeInt & x) const { //only process positive number CHugeInt tp(0); int L1 = strlen(s); int L2 = strlen(x.s); for(int i = L2 - 1; i >=0; i--) { char tp_s[MAX] = ""; int shift = L2 - 1 - i; int multipy_2 = x.s[i] - '0'; int Forward = 0; for(int j = L1 - 1; j >= 0; j--) { int multipy_1 = s[j] - '0'; int n = multipy_1 * multipy_2 + Forward; Forward = 0; if(n >= 10) { while(n>=10) { n -= 10; Forward++; } } tp_s[j] = n + '0'; } if(Forward >= 1) { for(int j = L1; j>=0; j--) tp_s[j+1] = tp_s[j]; tp_s[0] = Forward + '0'; Forward = 0; } tp += (CHugeInt(tp_s)<<shift); } return tp; } CHugeInt:: CHugeInt(const CHugeInt & x) { positive = x.positive; strcpy(s, x.s); // cout << tp.s <<"Copy con" << x.s <<endl; } CHugeInt CHugeInt::operator-(const CHugeInt &x) const { CHugeInt ans; int result_com = this->Compare(x); if(this->positive and x.positive) { if(result_com < 0) { ans = Sub(x, *this); ans.positive = false; } else ans = Sub(*this, x);//big+little return ans; } if(this->positive and !x.positive) { CHugeInt tp(x); tp.positive = true; return tp + *this; } if(!this->positive and x.positive) { CHugeInt tp1(*this); tp1.positive = true; CHugeInt tp(tp1 + x); tp.positive = false; return tp; } if(!this->positive and !x.positive) { CHugeInt tp1(*this); CHugeInt tp2(x); tp1.positive = true; tp2.positive = true; CHugeInt tp = tp2 - tp1; return tp; } } const CHugeInt CHugeInt:: operator++(int n) { CHugeInt tp(*this); *this = (*this) + 1; return tp; } const CHugeInt CHugeInt:: operator++() { *this = (*this) + 1; return *this; } const CHugeInt CHugeInt:: operator+=(const CHugeInt &x) { *this = (*this) + x; return *this; } ostream & operator<< (ostream & os, const CHugeInt & c) { if(!c.positive) os << "-"; os << c.s; return os; } CHugeInt operator+(const int x, CHugeInt & c) { return c + x; } CHugeInt CHugeInt:: operator+(const CHugeInt & x) const { int result_com = this->Compare(x); if(this->positive and x.positive) { if(result_com <= 0) return Add(x, *this); else return Add(*this, x);//big+little } if(this->positive and !x.positive) { CHugeInt tp(x); tp.positive = true; return *this - tp; } if(!this->positive and x.positive) { CHugeInt tp(*this); tp.positive = true; return x - tp; } if(!this->positive and !x.positive) { CHugeInt tp1(*this); CHugeInt tp2(x); tp1.positive = true; tp2.positive = true; CHugeInt tp = tp1 + tp2; tp.positive = false; return tp; } } CHugeInt operator+( int x, const CHugeInt & c) { return c + x; } CHugeInt:: CHugeInt(char *_s) { positive = true; if(_s[0] == '-') { positive = false; strcpy(s, _s + 1); //cout << "LOOK here" << s << endl; } else strcpy(s, _s); } CHugeInt:: CHugeInt(int x) { positive = true; if(x < 0) { positive = false; x = -x; } sprintf(s, "%d", x); } int main() { // #define gg #ifdef gg freopen("a.in", "r", stdin); freopen("a.out", "w", stdout); #endif // gg char s[MAX]; char c[2]; #ifdef online int T = 0; #endif // online if(cin >> s) { CHugeInt a(s); cin >> c; cin >> s; CHugeInt b(s); #ifdef online cout << T++ << " "; #endif // online if(c[0] == '+') cout << a + b << endl; if(c[0] == '-') cout << a - b << endl; if(c[0] == '*') cout << a * b << endl; if(c[0] == '/') cout << a / b << endl; } }

并不太完美,+, - 等操作适用于正负整数。*,/适用于正整数。其实稍微修改一下,乘除重载也适用于负整数

转载于:https://www.cnblogs.com/Airplus/p/5830254.html

最新回复(0)