c++语言之大数模板
Vivian 2018-06-04 来源 : 阅读 806 评论 0

摘要:本文主要向大家介绍了用c++语言来写大数模板,通过具体的代码向大家展示,希望对大家学习c++语言有所帮助。

   本文主要向大家介绍了用c++语言来写大数模板,通过具体的代码向大家展示,希望对大家学习c++语言有所帮助。

//支持正数的运算。两数相减也要为正数
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=10000;
struct bign 
{ 
    int len, s[MAXN]; 
    bign () 
    { 
        memset(s, 0, sizeof(s)); 
        len = 1; 
    } 
    bign (int num) { *this = num; } 
    bign (const char *num) { *this = num; } 
    bign operator = (const int num) 
    { 
        char s[MAXN]; 
        sprintf(s, "%d", num); 
        *this = s; 
        return *this; 
    } 
    bign operator = (const char *num) 
    { 
        for(int i = 0; num[i] == '0'; num++) ;  //去前导0 
        len = strlen(num); 
        for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0'; 
        return *this; 
    } 
    bign operator + (const bign &b) const //+ 
    { 
        bign c; 
        c.len = 0; 
        for(int i = 0, g = 0; g || i < max(len, b.len); i++) 
        { 
            int x = g; 
            if(i < len) x += s[i]; 
            if(i < b.len) x += b.s[i]; 
            c.s[c.len++] = x % 10; 
            g = x / 10; 
        } 
        return c; 
    } 
    bign operator += (const bign &b) 
    { 
        *this = *this + b; 
        return *this; 
    } 
    void clean() 
    { 
        while(len > 1 && !s[len-1]) len--; 
    } 
    bign operator * (const bign &b) //* 
    { 
        bign c; 
        c.len = len + b.len; 
        for(int i = 0; i < len; i++) 
        { 
            for(int j = 0; j < b.len; j++) 
            {
                c.s[i+j] += s[i] * b.s[j]; 
            } 
        }
        for(int i = 0; i < c.len; i++) 
        {
            c.s[i+1] += c.s[i]/10; 
            c.s[i] %= 10; 
        }
        c.clean();
        return c;
    }
    bign operator *= (const bign &b) 
    {
        *this = *this * b; 
        return *this; 
    }
    bign operator - (const bign &b) 
    { 
        bign c; 
        c.len = 0; 
        for(int i = 0, g = 0; i < len; i++) 
        {
            int x = s[i] - g;
            if(i < b.len) x -= b.s[i];
            if(x >= 0) g = 0;
            else 
            { 
                g = 1; 
                x += 10; 
            } 
            c.s[c.len++] = x; 
        } 
        c.clean(); 
        return c; 
    } 
    bign operator -= (const bign &b) 
    { 
        *this = *this - b; 
        return *this; 
    } 
    bign operator / (const bign &b) 
    { 
        bign c, f = 0; 
        for(int i = len-1; i >= 0; i--) 
        { 
            f = f*10; 
            f.s[0] = s[i]; 
            while(f >= b) 
            { 
                f -= b; 
                c.s[i]++; 
            } 
        } 
        c.len = len; 
        c.clean(); 
        return c; 
    } 
    bign operator /= (const bign &b) 
    { 
        *this  = *this / b; 
        return *this; 
    } 
    bign operator % (const bign &b) 
    { 
        bign r = *this / b; 
        r = *this - r*b; 
        return r; 
    } 
    bign operator %= (const bign &b) 
    { 
        *this = *this % b; 
        return *this; 
    } 
    bool operator < (const bign &b) 
    { 
        if(len != b.len) return len < b.len; 
        for(int i = len-1; i >= 0; i--) 
        { 
            if(s[i] != b.s[i]) return s[i] < b.s[i]; 
        } 
        return false; 
    } 
    bool operator > (const bign &b) 
    { 
        if(len != b.len) return len > b.len; 
        for(int i = len-1; i >= 0; i--) 
        { 
            if(s[i] != b.s[i]) return s[i] > b.s[i]; 
        } 
        return false; 
    } 
    bool operator == (const bign &b) 
    { 
        return !(*this > b) && !(*this < b); 
    } 
    bool operator != (const bign &b) 
    { 
        return !(*this == b); 
    } 
    bool operator <= (const bign &b) 
    { 
        return *this < b || *this == b; 
    } 
    bool operator >= (const bign &b) 
    { 
        return *this > b || *this == b; 
    } 
    string str() const 
    { 
        string res = ""; 
        for(int i = 0; i < len; i++) res = char(s[i]+'0') + res; 
        return res; 
    }
};
 
istream& operator >> (istream &in, bign &x) 
{ 
    string s; 
    in >> s; 
    x = s.c_str(); 
    return in; 
} 
   
ostream& operator << (ostream &out, const bign &x) 
{ 
    out << x.str(); 
    return out; 
}
int main(){
    bign a,b;
    cin>>a;
    cout<
</a<<endl;></cstring></iostream>

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @Vivian 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程