uva 424 - Integer Inquiry

大数相加,直接套模板。。蛋疼,还打算做题的,刚做完这题,uva就访问不到- -。

#include <iostream> #include <cstring> #include <cmath> using namespace std; const int MAX = 30010; class HP { public : int len, s[MAX]; HP() { *this = 0; } HP(int inte) { *this = inte; }; HP( const char* str) { *this = str; }; //注意头文件,iostream friend ostream& operator<<(ostream &cout , const HP &x ) ; HP operator=(int inte); HP operator=(const char *str); HP operator+(const HP &b); HP operator*(const HP &b); HP operator-(const HP &b); HP operator/(const HP &b); HP operator%(const HP &b); int compare(const HP &b) ; }; ostream& operator<<(ostream& cout, const HP& x) { for(int i = x.len - 1; i >= 0; i--) cout << x.s[i]; return cout; } HP HP::operator=(int inte) { //注意头文件, cstring memset(s, 0, sizeof(s)); if(inte == 0) { len = 1; s[0] = 0; return *this; } len = 0; while(inte != 0) { s[len++] = inte % 10; inte /= 10; } return *this; } HP HP::operator=(const char *str) { //注意头文件, cstring len = strlen(str); memset(s, 0, sizeof(s)); for(int i = 0; i < len; i++) { s[i] = str[len - i - 1] - '0'; } return *this; } HP HP::operator+(const HP& b) { int carry = 0; HP c; for(c.len = 0; c.len < this->len || c.len < b.len || carry != 0; c.len++) { c.s[c.len] = carry; if(c.len < this->len) c.s[c.len] += this->s[c.len]; if(c.len < b.len) c.s[c.len] += b.s[c.len]; carry = c.s[c.len] / 10; c.s[c.len] %= 10; } return c; } HP HP::operator-(const HP& b) { HP c; int borrow = 0; for(int i = 0; i < this->len; i++) { c.s[i] = this->s[i] - borrow; if(i < b.len) c.s[i] -= b.s[i]; if(c.s[i] < 0) { borrow = 1; c.s[i] += 10; }else borrow = 0; } c.len = this->len; while(c.len != 0 && c.s[c.len - 1] == 0) c.len--; if(c.len == 0) { c.len = 1; c.s[0] = 0; } return c; } HP HP::operator*(const HP& b) { HP c; c.len = this->len + b.len; int i, j; for(i = 0; i < this->len; i++) { for(j = 0; j < b.len; j++) { c.s[i + j] += this->s[i] * b.s[j]; } } for(i = 0; i < c.len - 1; i++) { c.s[i + 1] += c.s[i] / 10; c.s[i] %= 10; } while(c.len != 0 && c.s[c.len] == 0) c.len--; c.len++; return c; } HP HP::operator/(const HP& b) { HP c, d(0); int i, j; for(i = this->len - 1; i >= 0; i--) { if(!(d.len == 1 && d.s[0] == 0)) { for(j = d.len; j > 0; j--) d.s[j] = d.s[j - 1]; d.len++; } d.s[0] = this->s[i]; c.s[i] = 0; while((j = d.compare(b)) >= 0) { d = d - b; c.s[i]++; if(j == 0) break; } } c.len = this->len; i = c.len - 1; while(i != 0 && c.s[i] == 0) { c.len--; i--; } return c; } HP HP::operator%(const HP& b) { HP c(0); int i, j; for(i = this->len - 1; i >= 0; i--) { if(!(c.len == 1 && c.s[0] == 0)) { for(j = c.len; j > 0; j--) c.s[j] = c.s[j - 1]; c.len++; } c.s[0] = this->s[i]; while((j = c.compare(b)) >= 0) { c = c - b; if(j == 0) break; } } return c; } int HP::compare(const HP &b) { if(this->len > b.len) return 1; if(this->len < b.len) return -1; for(int i = b.len - 1; i >= 0; i--) { if(this->s[i] > b.s[i]) return 1; if(this->s[i] < b.s[i]) return -1; } return 0; } int main() { char s[100]; HP hp(0); while(cin >> s && s[0] != '0') { hp = hp + HP(s); } cout << hp << endl; return 0; } 

你可能感兴趣的:(c,Integer,Class,HP)