跳到正文

个人记录

_int8192

__Amnesia_search__约 33 分钟
原文
文章信息与版本

文章信息

发布
2024年10月16日
收录
2026年7月23日
更新
2026年11月8日
篇幅
约 33 分钟

版本

  1. v1当前
C++
#include<iostream>
#include<string.h> //字符串
#include<stack> //栈
using namespace std;

class _int8192{
    private:
        static const int BIT=1000; //长度
        short num[BIT+1];
        
        inline void getsize(){ //获取长度
            int i=BIT;
            while(i>=0 && num[i]==0) i--;
            if(i==-1) size=1;
            else size=i+1;
        }
        
        short cmp[BIT+1];
        bool PrintBool(){ //是否为0
            memset(cmp,0,sizeof(cmp));
            if(memcmp(num,cmp,sizeof(short)*BIT)==0) return true;
            return false;
        }
        
    public:
        int size; //位数
        bool porn; //正负
        
        inline void init(){ //初始化
            for(register int i=0; i<=BIT; i++){
                num[i]=0;
            }
        }
        
        static _int8192 intTO_int8192(int n){ //int转_int8192
            _int8192 ans;
            ans.init();
            if(n<0){
                ans.porn=false;
                n=-n;
            }
            else ans.porn=true;
            
            int p=0;
            while(n!=0){
                ans.num[p++]=n%10;
                n/=10;
            }
            
            ans.getsize();
            return ans;
        }
        #define _8192int(x) _int8192::intTO_int8192(x) //为了代码简便
        
        void read(){ //读入
            init();
            stack<short> st;
            size=0;
            memset(num,0,sizeof(num));
            char a;
            int x;
            int p=0;
            bool c=true;
            bool fr0=true;
            a=getchar();
            while(1){
                if(!((a>='0' && a<='9') || a=='-') || (c==false && a=='-')) break;
                
                if(a=='-'){
                    size--;
                    porn=false;
                }
                else if(fr0==false) st.push(a-'0');
                else if(fr0==true && a!='0'){
                    st.push(a-'0');
                    fr0=false;
                }
                else size--;
                
                if(c==true) c=false;
                size++;
                a=getchar();
            }
            
            for(register int i=0; i<size; i++){
                num[i]=st.top();
                st.pop();
            }
            if(size==0){
                size=1;
                num[0]=0;
            }
        }
        
        void print(){ //输出
            if(num[0]==-8){
                printf("Runtime Error! Zero(0) cannot be a divisor!");
                return;
            }
            if(num[0]==-9){
                printf("Runtime Error! Zero(0) to the power of zero(0) is undefined!");
                return;
            }
            if(PrintBool()==true){
                printf("0");
                return;
            }
            if(porn==false) printf("-");
            int p=size-1;
            while(p!=-1){
                printf("%d",num[p]);
                p--;
            }
        }
        
        void operator^(_int8192 &a){ //swap
            _int8192 t;
            t=*this;
            *this=a;
            a=t;
        }    
        
        void operator=(const _int8192 a){
            memcpy(num,a.num,sizeof(short)*BIT);
            porn=a.porn;
            size=a.size;
        }
        
        bool operator!(){
            return *this==0;
        }
        
        _int8192 abs(){ //绝对值
            _int8192 ret;
            ret=*this;
            ret.porn=true;
            return ret;
        }
        
        _int8192 opp(){ //相反数
            _int8192 ret;
            ret=*this;
            ret.porn=!ret.porn;
            return ret;
        }        
        
        bool operator>(const _int8192 a) const{
            if(porn==true && a.porn==false) return true;
            if(porn==false && a.porn==true) return false;
            bool ret=false;
            int bit=max(size,a.size);
            for(int i=bit-1; i>=0; i--){
                if(num[i]>a.num[i]){
                    ret=true;
                    break;
                }
                else if(num[i]<a.num[i]){
                    ret=false;
                    break;
                }
            }
            if(porn==false) return !ret;
            return ret;
        }
        
        bool operator<(const _int8192 a) const{
            if(porn==true && a.porn==false) return false;
            if(porn==false && a.porn==true) return true;
            bool ret=false;
            int bit=max(size,a.size);
            for(int i=bit-1; i>=0; i--){
                if(num[i]<a.num[i]){
                    ret=true;
                    break;
                }
                else if(num[i]>a.num[i]){
                    ret=false;
                    break;
                }
            }
            if(porn==false) return !ret;
            return ret;
        }
        
        bool operator==(const _int8192 a) const{
            if(porn!=a.porn) return false;
            int bit=max(size,a.size);
            for(int i=bit-1; i>=0; i--){
                if(num[i]!=a.num[i]) return false;
            }
            return true;
        }
        
        bool operator!=(const _int8192 a) const{
            return !(*this==a);
        }
        
        bool operator>=(const _int8192 a) const{
            return *this>a || *this==a;
        }
        
        bool operator<=(const _int8192 a) const{
            return *this<a || *this==a;
        }
        
        _int8192 operator+(const _int8192 a) const{ //高精度加法
            _int8192 ans;
            ans.init();
            
            if(porn==a.porn){
                ans.porn=porn;
                int bit=max(size,a.size);
                
                for(register int i=0; i<bit; i++){
                    ans.num[i]+=num[i]+a.num[i];
                    ans.num[i+1]=ans.num[i]/10;
                    ans.num[i]%=10;
                }
            }
            else{
                _int8192 b=a;
                _int8192 _this=*this;
                ans.porn=(b.abs()>_this.abs())?a.porn:porn;
                
                int bit=max(size,a.size);
                label:
                for(register int i=0; i<bit; i++){
                    ans.num[i]+=b.num[i]-_this.num[i];
                    if(ans.num[i]<0){
                        ans.num[i]+=10;
                        ans.num[i+1]--;
                    }
                }
                if(ans.num[bit]<0){
                    memset(ans.num,0,sizeof(short)*BIT);
                    b^_this;
                    goto label;
                }
            }
            
            ans.getsize();
            return ans;
        }
        
        
        _int8192 operator-(const _int8192 a) const{ //高精度减法
            _int8192 ans;
            ans.init();
            
            if(porn==a.porn){
                _int8192 b=a;
                _int8192 _this=*this;
                ans.porn=(b.abs()<_this.abs())?porn:!porn;
                
                int bit=max(size,a.size);
                if(ans.porn!=porn) b^_this;
                for(register int i=0; i<bit; i++){
                    ans.num[i]+=_this.num[i]-b.num[i];
                    if(ans.num[i]<0){
                        ans.num[i]+=10;
                        ans.num[i+1]--;
                    }
                }
            }
            
            else{
                ans.porn=porn;
                _int8192 b=a;
                b=b.opp();                
                int bit=max(size,b.size);
                for(register int i=0; i<bit; i++){
                    ans.num[i]+=num[i]+b.num[i];
                    ans.num[i+1]=ans.num[i]/10;
                    ans.num[i]%=10;
                }
            }            
            
            ans.getsize();
            return ans;
        }
        
        void operator+=(const _int8192 a){
            *this=*this+a;
        }
        
        void operator-=(const _int8192 a){
            *this=*this-a;
        }
        
        void operator++(){ //前置++
            *this=*this+_8192int(1);
        }
        
        _int8192 operator++(int){ //后置++
            _int8192 *ret=this;
            ++*this;
            return *ret;
        }
        
        void operator--(){ //前置--
            *this=*this-_8192int(1);
        }
        
        _int8192 operator--(int){ //后置--
            _int8192 *ret=this;
            --*this;
            return *ret;
        }
        
        _int8192 operator*(const _int8192 a) const{ //高精度乘法
            _int8192 ans;
            ans.init();
            ans.porn=(porn==a.porn)?true:false;
            
            for(register int i=0; i<size; i++){
                for(register int j=0; j<a.size; j++){
                    ans.num[i+j]+=num[i]*a.num[j];
                } 
            }            
            for(register int i=0; i<BIT; i++){
                ans.num[i+1]+=ans.num[i]/10;
                ans.num[i]%=10;
            }
            
            ans.getsize();
            return ans;
        }
        
        _int8192 operator/(const _int8192 a) const{ //高精度除法
            if(a==_8192int(0)){
//                cout<<"Runtime Error! Zero(0) cannot be a divisor!";
                _int8192 re;
                re.init();
                re.num[0]=-8;
                return re;
            }
            
            _int8192 ans;
            ans.init();
            
            _int8192 _this=*this;
            _int8192 b=a;
            _this=_this.abs();
            b=b.abs();
            ans=_8192int(0);
            while(_this>=b){
                _this-=b;
                ans++;
            }
            
            ans.porn=ans.porn=(porn==a.porn)?true:false;
            ans.getsize();
            return ans;
        }
        
        _int8192 operator%(const _int8192 a) const{ //高精度MOD
            if(a==0){
//                cout<<"Runtime Error! Zero(0) cannot be a divisor!";
                _int8192 re;
                re.init();
                re.num[0]=-8;
                return re;
            }
            _int8192 ans;
            ans.init();
            
            _int8192 _this=*this;
            _int8192 b=a;
            _this=_this.abs();
            b=b.abs();
            while(_this>=b) _this-=b;
            
            if(porn==a.porn) ans=_this;
            else ans=b-_this;
            ans.porn=a.porn;
            
            ans.getsize();
            return ans;
        }
        
        void operator*=(const _int8192 a){
            *this=*this*a;
        }
        
        void operator/=(const _int8192 a){
            *this=*this/a;
        }
        
        void operator%=(const _int8192 a){
            *this=*this%a;
        }
        
       //以下operator函数为两个操作数中一个为int时的操作
        _int8192 operator+(const int a) const{
            return *this+_8192int(a);
        }
        
        _int8192 operator-(const int a) const{
            return *this-_8192int(a);
        }
        
        _int8192 operator*(const int a) const{
            return *this*_8192int(a);
        }
        
        _int8192 operator/(const int a) const{
            return *this/_8192int(a);
        }
        
        _int8192 operator%(const int a) const{
            return *this%_8192int(a);
        }
        
        bool operator<(const int a) const{
            return *this<_8192int(a);
        }
        
        bool operator>(const int a) const{
            return *this>_8192int(a);
        }
        
        bool operator==(const int a) const{
            return *this==_8192int(a);
        }
        
        bool operator!=(const int a) const{
            return *this!=_8192int(a);
        }
        
        bool operator>=(const int a) const{
            return *this>=_8192int(a);
        }
        
        bool operator<=(const int a) const{
            return *this<=_8192int(a);
        }
        
        void operator+=(const int a){
            *this+=_8192int(a);
        }
        
        void operator-=(const int a){
            *this-=_8192int(a);
        }
        
        void operator*=(const int a){
            *this*=_8192int(a);
        }
        
        void operator/=(const int a){
            *this/=_8192int(a);
        }
        
        void operator%=(const int a){
            *this%=_8192int(a);
        }
        
        void operator=(int a){
            *this=_8192int(a);
        }
        
        friend _int8192 operator+(int a,_int8192 b){
            return _8192int(a)+b;
        }
        
        friend _int8192 operator-(int a,_int8192 b){
            return _8192int(a)-b;
        }
        
        friend _int8192 operator*(int a,_int8192 b){
            return _8192int(a)*b;
        }
        
        friend _int8192 operator/(int a,_int8192 b){
            return _8192int(a)/b;
        }
        
        friend _int8192 operator%(int a,_int8192 b){
            return _8192int(a)%b;
        }
        
        _int8192 pow(_int8192 b){ //幂
            if(b==0 && *this!=0) return _8192int(1);
            else if(b==0 && *this==0){
//                printf("Runtime Error! Zero(0) to the power of zero(0) is undefined!");
                _int8192 re;
                re.init();
                re.num[0]=-9;
                return re;
            }
            
            bool mui=true;
            label:
            _int8192 ret=*this;
            if(b.porn==true){
                for(_int8192 i=2; i<=b; i++){
                    ret*=*this;
                }
            }
            else{
                b=b.opp();
                mui=false;
                goto label;
            }
            
            if(mui==false) ret=1/ret;
            ret.getsize();
            return ret;
        }
        
        _int8192 pow(int b){
            return pow(_8192int(b));
        }
        
        _int8192(){
            init();
            porn=true;
        }
        
        _int8192(int n){
            *this=_8192int(n);
            porn=true;
        }
        
        friend ostream& operator<<(ostream &os,const _int8192 &n){ //重载cout<<
            ios::sync_with_stdio(false);
            if(n.num[0]==-8){
                os<<"Runtime Error! Zero(0) cannot be a divisor!";
                return os;
            }
            if(n.num[0]==-9){
                os<<"Runtime Error! Zero(0) to the power of zero(0) is undefined!";
                return os;
            }
            
            _int8192 b=n;
            if(b.PrintBool()==true){
                os<<"0";
                return os;
            }
            if(n.porn==false) os<<"-";
            int p=n.size-1;
            while(p!=-1){
                os<<n.num[p];
                p--;
            }
            return os;
        }
        
        friend istream& operator>>(istream &is,_int8192 &n){ //重载cin>>
            ios::sync_with_stdio(false);
            n.init();
            stack<short> st;
            n.size=0;
            memset(n.num,0,sizeof(num));
            char a;
            int x;
            int p=0;
            bool c=true;
            bool fr0=true;
            a=getchar();
            while(1){
                if(!((a>='0' && a<='9') || a=='-') || (c==false && a=='-')) break;
                
                if(a=='-'){
                    n.size--;
                    n.porn=false;
                }
                else if(fr0==false) st.push(a-'0');
                else if(fr0==true && a!='0'){
                    st.push(a-'0');
                    fr0=false;
                }
                else n.size--;
                
                if(c==true) c=false;
                n.size++;
                a=getchar();
            }
            
            for(register int i=0; i<n.size; i++){
                n.num[i]=st.top();
                st.pop();
            }
            if(n.size==0){
                n.size=1;
                n.num[0]=0;
            }
            
            return is;
        }
};

int main(){
    _int8192 n,m; //声明
    cin>>n>>m; //使用
    cout<<n<<"+"<<m<<"="<<n+m<<"\n";
    cout<<n<<"-"<<m<<"="<<n-m<<"\n"; //输出符号的问题无关紧要
    cout<<n<<"*"<<m<<"="<<n*m<<"\n";
    cout<<n<<"/"<<m<<"="<<n/m<<"\n";
    cout<<n<<"%"<<m<<"="<<n%m<<"\n";
    cout<<n<<"^"<<m<<"="<<n.pow(m);
    
    return 0; //结束,一共10行,太“短”了
}
原文