高精度运算模板

/*
 * Author: Gatevin
 * Created Time:  2014/7/5 23:58:51
 * File Name: test.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define MAXN 1000


struct bign
{
    int len;
    int s[MAXN];
    
    void cleanLeadZero();
    
    string str() const;
    
    bign();
    bign(int);
    bign(const char*);
    
    bign getFront(int) const;
    
    bign operator = (const char*);
    bign operator = (int);
    
    bign operator + (const bign&) const;
    bign operator - (const bign&) const;
    bign operator * (const bign&) const;
    bign operator / (const bign&) const;
    bign operator % (const bign&) const;
    /*
     * bign operator & (const bign&) const;
     * bign operator | (const bign&) const;
     * bign operator ^ (const bign&) const;
     * bign operator ~ () const;
     * bign operator >> (const bign&) const;
     * bign operator << (const bign&) const;
     */
    
    
    bign operator += (const bign&);
    bign operator -= (const bign&);
    bign operator *= (const bign&);
    bign operator /= (const bign&);
    bign operator %= (const bign&);
    /*
     * bign operator &= (const bign&);
     * bign operator |= (const bign&);
     * bign operator ^= (const bign&);
     * bign operator >>= (const bign&);
     * bign operator <<= (const bign&);
     */
    
    bool operator < (const bign&) const;
    bool operator > (const bign&) const;
    bool operator <= (const bign&) const;
    bool operator >= (const bign&) const;
    bool operator == (const bign&) const;
    
    friend istream & operator >> (istream&, bign&);
    friend ostream & operator << (ostream&, bign&);
    
};

void bign :: cleanLeadZero()
{
    while(len > 1 && !s[len - 1])
    {
        len--;
    }
    return;
}

string bign :: str() const
{
    string res = "";
    for(int i = 0; i < len; i++)
    {
        res = (char)(s[i] + '0') + res;
    }
    if(res == "") res = "0";
    return res;
}

bign :: bign()
{
    len = 0;
    memset(s, 0, sizeof(s));
}

bign :: bign(const char* num)
{
    *this = num;
}

bign :: bign(int num)
{
    *this = num;
}

bign bign :: getFront(int n) const
{
    bign c;
    c.len = 0;
    for(int i = 0; i < n; i++)
    {
        c.s[c.len++] = s[len - n + i];
    }
    return c;
}

bign bign :: operator = (const char* num)
{
    len = strlen(num);
    for(int i = 0; i < len; i++)
    {
        s[i] = num[len - i - 1] - '0';
    }
    return *this;
}

bign bign :: operator = (int num)
{
    char s[MAXN];
    sprintf(s, "%d", num);
    *this = s;
    return *this;
}

bign bign :: operator + (const bign& x) const
{
    bign r;
    r.len = 0;
    int max_len = max(len, x.len);
    for(int i = 0, up = 0; up || i < max_len; i++)
    {
        int tmp = up;
        if(i < len) tmp += s[i];
        if(i < x.len) tmp += x.s[i];
        up = tmp /10;
        r.s[r.len++] = tmp % 10;
    }
    r.cleanLeadZero();
    return r;
}

bign bign :: operator - (const bign& x) const
{
    bign c;
    c.len = 0;
    for(int i= 0, down = 0; i < len; i++)
    {
        int tmp = s[i] - down;
        if(i < x.len) tmp -= x.s[i];
        if(tmp >= 0) down = 0;
        else
        {
            down = 1;
            tmp += 10;
        }
        c.s[c.len++] = tmp;
    }
    c.cleanLeadZero();
    return c;
}

bign bign :: operator * (const bign& x) const
{
    bign c;
    c.len = len + x.len;
    for(int i = 0; i < len; i++)
    {
        for(int j = 0; j < x.len; j++)
        {
            c.s[i + j] += s[i] * x.s[j];
        }
    }
    for(int i = 0; i < c.len - 1; i++)
    {
        c.s[i + 1] += c.s[i] / 10;
        c.s[i] %= 10;
    }
    c.cleanLeadZero();
    return c;
}

bign bign :: operator / (const bign& x) const
{
    bign r;
    r.len = 0;
    bign tmp = this->getFront(x.len - 1);
    for(int i = len - x.len; i>= 0; i--)
    {
        tmp = tmp * 10 + s[i];
        if(tmp < x)
        {
            r.s[r.len++] = 0;
        }
        else
        {
            int j;
            for(j = 1; j <= 10; j++)
            {
                if(x * j > tmp) break;
            }
            r.s[r.len++] = j - 1;
            tmp = tmp - (x * (j - 1));
        }
    }
    for(int i = 0; i < (r.len >> 1); i++)
    {
        int tmp = r.s[i];
        r.s[i] = r.s[r.len - 1 - i];
        r.s[r.len - 1 - i] = tmp;
    }
    r.cleanLeadZero();
    return r;
}

bign bign :: operator % (const bign& x) const
{
    bign r;
    r = *this / x;
    r = *this - r *x;
    return r;
}

bign bign :: operator += (const bign& x)
{
    *this = *this + x;
    return *this;
}

bign bign :: operator -= (const bign& x)
{
    *this = *this - x;
    return *this;
}

bign bign :: operator *= (const bign& x)
{
    *this = *this * x;
    return *this;
}

bign bign:: operator /= (const bign& x)
{
    *this = *this / x;
    return *this;
}

bign bign :: operator %= (const bign& x)
{
    *this = *this % x;
    return *this;
}

bool bign :: operator < (const bign& x) const
{
    if(len != x.len) return len < x.len;
    for(int i = len - 1; i >= 0; i--)
    {
        if(s[i] != x.s[i]) return s[i] < x.s[i];
    }
    return false;
}

bool bign :: operator > (const bign& x) const
{
    return x < *this;
}

bool bign :: operator <= (const bign& x) const
{
    return !(*this  > x);
}

bool bign :: operator >= (const bign& x) const
{
    return !(*this < x);
}

bool bign :: operator == (const bign& x) const
{
    return !(*this > x) && !(*this < x);
}


istream & operator >> (istream & in, bign& x)
{
    string s;
    in >> s;
    x = s.c_str();
    return in;
}

ostream & operator << (ostream & out, bign& x)
{
    out<<x.str();
    return out;
}

int main()
{
    bign x1,x2;
    while(cin>>x1>>x2)
    {
       // cout<<x1 * x2<<endl;  Compile question
        bign x3 = x1*x2;
        cout<<x3<<endl;//No problem
    }
    return 0;
}

整理一下模板吧,以后应该会有用,不过还有一些功能和bug待修复


你可能感兴趣的:(模板,高精度运算)