博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
加与减与乘与除
阅读量:4599 次
发布时间:2019-06-09

本文共 2466 字,大约阅读时间需要 8 分钟。

#include<bits/stdc++.h>

using namespace std;
const int maxn=2000;
struct bign
{
    int d[maxn],len;
    void clean()
    {
        while(len>1&&!d[len-1])len--;
    }
    bign()
    {
        memset(d,0,sizeof(d));
        len=1;
    }
    bign(int num)
    {
        *this=num;
    }
    bign(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)
    {
        len=strlen(num);
        for(int i=0; i<len; i++)d[i]=num[len-1-i]-48;
        clean();
        return *this;
    }
    string str()const
    {
        string res;
        for(int i=0; i<len; i++)res=char(d[i]+48)+res;
        return res;
    }
    bign operator + (const bign& b)
    {
        bign c;
        int i;
        c=*this;
        for(i=0; i<b.len; i++)
        {
            c.d[i]+=b.d[i];
            if(c.d[i]>9)
            {
                c.d[i]-=10;
                c.d[i+1]++;
            }
        }
        while(c.d[i]>9)
        {
            c.d[i++]-=10;
            c.d[i]++;
        }
        c.len=max(b.len,len);
        if(c.d[i]&&c.len<=i)c.len=i+1;
        return c;
    }
    bign operator - (const bign& b)
    {
        bign c;
        int i;
        c=*this;
        for(i=0; i<b.len; i++)
        {
            c.d[i]-=b.d[i];
            if(c.d[i]<0)
            {
                c.d[i]+=10;
                c.d[i+1]--;
            }
        }
        while(c.d[i]<0)
        {
            c.d[i++]+=10;
            c.d[i]--;
        }
        c.clean();
        return c;
    }
    bign operator * (const bign& b)const
    {
        int i,j;
        bign c;
        c.len=len+b.len;
        for(i=0;i<len;i++)
        {
            for(j=0;j<b.len;j++)c.d[i+j]+=d[i]*b.d[j];
        }
        for(i=0;i<c.len-1;i++)
        {
            c.d[i+1]+=c.d[i]/10;
            c.d[i]%=10;
        }
        c.clean();
        return c;
    }
    bign operator / (const bign& b)
    {
        int i,j;
        bign c=*this,a=0;
        for(i=len-1;i>=0;i--)
        {
            a=a*10+d[i];
            for(j=0;j<10;j++)
            {
                if(a<b*(j+1))break;
            }
            c.d[i]=j;
            a=a-b*j;
        }
        c.clean();
        return c;
    }
        bign operator % (const bign& b)
    {
        int i,j;
        bign a=0;
        for(i=len-1;i>=0;i--)
        {
            a=a*10+d[i];
            for(j=0;j<10;j++)
            {
                if(a<b*(j+1))break;
            }
            a=a-b*j;
        }
        return a;
    }
    bool operator < (const bign& b)const
    {
        if(len!=b.len)return len<b.len;
        for(int i=len-1;i>=0;i--)
        {
            if(d[i]!=b.d[i])return d[i]<b.d[i];
        }
        return false;
    }
    bool operator > (const bign& b)const{return b<*this;}
    bool operator <= (const bign& b)const{return !(b<*this);}
    bool operator >= (const bign& b)const{return !(*this<b);}
    bool operator != (const bign& b)const{return b<*this||*this<b;}
    bool operator == (const bign& b)const{return !(b<*this)&&!(*this<b);}
};
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>>b;
    cout<<a<<"+"<<b<<"="<<a+b<<endl;
    cout<<a<<"-"<<b<<"="<<a-b<<endl;
    cout<<a<<"*"<<b<<"="<<a*b<<endl;
    cout<<a<<"/"<<b<<"="<<a/b<<endl;
    cout<<a<<"%"<<b<<"="<<a%b<<endl;
    return 0;
}

转载于:https://www.cnblogs.com/sphreez/p/8594951.html

你可能感兴趣的文章
struts2 入门
查看>>
.net 编译原理
查看>>
mean 快速开发和现有技术的对比分析
查看>>
Metro Style app :浏览器扩展
查看>>
linux的kernel是怎样工作的(TI_DM36X_ARM系统)(1)
查看>>
[luogu4310] 绝世好题 (递推)
查看>>
[luogu3203 HNOI2010] 弹飞绵羊 (分块)
查看>>
-Dmaven.multiModuleProjectDirectory system propery is not set.
查看>>
Python2 unichr() 函数
查看>>
Python 字典 copy()方法
查看>>
Minimum Path Sum
查看>>
Remove Duplicates from Sorted Array II
查看>>
常量指针和指针常量巧妙记忆方法[转]
查看>>
python-haproxy作业讲解视频总结
查看>>
批处理文件脚本总结
查看>>
快速排序C++代码
查看>>
mui搜索框 搜索点击事件
查看>>
bzoj 5289: [Hnoi2018]排列
查看>>
IE10 招贤纳意问题整理文章-安装卸载、功能设置篇
查看>>
joomla处境堪忧
查看>>