【原创】高精度(压位储存)模板

无聊写了个高精度模板玩玩......

(以前有些地方写错了TAT)

  1 /*
  2 唐代李白
  3 《江夏别宋之悌》
  4 楚水清若空,遥将碧海通。人分千里外,兴在一杯中。
  5 谷鸟吟晴日,江猿啸晚风。平生不下泪,于此泣无穷.
  6 */
  7 #include <iostream>
  8 #include <cstdio>
  9 #include <algorithm>
 10 #include <cstring>
 11 #include <vector>
 12 #include <utility>
 13 #include <iomanip>
 14 #include <string>
 15 #include <cmath>
 16 #include <queue>
 17 #include <assert.h>
 18 #include <map>
 19 #include <ctime>
 20 #include <cstdlib>
 21 #include <stack>
 22 #include <set> 
 23 #define LOCAL
 24 const int INF = 0x7fffffff;
 25 const int MAXN = 100000  + 10;
 26 const int maxnode = 20000 * 2 + 200000 * 20;
 27 const int MAXM = 50000 + 10;
 28 const int MAX = 100;
 29 using namespace std;
 30 struct hp{
 31        int num[MAX];
 32        ////hp(){num[0] = 0;}
 33        hp & operator = (const char *str);
 34        hp & operator = (int b);
 35        
 36        //最好先比较一下再大的减小的 
 37        hp operator + (const hp &)const;
 38        hp operator * (const hp &)const;
 39        hp operator - (const hp &)const;
 40        hp operator / (const hp &)const;
 41        
 42        bool operator < (const hp &)const;
 43        bool operator > (const hp &)const;
 44        bool operator == (const hp &)const;
 45        bool operator >= (const hp &)const;
 46        bool operator <= (const hp &)const;
 47 };
 48 bool hp::operator < (const hp &b)const{
 49      if (num[0] < b.num[0]) return 1;
 50      for (int i = num[0]; i >= 1; i--){
 51          if (num[i] < b.num[i]) return 1;
 52          else if (num[i] > b.num[i]) return 0;
 53      }
 54      return 0;
 55 }
 56 bool hp::operator > (const hp &b)const{return b < (*this);}
 57 bool hp::operator == (const hp &b)const {return ((!(b < (*this))) && (!(b > (*this))));}
 58 bool hp::operator <= (const hp &b)const {return !((*this) > b);}
 59 bool hp::operator >= (const hp &b)const {return !((*this) < b);}
 60 //注意d为余数 
 61 hp hp::operator / (const hp &b)const{
 62     hp d, c;
 63     memset(c.num, 0, sizeof(c));
 64     memset(d.num, 0, sizeof(d));
 65     c.num[0] = num[0] + b.num[0] + 1;//和乘法的长度是一样的,长除法..
 66     d.num[0] = 0;
 67     
 68     for (int i = num[0]; i >= 1; i--){
 69         //这个是腾出一个d.num[1]的空间 
 70         memmove(d.num + 2, d.num + 1, sizeof(d.num) - sizeof(int) * 2);
 71         d.num[0]++;
 72         d.num[1] = num[i];
 73         
 74         int l = 0, r = 9999, mid;//枚举除数
 75         while (l < r){//不同的写法当然可以 
 76               int mid = (l + r) >> 1;
 77               hp tmp; 
 78               tmp = mid;tmp = tmp * b;
 79               //b乘以乘数当然不能大于余数 
 80               if (tmp <= d) l = mid + 1;
 81               else r  = mid;
 82         } 
 83         c.num[i] = r - 1;
 84         hp tmp; tmp = r - 1;//r - 1为得到答案 
 85         tmp = tmp * b; 
 86         d = d - tmp;
 87     }
 88     while (c.num[c.num[0]] == 0 && c.num[0] > 1) c.num[0]--;
 89     return c;         
 90 }
 91 hp & hp::operator = (const int b){
 92    char str[MAX];
 93    sprintf(str, "%d", b);
 94    return *this = str; 
 95 }
 96 hp & hp::operator = (const char *str){
 97    int len = strlen(str), k = 1;
 98    memset(num, 0, sizeof(num));
 99    num[0] = 1;
100    for (int i = len - 1 ; i >= 0; i--){
101        if (k == 10000) {k = 1; num[0]++;}
102        num[num[0]] += k * (str[i] - '0');
103        k *= 10;
104    }
105    return *this;
106 }
107 hp hp::operator + (const hp &b)const{
108    hp c;
109    memset(c.num, 0, sizeof(c.num));
110    c.num[0] = max(num[0], b.num[0]);
111    for (int i = 1; i <= c.num[0]; i++){
112        c.num[i] += num[i] + b.num[i];
113        c.num[i + 1] += c.num[i] / 10000;
114        c.num[i] %= 10000;
115    }
116    if (c.num[c.num[0] + 1] > 0) c.num[0]++;
117    return c;
118 }
119 hp hp::operator * (const hp &b)const{
120    hp c;
121    memset(c.num, 0, sizeof(c.num));
122    c.num[0] = num[0] + b.num[0] + 1;
123    for (int i = 1; i <= num[0]; i++)
124    for (int j = 1; j <= b.num[0]; j++){
125        c.num[i + j - 1] += num[i] * b.num[j];
126        c.num[i + j] += c.num[i + j - 1] / 10000;
127        c.num[i + j - 1] %= 10000;
128    }
129    while (c.num[c.num[0]] == 0 && c.num[0] > 1) c.num[0]--;
130    return c;
131 }
132 hp hp::operator - (const hp &b)const{
133    hp c;
134    memset(c.num, 0, sizeof(c.num));
135    c.num[0] = max(num[0], b.num[0]);
136    for (int i = 1; i <= c.num[0]; i++){
137        c.num[i] += num[i] - b.num[i];
138        if (c.num[i] < 0){
139           c.num[i + 1]--;
140           c.num[i] += 10000; 
141        }
142    }
143    while (c.num[c.num[0]] == 0 && c.num[0] > 1) c.num[0]--;
144    return c;
145 }
146 void print(hp c){
147      printf("%d", c.num[c.num[0]]);
148      for (int i = c.num[0] - 1; i >= 1; i--) printf("%04d", c.num[i]);
149      return;
150 }
151 
152 int main(){
153    #ifdef LOCAL
154    freopen("data.txt", "r", stdin);
155    freopen("out.txt", "w", stdout);
156    #endif
157    hp c;
158    c = 0;
159    hp a; 
160    a = 20300;
161    hp b;
162    //print(a);
163    //print(c);
164    b = c / a; 
165    print(b);
166    //printf("%d", (a == c));
167    return 0; 
168 }
View Code

 

posted @ 2014-05-20 21:03  TCtower  阅读(427)  评论(0编辑  收藏  举报