zoukankan      html  css  js  c++  java
  • 加与减与乘与除

    #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;
    }

  • 相关阅读:
    VML编程之shape多边型.shapetype模版.shape与curve曲线《VML极道教程》原著:沐缘华
    VML编程之image图片《VML极道教程》原著:沐缘华
    软件开发项目的风险管理 (转)
    VML编程之polyline多边型《VML极道教程》原著:沐缘华
    VML编程之标记实战与line线《VML极道教程》原著:沐缘
    xml操作类,封装了常用的对XML文件的操作功能....
    Bugzilla 安装手册
    IT项目管理之<<少林练步拳>>(转)
    Atlas学习手记(8):调用本地Web Service简单介绍(转摘)
    WF从设计器出发,到对从设计器出来的工作流的调用加载,已经完成了整个工作流的详细设计,目前工作流设计器已经完成!
  • 原文地址:https://www.cnblogs.com/sphreez/p/8594951.html
Copyright © 2011-2022 走看看