美文网首页
高精度类的实现 加减乘除幂余

高精度类的实现 加减乘除幂余

作者: Amrzs | 来源:发表于2014-06-14 14:28 被阅读152次

    高精度类的实现 加减乘除幂余

    from my csdn blog

    信息安全原理 hw2-1

    • HW2. Large number arithmetic
      • Write a +-*/ algorithm for large integers. (10 point)
      • Implement the DH algorithm. (5 point)
    // name: bigint.h
    // author: amrzs
    // date: 2014/03/18
    
    #ifndef BIGINT_H
    #define BIGINT_H
    
    #include <string>
    
    using namespace std;
    
    
    class Bigint{
    
    private:
        static const int MAX_SIZE = 500;
    
        int size;
        int arr[MAX_SIZE];
    
    public:
        Bigint();
        Bigint(string &s); //initialize with a string num
        ~Bigint();
    
        int getSize();
    
        Bigint operator+(Bigint &);
        Bigint operator-(Bigint &);
        Bigint operator*(Bigint &);
        Bigint operator/(Bigint &);
        Bigint operator%(Bigint &);
    
        Bigint operator/(int x); 
    
        Bigint getPow(int n, Bigint &); //return this^n % x
        Bigint getPow(Bigint &, Bigint &);
        Bigint getPow10(int n); //return this*(10^n)
    
        bool operator>=(Bigint &);
    
        bool isOdd();
        bool equalOne();
    
        void clear();
    
        void printNum();
    };
    
    #endif // BIGINT_H
    
    // name: bigint.cpp
    // author: amrzs
    // date: 2014/03/18
    
    #include <cstring>
    #include <iostream>
    
    #include "bigint.h"
    
    using namespace std;
    
    Bigint::Bigint(){
        //make sure to be zero(0)
    
        clear();
    }
    
    Bigint::Bigint(string &s){
    
        clear();
        size = s.length();
    
        if (size > MAX_SIZE){
            
            //something wrong
            cout << "too many numbers" << endl;
        }
    
        for (int i = size-1; i >= 0; i--)
            arr[i] = s[size-i-1] - '0';        
    }
    
    Bigint::~Bigint(){
    
        //to do sth    
    }
    
    int Bigint::getSize(){
    
        return size;
    }
    
    
    Bigint Bigint::operator+(Bigint &x){
    
        Bigint result;
    
        result.size = max(size, x.size);
        for (int i = 0; i < result.size; i++)
            result.arr[i] = arr[i] + x.arr[i];
        for (int i = 0; i < result.size; i++)
            if (result.arr[i] > 9){
                result.arr[i+1] += result.arr[i] / 10;
                result.arr[i] %= 10;
            }
    
        if(result.size > MAX_SIZE){
            cout << "Out of range in operator+" << endl;
        }
    
        return result;
    }
    
    Bigint Bigint::operator-(Bigint &x){
    
        Bigint result = *this;
        
        for (int i = 0; i < x.size; i++)
            result.arr[i] = arr[i] - x.arr[i];
        for (int i = 0; i < result.size; i++)
            if (result.arr[i] < 0){
                result.arr[i] += 10;
                result.arr[i+1]--;
            }
    
        while(result.size > 1 && 0 == result.arr[result.size-1])    
            result.size--;
    
        return result;
    }
    
    Bigint Bigint::operator*(Bigint &x){
    
        Bigint result;
    
        result.size = size + x.size - 1;
        if(result.size > MAX_SIZE){
            cout << "Out of range in operator*" << endl;
        }
        for (int i = 0; i < size; i++)
            for (int j = 0; j < x.size; j++){
                result.arr[i+j] += arr[i] * x.arr[j];
                if (result.arr[i+j] > 9){
                    result.arr[i+j+1] += result.arr[i+j] / 10;
                    result.arr[i+j] %= 10;
                }
            }
        if (result.arr[result.size] > 0)
            result.size++;        
    
        return result;
    }
    
    Bigint Bigint::operator/(Bigint &x){
    
        Bigint result;    
        
        if(size < x.size)
            return result;
        
        Bigint y = *this, z;    
        int pow = y.size - x.size;
        while(y >= x){
            z = x.getPow10(pow);
            while(y >= z){
                y = y - z;
                result.arr[pow]++;
            }
            pow--;
        }
    
        result.size = size - x.size + 1;
        while(result.size > 1 && 0 == result.arr[result.size-1])
            result.size--;
    
        return result;
    }
    
    Bigint Bigint::operator/(int x){
    
        Bigint result;
        int tmp = 0;
    
        for(int i = size-1; i >= 0; i--){
            tmp = tmp * 10 + arr[i];
            result.arr[i] = tmp / x;
            tmp %= x;
        }
        
        result.size = size;
        while(result.size > 1 && 0 == result.arr[result.size-1])
            result.size--;
    
        return result;
    }
    
    Bigint Bigint::operator%(Bigint &x){
    
        Bigint t = *this / x * x;    
    
        return *this - t; 
    }
    
    Bigint Bigint::getPow(int n, Bigint &x){
    
        if(n < 1){
            cout << "Parameter out of range in operator^" << endl;
        }
    
        if(1 == n){
            return *this % x;
        }
    
        Bigint t = getPow(n/2, x);
    
        Bigint result = t * t % x;
        if(1 == n%2){
            result = *this * result % x;
        } 
    
        return result;
    }
    
    Bigint Bigint::getPow(Bigint &n, Bigint &x){
    
        if(n.equalOne()){
            return *this % x;
        }
    
        Bigint nDiv2 = n / 2;
        Bigint t = getPow(nDiv2, x);
    
        Bigint result = t * t % x;
        if(n.isOdd()){
            result = *this * result % x;
        }
    
        return result;
    }
    
    Bigint Bigint::getPow10(int n){
    
        if(size+n >= MAX_SIZE || n < 0)
            cout << "Out of range" << endl;
    
        Bigint x;
    
        for(int i = 0; i < size; i++)
            x.arr[i+n] = arr[i];
        x.size = size + n;
    
        return x;
    }
    
    bool Bigint::operator>=(Bigint &x){
    
        if(size < x.size)
            return false;
        
        if(size == x.size){
            for(int i = size-1; i >= 0; i--)
                if(arr[i] > x.arr[i])
                    return true;    //greater
                else if(arr[i] < x.arr[i])
                    return false;
            return true;    //equal
        }
    
        return true; //greater
    }
    
    bool Bigint::isOdd(){
    
        return (arr[0] & 1);
    }
    
    bool Bigint::equalOne(){
    
        return (1==size && 1==arr[0]);
    }
    
    void Bigint::clear(){
    
        size = 1;
        memset(arr, 0, sizeof(*arr) * MAX_SIZE);
    }
    
    void Bigint::printNum(){
    
        for (int i = size-1; i >= 0; i--)
            cout << arr[i];
        cout << endl;
    }
    
    // name: main.cpp
    // author: amrzs
    // date: 2014/03/18
    
    #include <string>
    #include <iostream>
    
    #include "bigint.h"
    
    using namespace std;
    
    Bigint calc(Bigint a, Bigint b, char c){
    
        Bigint result;
        switch(c){
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                result = a / b;
                break;
            case '%':
                result = a % b;
                break;
            default:
                cout << "You input a wrong operator" << endl;
                break;
        }
        
        return result;
    }
    
    
    int main(){
    
        cout <<"Please input a expression like 123456789 * 987654321"
            <<"if DH then like 123456789 ^ 987654321 % 123456789" << endl;
        
        string s1, s2, s3;
        char ch1, ch2;
    
        while(true){ // don't like for(;;)
            
            cin >> s1 >> ch1 >> s2;
            if("quit" == s1){
                break;
            }
    
            Bigint a(s1), b(s2), result;        
    
            if('^' == ch1){         //for DH 
                cin >> ch2 >> s3;
                Bigint c(s3);
                result = a.getPow(b, c);
            }else{                  //normal expression
                result = calc(a, b, ch1);
            }
    
            result.printNum();
        }
    
        return 0;
    }
    

    Makefile:

    CPP = g++
    OFLAG = -o
    TARGET = a.out
    OBJS = main.o bigint.o
    
    $(TARGET): $(OBJS)
        $(CPP) $(OFLAG) $(TARGET) $(OBJS)
    main.o: main.cpp bigint.o
    bigint.o: bigint.cpp
    
    .PHONY: clean
    clean:
        -rm $(TARGET) $(OBJS)
    

    不足

    • 以前写高精度,总是函数方式,当然这样不妥当,我都是将最低位放在arr[1]中,而这次是放在arr[0]中,这样造成了一些细微的差别使得程序写的时候发生了一些小问题

    • 要考虑到类的数组中没有清0这件事情,我总觉得清0不好,因为效率么,当然这就带来了编程的复杂性,总是被数组里的垃圾数据干扰,出现很多错误,使得11号写的程序13号才总算完成,以后的话,我宁愿降低一些效率,使程序逻辑简单一些

    • 学了一下gdb,发现听不错的,clang++和g++在调试的时候有些区别,最好还是用g++来调试程序,当然,生成可执行文件g++和clang++都不错,主要clang++的错误提示信息很友好

    相关文章

      网友评论

          本文标题:高精度类的实现 加减乘除幂余

          本文链接:https://www.haomeiwen.com/subject/grqjtttx.html