关于“重载型高精度算法”

God·Hero

2021-08-19 20:54:05

Personal

一个适用于正负高精度算法的模板(原创)

想拿就自己拿吧,省的再写高精度了。

使用方法见代码下方

struct node{
    int num[10005],len;
    bool Positive_and_negative_attributes,Error;
    node(){//初始化 
        len = 1;
        memset(num,0,sizeof(num));
        Positive_and_negative_attributes = 1;
        Error = 0;
    }
    /*逻辑运算部分begin*/
    bool operator>(node b){
        if(Positive_and_negative_attributes != b.Positive_and_negative_attributes)
            return Positive_and_negative_attributes > b.Positive_and_negative_attributes;
        if(Positive_and_negative_attributes == 0){
            if(len > b.len)
                return 0;
            if(len < b.len)
                return 1;
            for(int i = len;i >= 1;i --){
                if(num[i] > b.num[i])
                    return 0;
                if(num[i] < b.num[i])
                    return 1;
            }
            return 0;
        }
        if(len > b.len)
            return 1;
        if(len < b.len)
            return 0;
        for(int i = len;i >= 1;i --){
            if(num[i] > b.num[i])
                return 1;
            if(num[i] < b.num[i])
                return 0;
        }
        return 0;
    }
    bool operator>(int b){
        node c;
        c = b;
        if(*this > c)
            return 1;
        return 0;
    }
    bool operator<(node b){
        if(Positive_and_negative_attributes != b.Positive_and_negative_attributes)
            return Positive_and_negative_attributes < b.Positive_and_negative_attributes;
        if(Positive_and_negative_attributes == 0){
            if(len > b.len)
                return 1;
            if(len < b.len)
                return 0;
            for(int i = len;i >= 1;i --){
                if(num[i] > b.num[i])
                    return 1;
                if(num[i] < b.num[i])
                    return 0;
            }
            return 0;
        }
        if(len > b.len)
            return 0;
        if(len < b.len)
            return 1;
        for(int i = len;i >= 1;i --){
            if(num[i] > b.num[i])
                return 0;
            if(num[i] < b.num[i])
                return 1;
        }
        return 0;
    }
    bool operator<(int b){
        node c;
        c = b;
        if(*this < c)
            return 1;
        return 0;
    }
    bool operator==(node b){
        if(len != b.len || Positive_and_negative_attributes != b.Positive_and_negative_attributes)
            return 0;
        for(int i = len;i >= 1;i --)
            if(num[i] != b.num[i])
                return 0;
        return 1;
    }
    bool operator==(int b){
        node c;
        c = b;
        if(*this == c)
            return 1;
        return 0;
    }
    bool operator>=(node b){
        if(*this > b || *this == b)
            return 1;
        return 0;
    }
    bool operator>=(int b){
        node c;
        c = b;
        return *this >= c;
    }
    bool operator<=(node b){
        if(*this < b || *this == b)
            return 1;
        return 0;
    }
    bool operator<=(int b){
        node c;
        c = b;
        return *this <= c;
    }
    bool operator!=(node b){
        if(*this == b)
            return 0;
        return 1;
    }
    bool operator!=(int b){
        node c;
        c = b;
        return *this != c;
    }
    /*逻辑运算部分end*/
    /*算数运算部分begin*/ 
    void operator++(){
        *this = *this + 1;
    }
    void operator++(int){
        *this = *this + 1;
    }
    void operator--(){
        *this = *this - 1;
    }
    void operator--(int){
        *this = *this - 1;
    }
    void operator+=(node b){
        *this = *this + b;
    }
    void operator+=(int b){
        *this = *this + b;
    }
    void operator-=(node b){
        *this = *this - b;
    }
    void operator-=(int b){
        *this = *this - b;
    }
    void operator*=(node b){
        *this = *this * b;
    }
    void operator*=(int b){
        *this = *this * b;
    }
    void operator/=(node b){
        *this = *this / b;
    }
    void operator/=(int b){
        *this = *this / b;
    }
    void operator%=(node b){
        *this = *this % b;
    }
    void operator%=(int b){
        *this = *this % b;
    }
    void operator=(int b){
        node c;
        if(b < 0){
            c.Positive_and_negative_attributes = 0;
            b *= -1;
        }
        while(b != 0){
            c.num[c.len] = b % 10;
            b /= 10;
            c.len ++;
        }
        c.rule();
        *this = c;
    }
    node operator+(int b){
        node c;
        c = b;
        return *this + c;
    }
    node operator+(node b){
        node c;
        if(Positive_and_negative_attributes == 0 || b.Positive_and_negative_attributes == 0){
            if(Positive_and_negative_attributes == b.Positive_and_negative_attributes)
                c.Positive_and_negative_attributes = 0;
            else{
                if(Abs() > b.Abs()){
                    if(Positive_and_negative_attributes == 0){
                        Positive_and_negative_attributes = 1;
                        c = b - *this;
                        Positive_and_negative_attributes = 0;
                        c.Positive_and_negative_attributes = 0;
                        return c;
                    }
                    else{
                        b.Positive_and_negative_attributes = 1;
                        c = *this - b;
                        return c;
                    }
                }
                else {
                    if(Positive_and_negative_attributes == 0){
                        Positive_and_negative_attributes = 1;
                        c = b - *this;
                        Positive_and_negative_attributes = 0;
                        return c;
                    }
                    else{
                        b.Positive_and_negative_attributes = 1;
                        c = b - *this;
                        if(c != 0)
                            c.Positive_and_negative_attributes = 0;
                        return c;
                    }
                }
            }
        }
        c.len = std::max(b.len,len) + 1;
        for(int i = 1;i <= c.len;i ++){
            c.num[i] += num[i] + b.num[i];
            c.num[i + 1] = c.num[i] / 10;
            c.num[i] %= 10; 
        }
        c.rule();
        return c;
    }
    node operator-(int b){
        node c;
        c = b;
        return *this - c;
    }
    node operator-(node b){
        node c,d;
        if(Positive_and_negative_attributes == 0 || b.Positive_and_negative_attributes == 0){
            if(b.Positive_and_negative_attributes == 0){
                b.Positive_and_negative_attributes = 1;
                c = *this + b;
                return c;
            }   
            Positive_and_negative_attributes = 1;
            c = *this + b;
            Positive_and_negative_attributes = 0;
            c.Positive_and_negative_attributes = 0;
            return c;
        }
        d = *this;
        if(d < b){
            c.Positive_and_negative_attributes = 0;
            std::swap(d,b);
        }
        c.len = std::max(b.len,d.len);
        for(int i = 1;i <= c.len;i ++){
            if(d.num[i] < b.num[i]){
                d.num[i] += 10;
                d.num[i + 1] -= 1;
            }
            c.num[i] = d.num[i] - b.num[i];
        }
        c.rule();
        return c;
    }
    node operator*(int b){
        node c;
        c = b;
        return *this * c;
    }
    node operator*(node b){
        node c;
        if(Positive_and_negative_attributes != b.Positive_and_negative_attributes)
            c.Positive_and_negative_attributes = 0;
        c.len = len + b.len;
        for(int i = 1;i <= len;i ++){
            for(int j = 1;j <= b.len;j ++){
                c.num[i + j - 1] += num[i] * b.num[j];
                c.num[i + j] += c.num[i + j - 1] / 10;
                c.num[i + j - 1] %= 10;
            }
        }
        c.rule();
        return c;
    }
    node operator/(int b){
        node c;
        c = b;
        return *this / c;
    }
    node operator/(node b){
        if(b == 0){
            b.Error = 1;
            return b;
        }
        node c,d,e;
        e = *this;
        bool flag = false,b_Positive_and_negative_attributes = b.Positive_and_negative_attributes;
        int used_len;
        if(Abs() < b.Abs())
            return c;
        e.Positive_and_negative_attributes = b.Positive_and_negative_attributes = 1;
        d = e.Peek(1,b.len);used_len = b.len;
        while(used_len <= e.len){
            while(d < b){
                if(used_len < e.len)
                    d = d * 10 + e.Peek(++ used_len,used_len);
                else{
                    flag = true;
                    break;
                }
                c *= 10;
            }
            if(flag)
                break;
            while(d >= b){
                d -= b;
                c ++;
            }
        }
        if(Positive_and_negative_attributes != b_Positive_and_negative_attributes)
            c.Positive_and_negative_attributes = 0;
        c.Reverse();
        c.rule();
        return c;
    }
    node operator%(node b){
        return *this - (*this / b) * b;
    }
    node operator%(int b){
        node c;
        c = b;
        return *this % c;
    }
    /*算数运算部分end*/
    /*成员函数begin*/
    node Max(node b){
        if(*this > b)
            return *this;
        return b;
    }
    node Max(int b){
        node c;
        c = b;
        return Max(c);
    }
    node Min(node b){
        if(*this < b)
            return *this;
        return b;
    }
    node Min(int b){
        node c;
        c = b;
        return Min(c);
    } 
    node Abs(){
        node b;
        b = *this;
        b.Positive_and_negative_attributes = 1;
        return b;
    }
    node Pow(int b){
        node c,d;
        c = 1;
        if(b == 0 && *this != 0)
            return c;
        if(*this == 0)
            return d;
        d = *this;
        while(-- b)
            d *= *this;
        return d;
    }
    node Reverse(){
        node b = *this;
        int l = 1,r = b.len; 
        while(l < r){
            std::swap(b.num[l],b.num[r]); 
            l ++;r --;
        }
        return b;
    }
    node Peek(int b,int c){
        node d;
        d.len = c - b + 1;
        d.Positive_and_negative_attributes = Positive_and_negative_attributes;
        for(int i = b;i <= c;i ++)
            d.num[c - i + 1] = num[len - i + 1];
        d.rule();
        return d;
    }
    node Sqrt(){
        node l,r,mid,b;
        l = 0;
        r = *this;
        mid = (l + r) / 2;
        while(l < r - 1){
            b = mid.Pow(2);
            if(b == *this)
                break;
            else if(b > *this)
                r = mid;
            else 
                l = mid;
            mid = (l + r) / 2;
        }
        return mid;
    }
    void rule(){
        while(len != 1 && num[len] == 0) len --;
        if(len == 1 && num[1] == 0)
            Positive_and_negative_attributes = 1;
    }
    bool Prime(){
        if(*this == 1 || (*this % 2 == 0 && *this != 2))
            return 0;
        node c,d;
        d = Sqrt();
        for(c = 3;c <= d;c += 2) 
            if(*this % c == 0) 
                return 0;
        return 1;
    }
    /*成员函数end*/
    /*输入/出begin*/
    void Input(){
        std::string d;
        std::cin >> d;
        len = d.size();
        if(d[0] == '-'){
            Positive_and_negative_attributes = 0;
            len -= 1;
            for(int i = 1;i <= len;i ++)
                num[len - i + 1] = d[i] - '0';
            return;
        }
        for(int i = 0;i < len;i ++)
            num[len - i] = d[i] - '0';
        return;
    }
    void Output(){
        if(Error){
            printf("[Error] -> 运算过程中除数为0!\n");
            return;
        }
        if(!Positive_and_negative_attributes)
            printf("-");
        for(int i = len;i >= 1;i --)
            printf("%d",num[i]);
        return;
    }
    /*输入/出end*/
};

使用方法:

1、头文件需包含以下头文件:

#include <iostream>
#include <cstdio>
#include <cstring>

2、定义:

例:

node a;

("a"可更换为其他任意你想定义的参数名)

3、输入:

例:

a.Input();

("a"可更换为其他任意你定义过的参数名)(输入限制:仅可输入数字及负号)

4、输出:

例:

a.Output();

("a"可更换为其他任意你定义过的参数名)(输出内容:a所代表的数,若为负数会自行输出负号)

5、成员函数:

(1)比较该类型数的大小:

6、运算

(1)赋值

(转载请标明出处)

(转载请标明出处)

(转载请标明出处)