加与减

Posted Sphreez

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加与减相关的知识,希望对你有一定的参考价值。

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

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;
  return 0; }

 

以上是关于加与减的主要内容,如果未能解决你的问题,请参考以下文章

Springmvc 加与不加@requestparam有啥区别

JavaScript--JS中函数名后面的括号加与不加的区别和作用

JS 中函数名后面加与不加括号的区别

@RequestParam详解以及加与不加的区别

@RequestParam加与不加的区别

@RequestParam加与不加的区别