bign(高精度)大整数输入输出及运算
Posted ogreee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bign(高精度)大整数输入输出及运算相关的知识,希望对你有一定的参考价值。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<vector> 7 #include<queue> 8 #include<stack> 9 #include<map> 10 #include<set> 11 #include<cmath> 12 using namespace std; 13 #define ll long long 14 #define ull unsigned long long 15 #define ldb long double 16 #define db double 17 #define fi first 18 #define se second 19 #define INF 0x3f3f3f3f 20 #define endl "\n" 21 #define rush() int T;cin>>T;while(T--) 22 #define mem(a,b) memset((a),(b),sizeof(a)) 23 const db pi = acos((db)-1); 24 const ll MAXN = 5005; 25 const ll mod = 1e9+7; 26 27 struct bign 28 { 29 int len, s[MAXN]; 30 bign () //初始化 31 { 32 memset(s, 0, sizeof(s)); 33 len = 1; 34 } 35 bign (int num) { *this = num; } 36 bign (const char *num) { *this = num; } //让this指针指向当前字符串 37 bign operator = (const int num) 38 { 39 char s[MAXN]; 40 sprintf(s, "%d", num); //sprintf函数将整型映到字符串中 41 *this = s; 42 return *this; //再将字符串转到下面字符串转化的函数中 43 } 44 bign operator = (const char *num) 45 { 46 for(int i = 0; num[i] == ‘0‘; num++) ; //去前导0 47 len = strlen(num); 48 for(int i = 0; i < len; i++) s[i] = num[len-i-1] - ‘0‘; //反着存 49 return *this; 50 } 51 bign operator + (const bign &b) const //对应位相加,最为简单 52 { 53 bign c; 54 c.len = 0; 55 for(int i = 0, g = 0; g || i < max(len, b.len); i++) 56 { 57 int x = g; 58 if(i < len) x += s[i]; 59 if(i < b.len) x += b.s[i]; 60 c.s[c.len++] = x % 10; //关于加法进位 61 g = x / 10; 62 } 63 return c; 64 } 65 bign operator += (const bign &b) //如上文所说,此类运算符皆如此重载 66 { 67 *this = *this + b; 68 return *this; 69 } 70 void clean() //由于接下来的运算不能确定结果的长度,先大而估之然后再查 71 { 72 while(len > 1 && !s[len-1]) len--; //首位部分‘0’故删除该部分长度 73 } 74 bign operator * (const bign &b) //乘法重载在于列竖式,再将竖式中的数转为抽象,即可看出运算法则。 75 { 76 bign c; 77 c.len = len + b.len; 78 for(int i = 0; i < len; i++) 79 { 80 for(int j = 0; j < b.len; j++) 81 { 82 c.s[i+j] += s[i] * b.s[j];//不妨列个竖式看一看 83 } 84 } 85 for(int i = 0; i < c.len; i++) //关于进位,与加法意同 86 { 87 c.s[i+1] += c.s[i]/10; 88 c.s[i] %= 10; 89 } 90 c.clean(); //我们估的位数是a+b的长度和,但可能比它小(1*1 = 1) 91 return c; 92 } 93 bign operator *= (const bign &b) 94 { 95 *this = *this * b; 96 return *this; 97 } 98 bign operator - (const bign &b) //对应位相减,加法的进位改为借1 99 { //不考虑负数 100 bign c; 101 c.len = 0; 102 for(int i = 0, g = 0; i < len; i++) 103 { 104 int x = s[i] - g; 105 if(i < b.len) x -= b.s[i]; //可能长度不等 106 if(x >= 0) g = 0; //是否向上移位借1 107 else 108 { 109 g = 1; 110 x += 10; 111 } 112 c.s[c.len++] = x; 113 } 114 c.clean(); 115 return c; 116 } 117 bign operator -= (const bign &b) 118 { 119 *this = *this - b; 120 return *this; 121 } 122 bign operator / (const bign &b) //运用除是减的本质,不停地减,直到小于被减数 123 { 124 bign c, f = 0; //可能会在使用减法时出现高精度运算 125 for(int i = len-1; i >= 0; i--) //正常顺序,从最高位开始 126 { 127 f = f*10; //上面位的剩余到下一位*10 128 f.s[0] = s[i]; //加上当前位 129 while(f >= b) 130 { 131 f -= b; 132 c.s[i]++; 133 } 134 } 135 c.len = len; //估最长位 136 c.clean(); 137 return c; 138 } 139 bign operator /= (const bign &b) 140 { 141 *this = *this / b; 142 return *this; 143 } 144 bign operator % (const bign &b) //取模就是除完剩下的 145 { 146 bign r = *this / b; 147 r = *this - r*b; 148 r.clean(); 149 return r; 150 } 151 bign operator %= (const bign &b) 152 { 153 *this = *this % b; 154 return *this; 155 } 156 bool operator < (const bign &b) //字符串比较原理 157 { 158 if(len != b.len) return len < b.len; 159 for(int i = len-1; i != -1; i--) 160 { 161 if(s[i] != b.s[i]) return s[i] < b.s[i]; 162 } 163 return false; 164 } 165 bool operator > (const bign &b) //同理 166 { 167 if(len != b.len) return len > b.len; 168 for(int i = len-1; i != -1; i--) 169 { 170 if(s[i] != b.s[i]) return s[i] > b.s[i]; 171 } 172 return false; 173 } 174 bool operator == (const bign &b) 175 { 176 return !(*this > b) && !(*this < b); 177 } 178 bool operator != (const bign &b) 179 { 180 return !(*this == b); 181 } 182 bool operator <= (const bign &b) 183 { 184 return *this < b || *this == b; 185 } 186 bool operator >= (const bign &b) 187 { 188 return *this > b || *this == b; 189 } 190 string str() const //将结果转化为字符串(用于输出) 191 { 192 string res = ""; 193 for(int i = 0; i < len; i++) res = char(s[i]+‘0‘)+res; 194 return res; 195 } 196 }; 197 198 istream& operator >> (istream &in, bign &x) //重载输入流 199 { 200 string s; 201 in >> s; 202 x = s.c_str(); //string转化为char[] 203 return in; 204 } 205 206 ostream& operator << (ostream &out, const bign &x) //重载输出流 207 { 208 out << x.str(); 209 return out; 210 } 211 212 int main() 213 { 214 bign a,b; 215 216 cin>>a>>b; 217 cout<<a*b<<endl; 218 219 return 0; 220 }
以上是关于bign(高精度)大整数输入输出及运算的主要内容,如果未能解决你的问题,请参考以下文章