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

信息安全原理

作业3 

HW2. Large number arithmetic
  • Write a +-*/ algorithm for large integers. (10 point)
  • Implement the DH algorithm. (5 point)

继承上个版本的,目前最新版,暂无bug

// 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)
 

用python生成的数据做过测试,不过python生成很大的幂的数要很长时间,所以只是用了小的幂的数据测试

随便输入一个几十位的幂的表达式,产生结果速度挺快的

bigint.cpp类的函数实现顺序有点乱

C++如果标准库有单元测试框架就好了,这样写程序做测试就方便多了,还能合作开发


这个程序写了好久,挺累的,没想到自己水平还不咋地。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值