标签:
今天无意间看到一个很好的大数模板,能算加、减、乘、除等基本运算,但操作减法的时候只能大数减小数,也不支持负数,如果是两个负数的话去掉符号相加之后再取反就可以了,一正一负比较绝对值大小,然后相减。我借用了一下:(作过少许代码上的精简)
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<algorithm> 5 #include<iostream> 6 using namespace std; 7 8 const int maxn = 2003; 9 10 struct bign{ 11 int len, s[maxn]; 12 bign(){ 13 memset(s,0,sizeof(s)); 14 len= 1; 15 } 16 bign(int num) { *this = num; } 17 bign(const char *num) { *this = num; } 18 bign operator = (const int num){ 19 char s[maxn]; 20 sprintf(s,"%d",num); 21 return *this = s; 22 } 23 bign operator = (const char *num){ 24 for(int i = 0; num[i] ==‘0‘; ++num) ; 25 len = strlen(num); 26 for(int i = 0; i < len; ++i) 27 s[i] = num[len-1-i] -‘0‘; 28 return *this; 29 } 30 bign operator + (const bign &b) const { 31 bign c; 32 c.len = 0; 33 int maxlen = max(len, b.len); 34 for(int i = 0, g = 0; g || i < maxlen; ++i){ // g是进位 35 int x = g; 36 if(i < len) x += s[i]; 37 if(i < b.len) x += b.s[i]; 38 c.s[c.len++] = x % 10; 39 g = x / 10; 40 } 41 return c; 42 } 43 bign operator += (const bign &b){ 44 return *this = *this + b; 45 } 46 void clean(){ 47 while(len >1 && !s[len-1]) --len; 48 } 49 bign operator * (const bign &b) const { 50 bign c; 51 c.len = len + b.len; 52 53 for(int i = 0; i < len; ++i) 54 for(int j = 0; j < b.len; ++j) 55 c.s[i+j] += s[i] * b.s[j]; 56 57 for(int i = 0; i < c.len; ++i){ 58 c.s[i+1] += c.s[i]/10; 59 c.s[i] %= 10; 60 } 61 c.clean(); 62 return c; 63 } 64 bign operator *= (const bign &b){ 65 return *this = *this * b; 66 } 67 bign operator -(const bign &b) const { 68 bign c; 69 c.len = 0; 70 for(int i = 0, g = 0; i < len; ++i){ //此时的 g是退位 71 int x = s[i]- g; 72 if(i < b.len) x -= b.s[i]; 73 if(x >= 0) g = 0; 74 else { 75 g = 1; 76 x += 10; 77 } 78 c.s[c.len++] = x; 79 } 80 c.clean(); 81 return c; 82 } 83 bign operator -= (const bign &b){ 84 return *this = *this - b; 85 } 86 bign operator / (const bign &b) const { 87 bign c, f = 0; 88 for(int i = len-1; i >= 0; --i){ 89 f *= 10; 90 f.s[0] = s[i]; 91 while(f >= b){ 92 f -= b; 93 ++c.s[i]; 94 } 95 } 96 c.len = len; 97 c.clean(); 98 return c; 99 } 100 bign operator /= (const bign &b){ 101 return *this = *this / b; 102 } 103 bign operator % (const bign &b) const { 104 bign r = *this / b; 105 return *this- r*b; 106 } 107 bign operator %= (const bign &b){ 108 return *this = *this % b; 109 } 110 bool operator < (const bign &b) const { 111 if(len != b.len) return len < b.len; 112 for(int i= len-1; i>=0; --i) 113 if(s[i] != b.s[i]) return s[i] < b.s[i]; 114 return 0; 115 } 116 bool operator > (const bign &b) const { 117 if(len != b.len) return len > b.len; 118 for(int i = len-1; i >= 0; --i) 119 if(s[i] != b.s[i]) return s[i] > b.s[i]; 120 return 0; 121 } 122 bool operator == (const bign &b) const { 123 return !(*this < b || *this > b); 124 } 125 bool operator != (const bign &b) const { 126 return !(*this == b); 127 } 128 bool operator <= (const bign &b) const { 129 return *this < b || *this == b; 130 } 131 bool operator >= (const bign &b) const { 132 return *this > b || *this == b; 133 } 134 string str() const { 135 string res = ""; 136 for(int i = 0; i < len; ++i) 137 res = char(s[i]+‘0‘) + res; //这里一定不能掉乱 +的顺序! 138 return res; 139 } 140 }; 141 142 istream& operator >> (istream &in, bign &x){ 143 string s; 144 in >> s; 145 x = s.c_str(); 146 return in; 147 } 148 149 ostream& operator << (ostream &out, const bign &x){ 150 out << x.str(); 151 return out; 152 } 153 154 int main() 155 { 156 bign a,b,c,d,e,f,g; 157 while(cin>>a>>b) 158 { 159 a.clean(); 160 b.clean(); 161 c = a+b; 162 d = a-b; 163 e = a*b; 164 f = a/b; 165 g = a%b; 166 cout<<"a+b = "<<c<<endl; 167 cout<<"a-b = "<<d<<endl; 168 cout<<"a*b = "<<e<<endl; 169 cout<<"a/b = "<<f<<endl; 170 cout<<"a%b = "<<g<<endl; 171 cout<<(a==b? "a = b":"a != b")<<endl; 172 } 173 return 0; 174 }
在此感谢原博客的模板提供: bign类C++高精度模板
标签:
原文地址:http://www.cnblogs.com/Newdawn/p/4248365.html