首页 > 其他 > 详细

高精度模板

时间:2015-11-22 00:14:41      阅读:200      评论:0      收藏:0      [点我收藏+]
  1 //http://www.cnblogs.com/HarryGuo2012/p/4524041.html
  2 
  3 
  4 #include<string>
  5 #include<iostream>
  6 #include<iosfwd>
  7 #include<cmath>
  8 #include<cstring>
  9 #include<stdlib.h>
 10 #include<stdio.h>
 11 #include<cstring>
 12 #define MAX_L 2005 //最大长度,可以修改
 13 using namespace std;
 14 
 15 class bign
 16 {
 17 public:
 18     int len, s[MAX_L];//数的长度,记录数组
 19 //构造函数
 20     bign();
 21     bign(const char*);
 22     bign(int);
 23     bool sign;//符号 1正数 0负数
 24     string toStr() const;//转化为字符串,主要是便于输出
 25     friend istream& operator>>(istream &,bign &);//重载输入流
 26     friend ostream& operator<<(ostream &,bign &);//重载输出流
 27 //重载复制
 28     bign operator=(const char*);
 29     bign operator=(int);
 30     bign operator=(const string);
 31 //重载各种比较
 32     bool operator>(const bign &) const;
 33     bool operator>=(const bign &) const;
 34     bool operator<(const bign &) const;
 35     bool operator<=(const bign &) const;
 36     bool operator==(const bign &) const;
 37     bool operator!=(const bign &) const;
 38 //重载四则运算
 39     bign operator+(const bign &) const;
 40     bign operator++();
 41     bign operator++(int);
 42     bign operator+=(const bign&);
 43     bign operator-(const bign &) const;
 44     bign operator--();
 45     bign operator--(int);
 46     bign operator-=(const bign&);
 47     bign operator*(const bign &)const;
 48     bign operator*(const int num)const;
 49     bign operator*=(const bign&);
 50     bign operator/(const bign&)const;
 51     bign operator/=(const bign&);
 52 //四则运算的衍生运算
 53     bign operator%(const bign&)const;//取模(余数)
 54     bign factorial()const;//阶乘
 55     bign Sqrt()const;//整数开根(向下取整)
 56     bign pow(const bign&)const;//次方
 57 //一些乱乱的函数
 58     void clean();
 59     ~bign();
 60 };
 61 #define max(a,b) a>b ? a : b
 62 #define min(a,b) a<b ? a : b
 63 
 64 bign::bign()
 65 {
 66     memset(s, 0, sizeof(s));
 67     len = 1;
 68     sign = 1;
 69 }
 70 
 71 bign::bign(const char *num)
 72 {
 73     *this = num;
 74 }
 75 
 76 bign::bign(int num)
 77 {
 78     *this = num;
 79 }
 80 
 81 string bign::toStr() const
 82 {
 83     string res;
 84     res = "";
 85     for (int i = 0; i < len; i++)
 86         res = (char)(s[i] + 0) + res;
 87     if (res == "")
 88         res = "0";
 89     if (!sign&&res != "0")
 90         res = "-" + res;
 91     return res;
 92 }
 93 
 94 istream &operator>>(istream &in, bign &num)
 95 {
 96     string str;
 97     in>>str;
 98     num=str;
 99     return in;
100 }
101 
102 ostream &operator<<(ostream &out, bign &num)
103 {
104     out<<num.toStr();
105     return out;
106 }
107 
108 bign bign::operator=(const char *num)
109 {
110     memset(s, 0, sizeof(s));
111     char a[MAX_L] = "";
112     if (num[0] != -)
113         strcpy(a, num);
114     else
115         for (int i = 1; i < strlen(num); i++)
116             a[i - 1] = num[i];
117     sign = !(num[0] == -);
118     len = strlen(a);
119     for (int i = 0; i < strlen(a); i++)
120         s[i] = a[len - i - 1] - 48;
121     return *this;
122 }
123 
124 bign bign::operator=(int num)
125 {
126     char temp[MAX_L];
127     sprintf(temp, "%d", num);
128     *this = temp;
129     return *this;
130 }
131 
132 bign bign::operator=(const string num)
133 {
134     const char *tmp;
135     tmp = num.c_str();
136     *this = tmp;
137     return *this;
138 }
139 
140 bool bign::operator<(const bign &num) const
141 {
142     if (sign^num.sign)
143         return num.sign;
144     if (len != num.len)
145         return len < num.len;
146     for (int i = len - 1; i >= 0; i--)
147         if (s[i] != num.s[i])
148             return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));
149     return !sign;
150 }
151 
152 bool bign::operator>(const bign&num)const
153 {
154     return num < *this;
155 }
156 
157 bool bign::operator<=(const bign&num)const
158 {
159     return !(*this>num);
160 }
161 
162 bool bign::operator>=(const bign&num)const
163 {
164     return !(*this<num);
165 }
166 
167 bool bign::operator!=(const bign&num)const
168 {
169     return *this > num || *this < num;
170 }
171 
172 bool bign::operator==(const bign&num)const
173 {
174     return !(num != *this);
175 }
176 
177 bign bign::operator+(const bign &num) const
178 {
179     if (sign^num.sign)
180     {
181         bign tmp = sign ? num : *this;
182         tmp.sign = 1;
183         return sign ? *this - tmp : num - tmp;
184     }
185     bign result;
186     result.len = 0;
187     int temp = 0;
188     for (int i = 0; temp || i < (max(len, num.len)); i++)
189     {
190         int t = s[i] + num.s[i] + temp;
191         result.s[result.len++] = t % 10;
192         temp = t / 10;
193     }
194     result.sign = sign;
195     return result;
196 }
197 
198 bign bign::operator++()
199 {
200     *this = *this + 1;
201     return *this;
202 }
203 
204 bign bign::operator++(int)
205 {
206     bign old = *this;
207     ++(*this);
208     return old;
209 }
210 
211 bign bign::operator+=(const bign &num)
212 {
213     *this = *this + num;
214     return *this;
215 }
216 
217 bign bign::operator-(const bign &num) const
218 {
219     bign b=num,a=*this;
220     if (!num.sign && !sign)
221     {
222         b.sign=1;
223         a.sign=1;
224         return b-a;
225     }
226     if (!b.sign)
227     {
228         b.sign=1;
229         return a+b;
230     }
231     if (!a.sign)
232     {
233         a.sign=1;
234         b=bign(0)-(a+b);
235         return b;
236     }
237     if (a<b)
238     {
239         bign c=(b-a);
240         c.sign=false;
241         return c;
242     }
243     bign result;
244     result.len = 0;
245     for (int i = 0, g = 0; i < a.len; i++)
246     {
247         int x = a.s[i] - g;
248         if (i < b.len) x -= b.s[i];
249         if (x >= 0) g = 0;
250         else
251         {
252             g = 1;
253             x += 10;
254         }
255         result.s[result.len++] = x;
256     }
257     result.clean();
258     return result;
259 }
260 
261 bign bign::operator * (const bign &num)const
262 {
263     bign result;
264     result.len = len + num.len;
265 
266     for (int i = 0; i < len; i++)
267         for (int j = 0; j < num.len; j++)
268             result.s[i + j] += s[i] * num.s[j];
269 
270     for (int i = 0; i < result.len; i++)
271     {
272         result.s[i + 1] += result.s[i] / 10;
273         result.s[i] %= 10;
274     }
275     result.clean();
276     result.sign = !(sign^num.sign);
277     return result;
278 }
279 
280 bign bign::operator*(const int num)const
281 {
282     bign x = num;
283     bign z = *this;
284     return x*z;
285 }
286 bign bign::operator*=(const bign&num)
287 {
288     *this = *this * num;
289     return *this;
290 }
291 
292 bign bign::operator /(const bign&num)const
293 {
294     bign ans;
295     ans.len = len - num.len + 1;
296     if (ans.len < 0)
297     {
298         ans.len = 1;
299         return ans;
300     }
301 
302     bign divisor = *this, divid = num;
303     divisor.sign = divid.sign = 1;
304     int k = ans.len - 1;
305     int j = len - 1;
306     while (k >= 0)
307     {
308         while (divisor.s[j] == 0) j--;
309         if (k > j) k = j;
310         char z[MAX_L];
311         memset(z, 0, sizeof(z));
312         for (int i = j; i >= k; i--)
313             z[j - i] = divisor.s[i] + 0;
314         bign dividend = z;
315         if (dividend < divid) { k--; continue; }
316         int key = 0;
317         while (divid*key <= dividend) key++;
318         key--;
319         ans.s[k] = key;
320         bign temp = divid*key;
321         for (int i = 0; i < k; i++)
322             temp = temp * 10;
323         divisor = divisor - temp;
324         k--;
325     }
326     ans.clean();
327     ans.sign = !(sign^num.sign);
328     return ans;
329 }
330 
331 bign bign::operator/=(const bign&num)
332 {
333     *this = *this / num;
334     return *this;
335 }
336 
337 bign bign::operator%(const bign& num)const
338 {
339     bign a = *this, b = num;
340     a.sign = b.sign = 1;
341     bign result, temp = a / b*b;
342     result = a - temp;
343     result.sign = sign;
344     return result;
345 }
346 
347 bign bign::pow(const bign& num)const
348 {
349     bign result = 1;
350     for (bign i = 0; i < num; i++)
351         result = result*(*this);
352     return result;
353 }
354 
355 bign bign::factorial()const
356 {
357     bign result = 1;
358     for (bign i = 1; i <= *this; i++)
359         result *= i;
360     return result;
361 }
362 
363 void bign::clean()
364 {
365     if (len == 0) len++;
366     while (len > 1 && s[len - 1] == \0)
367         len--;
368 }
369 
370 bign bign::Sqrt()const
371 {
372     if(*this<0)return -1;
373     if(*this<=1)return *this;
374     bign l=0,r=*this,mid;
375     while(r-l>1)
376     {
377         mid=(l+r)/2;
378         if(mid*mid>*this)
379             r=mid;
380         else
381             l=mid;
382     }
383     return l;
384 }
385 
386 bign::~bign()
387 {
388 }
389 
390 bign num0,num1,res;
391 
392 int main() {
393     num0 = 1, num1 = 2;
394     res=num0-num1;
395     cout << res << endl;
396     return 0;
397 }

 

高精度模板

原文:http://www.cnblogs.com/zxhl/p/4984923.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!