SHADL 高精度库 v1.01

· · 个人记录

/*
    Update Log 1.01
    1. Rewritten functions between high_decimal and integers with templates
    2. Fixed c_str function but now it needs to be freed by you.
   AWARE: I HAVE USED strrev() IN c_str() FUNCTION. IT CANNOT BE COMPILED UNDER LINUX.I COMMENTED IT IN THE CODE.
    3. Added boolean functions between high_decimal and integers with templates
*/
/*
    Logs
    I think I will focus on OI since then
    so I will change the version number into .0x for minor updates.
*/
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
class high_decimal
{
        //public:
    private:
        // store each bit and length
        char number[100001];
        int length;
        int _size;//this is sort of useless
        bool _signed;
        //Error fixing (very important)
        void remove_zero()
        {
            if(number[0]==0 && length==0)
                return;
            while((number[length]<=0 || number[length]>9) && length>=0) length--;
            length++;
            return;
        }
    public:
        //Basic
        void init(int x = 0)
        {
            memset(number,0,sizeof(number));
            _size=x;
            length=0;
            _signed=0;
            remove_zero();
        }
        int len()
        {
            return length;
        }
        //I/O
        void input()//Also a constructer
        {
            string buffer;
            cin>>buffer;
            length=0;
            if(buffer[0]=='-')
            {
                _signed=true;
                length--;
            }
            length=buffer.length();
            _size=buffer.length();
            for(int i=length-1; i>=0; i--)
            {
                number[length-1-i]=buffer[i]-48;
            }
            number[length+1]='\0';
            remove_zero();
            return;
        }
        void output()
        {
            if(_signed) cout<<'-';
            remove_zero();
            if(number[0]==0 && length==0)
            {
                cout<<0;
                return;
            }
            for(int i=0; i<length; i++)
            {
                printf("%d",number[length-i-1]);
            }
            return;
        }
        //Transferring functions
        long long to_longlong()
        {
            long long res=0;
            for(int t=length-1; t>=0; t--)
            {
                res*=10;
                res+=number[t];
            }
            if(_signed) res=-res;
            return res;
        }
        const char* c_str()
        {
            char *res;
            res=(char*)malloc(100001*sizeof(char));
            strcpy(res,number);
//          strrev(res);
            return res;
        }
        //constructers & deconstructers
        high_decimal ()
        {
            init();
        }
        high_decimal(const long long a)
        {
            int x=a;
            init(ceil(log10(x)));
            if(x<0) _signed=1,x=-x;
            length=0;
            for(int i=0; x!=0; i++)
            {
                number[i]=x%10;
                x/=10;
                length++;
            }
        }
        high_decimal(const int a)
        {
            int x=a;
            init(ceil(log10(x)));
            if(x<0) _signed=1,x=-x;
            if(x==0)
            {
                length=1,number[0]=0,_signed=0;
                return;
            }
            length=0;
            for(int i=0; x!=0; i++)
            {
                number[i]=x%10;
                x/=10;
                length++;
            }
        }
        high_decimal (const char* buffer)
        {
            init(strlen(buffer));
            length=0;
            if(buffer[0]=='-')
            {
                _signed=true;
                length--;
            }
            for(int i=strlen(buffer)-1; i>=0; i--)
            {
                length++;
                number[strlen(buffer)-1-i]=buffer[i]-48;
            }
            remove_zero();
            return;
        }
        ~high_decimal() {}

        //operators between high decimals

        //Boolean operators
        bool operator>(const high_decimal&b)
        {
            int tmp1,tmp2;
            bool result=0;
            if(_signed && !b._signed) return 0;
            if(!_signed && b._signed) return 1;
            if(length>b.length)
                return 1;
            if(length<b.length)
                return 0;
            for(int i=length-1; i>=0; i--)
            {
                tmp1=number[i];
                tmp2=b.number[i];
                if(tmp1>tmp2)
                {
                    result=1;
                    break;
                }
                if(tmp1<tmp2)
                {
                    result=0;
                    break;
                }
            }
            if(_signed && b._signed) result=!result;
            return result;
        }

        bool operator==(const high_decimal &b)
        {
            int tmp1,tmp2;
            if(_signed && b._signed == 0) return 0;
            if(!_signed && b._signed == 1) return 0;
            if(length<b.length)
                return 0;
            if(length>b.length)
                return 0;
            for(int i=length-1; i>=0; i--)
            {
                tmp1=number[i];
                tmp2=b.number[i];
                if(tmp1<tmp2)
                    return 0;
                if(tmp1>tmp2)
                    return 0;
            }
            return 1;
        }
        bool operator<(const high_decimal&b)
        {
            return !(*this>b || *this==b);
        }
        bool operator>=(const high_decimal &b)
        {
            return !(*this<b);
        }
        bool operator<=(const high_decimal &b)
        {
            return !(*this>b);
        }

        //Arithmetic operators
        void operator>>=(const int &b)//The shifting operators are based on decimal
        {
            if(b>=length)
            {
                number[0]=0;
                length=1;
                return;
            }
            for(int i=b; i<length; i++)
            {
                if(i-b>=0)
                    number[i-b]=number[i];
                number[i]=0;
            }
            length=length-b;
        }
        high_decimal operator>>(const int &b)
        {
            high_decimal t;
            t=*this;
            if(b>=t.length)
            {
                t.number[0]=0;
                t.length=1;
                return t;
            }
            for(int i=b; i<length; i++)
            {
                if(i-b>=0)
                    number[i-b]=number[i];
                t.number[i-b]=t.number[i];
                t.number[i]=0;
            }
            t.length=length-b;
            return t;
        }
        void operator<<=(const int &b)
        {
            length+=b;
            for(int i=length; i>=b; i--)
            {
                number[i]=number[i-b];
                number[i-b]=0;
            }
            remove_zero();
        }
        high_decimal operator<<(const int &b)
        {
            high_decimal t;
            t=*this;
            t.length+=b+1;
            for(int i=t.length; i>=b; i--)
            {
                t.number[i]=t.number[i-b];
                t.number[i-b]=0;
            }
            t.remove_zero();
            return t;
        }
        high_decimal operator+(const high_decimal &b)
        {
            high_decimal result=*this;
            result.length=(length>b.length?length:b.length)+1;
            for(long long i=0; i<result.length; i++)
            {
                result.number[i]+=b.number[i];
                if(result.number[i]>9)
                {
                    result.number[i]-=10;
                    result.number[i+1]++;
                }
            }
            result.remove_zero();
            return result;
        }
        high_decimal operator-(const high_decimal&b)
        {
            high_decimal result=*this;
            result.length=(length>b.length?length:b.length)+1;
            for(long long i=0; i<result.length; i++)
            {
                result.number[i]-=b.number[i];
                if(result.number[i]<0)
                {
                    result.number[i]+=10;
                    result.number[i+1]--;
                }
            }
            result.remove_zero();
            return result;
        }
        high_decimal operator*(const high_decimal&y)
        {
            high_decimal res;
            res.length=length+y.length;
            int tmp;
            for(int i=0; i<length; i++)
            {
                tmp=0;
                for(int j=0; j<y.length; j++)
                {
                    res.number[i+j]+=number[i]*y.number[j]+tmp;
                    tmp=res.number[i+j]/10;
                    res.number[i+j]%=10;
                }
                res.number[i+y.length]+=tmp;
            }
            res.remove_zero();
            return res;
        }
        high_decimal operator/(const high_decimal &b)
        {
            if(b.number[0]==0 && b.length==1)
            {
                signal(SIGFPE, SIG_DFL);
                exit(SIGFPE);
            }
            high_decimal rem,tmp,cst;
            long long now;
            rem=*this;
            char res[100001]= {0};
            if(rem<b) return res;
            while(rem>=b)
            {
                tmp=b;
                now=0;
                while(tmp<=rem)
                    tmp<<=1,now++;
                tmp>>=1,now--;
                cst="0";
                int k=0;
                while(cst<=rem)
                    cst=cst+tmp,k++;
                k--;
                cst=cst-tmp;
                rem=rem-cst;
                res[now]=k;
            }
            rem.init();
            rem.length=max(this->length,b.length)+1;
            for(int i=0; i<=rem.length; i++) rem.number[i]=res[i];
            return rem;
        }
        high_decimal operator%(const high_decimal &b)
        {
            return *this-(*this/b*b);
        }
        void operator+=(const high_decimal &b)
        {
            *this=*this+b;
        }
        void operator-=(const high_decimal &b)
        {
            *this=*this-b;
        }
        void operator*=(const high_decimal &b)
        {
            *this=*this*b;
        }
        void operator/=(const high_decimal &b)
        {
            *this=*this/b;
        }
        void operator%=(const high_decimal &b)
        {
            *this=*this%b;
        }
        //operators between high_decimal and integers/strings
        //operator =
        void operator=(const char* buffer)
        {
            init(strlen(buffer));
            length=0;
            if(buffer[0]=='-')
            {
                _signed=true;
                length--;
            }
            for(int i=strlen(buffer)-1; i>=0; i--)
            {
                length++;
                number[strlen(buffer)-1-i]=buffer[i]-48;
            }
            remove_zero();
            return;
        }
        void operator=(const long long &b)
        {
            long long x=b;
            init(ceil(log10(x)));
            init();
            if(x<0) _signed=1,x=-x;
            if(x==0)
            {
                length=1,number[0]=0,_signed=0;
                return;
            }
            length=0;
            for(int i=0; x!=0; i++)
            {
                number[i]=x%10;
                x/=10;
                length++;
            }
        }
        void operator=(const int &b)
        {
            int x=b;
            init(ceil(log10(x)));
            init();
            if(x<0) _signed=1,x=-x;
            if(x==0)
            {
                length=1,number[0]=0,_signed=0;
                return;
            }
            length=0;
            for(int i=0; x!=0; i++)
            {
                number[i]=x%10;
                x/=10;
                length++;
            }
        }
        //arithmetic operators
        template<typename T>
        high_decimal operator+(const T &t)
        {
            return *this+high_decimal(t);
        }
        template<typename T>
        high_decimal operator-(const T &t)
        {
            return *this-high_decimal(t);
        }
        template<typename T>
        high_decimal operator*(const T &t)
        {
            return *this*high_decimal(t);
        }
        template<typename T>
        high_decimal operator/(const T &t)
        {
            return *this/high_decimal(t);
        }
        template<typename T>
        high_decimal operator%(const T &t)
        {
            return *this%high_decimal(t);
        }
        template<typename T>
        void operator+=(const T &t)
        {
            *this=*this+high_decimal(t);
        }
        template<typename T>
        void operator-=(const T &t)
        {
            *this=*this-high_decimal(t);
        }
        template<typename T>
        void operator*=(const T &t)
        {
            *this=*this*high_decimal(t);
        }
        template<typename T>
        void operator/=(const T &t)
        {
            *this=*this/high_decimal(t);
        }
        template<typename T>
        void operator%=(const T &t)
        {
            *this=*this%high_decimal(t);
        }
        // Boolean operators
        template<typename T>
        bool operator==(const T &t)
        {
            return *this==high_decimal(t);
        }
        bool operator>(const int &t)
        {
            return *this>high_decimal(t);
        }
        template<typename T>
        bool operator<(const T &t)
        {
            return *this<high_decimal(t);
        }
        template<typename T>
        bool operator>=(const T &t)
        {
            return !(*this<high_decimal(t));
        }
        template<typename T>
        bool operator<=(const T &t)
        {
            return !(*this>high_decimal(t));
        }
        template<typename T>
        bool operator!=(const T &t)
        {
            return !(*this==high_decimal(t));
        }
};
//iostream
ostream &operator<<(ostream &out,high_decimal &a)
{
    a.output();
    return out;
}
ostream &operator<<(ostream &out,high_decimal a)
{
    a.output();
    return out;
}
istream &operator>>(istream &in,high_decimal &a)
{
    string x;
    in>>x;
    a=x.c_str();
    return in;
}

int main()
{
    high_decimal a;
    cin>>a;
    cout<<(a==114);
    return 0;
}