首页 > 代码库 > 高精度模板

高精度模板

转自郭大侠博客。。。

 

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

 

高精度模板