• 高精度运算(运算符重载)


    终于写好了哇,我的亲娘来,满满的都是泪啊。。。重载你妹啊。。。。。

    #include <iostream>
    #include <string.h>
    using namespace std;
    const int Base=1000000000;
    const int Capacity=100;
    typedef long long huge;
    
    struct BigInt
    {
        int Len;
        int Data[Capacity];
        BigInt() : Len(0) {}
        BigInt (const BigInt &V) : Len(V.Len)
        {
            memcpy (Data, V.Data, Len*sizeof*Data);
        }
        BigInt(int V) : Len(0)
        {
            for(; V>0; V/=Base) Data[Len++]=V%Base;
        }
        BigInt &operator=(const BigInt &V)
        {
            Len=V.Len;
            memcpy(Data, V.Data, Len*sizeof*Data);
            return *this;
        }
        int &operator[] (int Index)
        {
            return Data[Index];
        }
        int operator[] (int Index) const
        {
            return Data[Index];
        }
    };
    int compare(const BigInt &A, const BigInt &B)
    {
        if(A.Len!=B.Len) return A.Len>B.Len ? 1:-1;
        int i;
        for(i=A.Len-1; i>=0 && A[i]==B[i]; i--);
        if(i<0)return 0;
        return A[i]>B[i] ? 1:-1;
    }
    
    BigInt operator + (const BigInt &A,const BigInt &B)
    {
        int i,Carry(0);
        BigInt R;
        for(i=0; i<A.Len||i<B.Len||Carry>0; i++)
        {
            if(i<A.Len) Carry+=A[i];
            if(i<B.Len) Carry+=B[i];
            R[i]=Carry%Base;
            Carry/=Base;
        }
        R.Len=i;
        return R;
    }
    
    BigInt operator - (const BigInt &A,const BigInt &B)
    {
        int i,Carry(0);
        BigInt R;
        R.Len=A.Len;
        for(i=0; i<R.Len; i++)
        {
            R[i]=A[i]-Carry;
            if(i<B.Len) R[i]-=B[i];
            if(R[i]<0) Carry=1,R[i]+=Base;
            else Carry=0;
        }
        while(R.Len>0&&R[R.Len-1]==0) R.Len--;
        return R;
    }
    
    BigInt operator * (const BigInt &A,const BigInt &B)
    {
        int i;
        huge Carry(0);
        BigInt R;
        for(i=0; i<A.Len||Carry>0; i++)
        {
            if(i<A.Len) Carry+=huge(A[i])*B[i];
            R[i]=Carry%Base;
            Carry/=Base;
        }
        R.Len=i;
        return R;
    }
    istream &operator>>(istream &In,BigInt &V)
    {
        char Ch;
        for(V=0; In>>Ch;)
        {
            V=V*10+(Ch-'0');
            if(In.peek()<=' ') break;
        }
        return In;
    }
    ostream &operator<<(ostream &Out,const BigInt &V)
    {
        int i;
        Out<<(V.Len==0 ? 0:V[V.Len-1]);
        for(i=V.Len-2; i>=0; i--) for(int j=Base/10; j>0; j/=10) Out<<V[i]/j;
        return Out;
    }
    int main()
    {
        BigInt a,b;
        cin>>a>>b;
        cout<<"+"<<endl;
        cout<<a+b<<endl;
        if(compare(a, b)>0) cout<<"-"<<endl<<a-b<<endl;
        else cout<<"-"<<endl<<b-a<<endl;
        cout<<"*"<<endl<<a*b<<endl;
    }
    


  • 相关阅读:
    数据库-数据约束
    数据库-表2
    数据库-表
    MySQL入门
    记一次stm8l程序跑飞
    nRF24L01P的ShockBurst与Enhance ShockBurst
    电路板工艺中的NPTH和PTH
    nRF24L01P数据传输速率
    STM32F030-UART1_DMA使用提示
    Altium Designer 复制报错-奇怪的问题解决办法
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3005956.html
Copyright © 2020-2023  润新知