高精度板( 无除法、减法 )

本文介绍了一个自定义的大数类BIGNUM,实现了大数的基本运算,包括加法、乘法等,并提供了比较操作符。该类使用C++实现,能够处理超过标准整型范围的大数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
#include<ctime>
#include<climits>
#include<map>
#include<set>

using namespace std;

#define MAXM 205
#define INF 0x7f7f7f7f
const int MAXN = 2000;

struct BIGNUM{

    int len, s[MAXN];

    BIGNUM(){ len = 1 ; memset( s , 0 , sizeof(s)); }

    void get(){
        char num[MAXN];
        cin >> num;
        int n = strlen(num);
        for(len = 0; len < n; len++)
            s[len] = num[len] - '0';
        return ;
    }

    void print(){
        for(len --; len >= 0; len--)
            printf("%d", s[len]);
        return ;
    }

    BIGNUM operator = ( const char * num ){
        len = strlen(num);
        for( int i = 0 ; i < len ; ++ i ) s[i] = num[ len - i - 1 ] - '0';
        return * this;
    }

    BIGNUM operator  = ( const int num ){
        char a[MAXN];
        sprintf( a , "%d" , num );
        * this = a;
        return * this;
    }

    BIGNUM( const int num ){ * this = num; }

    BIGNUM( const char * num ){ * this = num; }

    BIGNUM operator + ( const BIGNUM & a ) const{
        BIGNUM c;
        c.len = max( len , a.len ) + 1;
        for( int i = 0 , x = 0; i <c.len ; ++ i ){
            c.s[i] = s[i] + a.s[i] + x;
            x = c.s[i] / 10;
            c.s[i] = c.s[i] % 10;
        }
        if( c.s[ c.len - 1 ] == 0 ) c.len --;
        return c;
    }

    BIGNUM operator += ( const BIGNUM & a ){
        * this = *this + a;
        return * this;
    }

    BIGNUM operator * ( const BIGNUM & x ){
        BIGNUM c;
        c.len = len + x.len;
        for( int i = 0 ; i < len ; ++ i )
            for( int j = 0 ; j < x.len ; ++ j){
                c.s[ i + j ] += s[i] * x.s[j];
                c.s[ i + j + 1 ] += c.s[ i + j ] / 10;
                c.s[ i + j ] %= 10;
            }
        if( c.s[ c.len - 1 ] == 0 ) c.len --;
        return c;
    }

    BIGNUM operator *= ( const BIGNUM & a ){
        * this = * this * a;
        return * this;
    }

    bool operator < ( const BIGNUM & x ) const {
        if( len != x.len ) return len < x.len;
        for( int i = len - 1 ; i >= 0 ; -- i ){
            if( s[i] != x.s[i] ) 
                return s[i] < x.s[i];
        }
        return 0;   
    }

    bool operator > ( const BIGNUM & x ) const { return x < * this; }

    bool operator <= ( const BIGNUM & x ) const { return !( x < *this ); }

    bool operator >= ( const BIGNUM & x ) const { return !( x > * this ); }

    bool operator == ( const BIGNUM & x ) const { return !( x < * this || * this < x ); }

    bool operator != ( const BIGNUM & x ) const { return x < * this || * this < x; }
    }a, b, f = INT_MAX, t;
    ostream & operator << ( ostream & out , const BIGNUM & x ){
    for( int i = x.len - 1 ; i >= 0 ; -- i )
        cout << x.s[i];
    return out; 
    }
    istream & operator >> ( istream & in , BIGNUM & x ){
    char num[MAXN];
    in >> num;
    x = num;
    return in;
    }
    int main(){
    return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值