5.15作业
一 问题描述
定义一个整数型大数类,要求能够完成100位以内的较大整数的加法和减法计算,main(void)函数完成对其的测试。
二 设计思路
1.利用字符数组按位存储 第一位为正负号
2.利用cstring头文件 函数进行运算
三 代码
#include <iostream>
using namespace std;
#define N 100
#include<cstring>
class·BigNum
{
private:
····char·num[N];
public:
····BigNum(·char·c[N]·=·"+0"·);
····BigNum(·const·BigNum·&p·);
····~BigNum()
····{
········cout<<"BigNum·Destructor·run"<<endl;
····}
····void·setNum(·char·c[N]·);
····char·const·*·getNum(void)·const;
····BigNum·operator·+·(·const·BigNum·&a·);
····BigNum·operator·-·(··BigNum·&b·);
····void·show()·const;
};
·
·
BigNum::BigNum(·char·c[N]·)
{
····int·i,j,a·=·strlen(c);
····if(·c[0]·!=·'-')
····{
········num[0]·=·'+';
········if(·c·)
········{
············for(·i=strlen(c),·j=0;·i>=1;·i--,j++)
············{
················num[i]·=·c[j];
············}
········}
········num[a+1]·=·'\0';
····}
····else
····{
········num[0]·=·c[0];
········for(·i=strlen(c)-1,·j=1;·i>=1;·i--,j++)
········{
············num[i]·=·c[j];
········}
········num[a]·=·'\0';
····}
····cout<<"BigNum·Constructor·run"<<endl;
}
·
BigNum::BigNum(·const·BigNum·&p·)
{
····int·i=0;
····if(p.num)
····{
········while(·p.num[i]·!=·'\0'·)
········{
············num[i]·=·p.num[i];
············i++;
········}
········num[i]·=·'\0';
····}
}
//主函数
int main(void){
char c[100],op;
cin>>c;
BigNum b1(c),b2(b1);
b1.show();
cin>>c;
b2.setNum(c);
b2.show();
cin>>op;
if(op=='+') (b1+b2).show();
if(op=='-') (b1-b2).show();
return 0;
}