高精度加减乘除类的实现

10 篇文章 0 订阅
9 篇文章 3 订阅

信息安全原理课程

作业2,题1

Write a +-*/ algorithm for large integers.


本来是想用python来实现的,但python中没有数组这个类型,列表(list)感觉不能胜任这项工作

C++这么低层(抽象程度低)的语言来写还是非常不错的。

其实python中可以直接计算表达式,这个功能非常强大,我就是用python跑的结果来测试C++这个类的

我不是很清楚C++的单元测试怎么用,这周时间比较紧张,我希望下次能了解C++的单元测试,这样测试程序自动化会令人感觉非常爽。


// name: bigint.h
// author: amrzs
// date: 2014/03/11

#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();
    int *getNum();    
    Bigint getPow10(int);

    Bigint operator+(Bigint &);
    Bigint operator-(Bigint &);
    Bigint operator*(Bigint &);
    Bigint operator/(Bigint &);

    bool operator>=(Bigint &);

    void clear();

    void printNum();
};

#endif // BIGINT_H


// name: bigint.cpp
// author: amrzs
// date: 2014/03/11

#include <cstring>
#include <iostream>

#include "bigint.h"

using namespace std;

Bigint::Bigint(){
    //make sure to be zero(0)

    size = 1;
    arr[0] = 0; 
}

Bigint::Bigint(string &s){

    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;
}

int *Bigint::getNum(){

    return arr;
}

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;
        }

    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.clear(); //make sure arr[i] of result must be 0
    result.size = size + x.size - 1;
    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;    
    result.clear(); //make result being zero 
    
    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::getPow10(int n){

    if(size+n >= MAX_SIZE || n < 0)
        cout << "Out of range" << endl;

    Bigint x;
    x.clear();
    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
}


void Bigint::clear(){

    size = 1;
    memset(arr, 0, sizeof(*arr) * MAX_SIZE);
}

void Bigint::printNum(){

    cout << size << endl;
    for (int i = size-1; i >= 0; i--)
        cout << arr[i];
    cout << endl;
}



附加一个main.cpp使用这个类做一些事情


// name: main.cpp
// author: amrzs
// date: 2014/03/11

#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;
        default:
            cout << "You input a wrong operator" << endl;
            break;
    }
    
    return result;
}


int main(){

    string s1, s2;
    char c;
    cin >> s1 >> c >> s2; // 2 operands and 1 operator

    while(s1!="quit"){

        Bigint a(s1), b(s2);
        Bigint result = calc(a, b, c);
        result.printNum();
        
        cin >> s1 >> c >> s2;
    }

    return 0;
}


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)
 


写完程序几点感想与不足:

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

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

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



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值