BigNum Add

#ifndef _ADD_H_
#define _ADD_H_

#include <iostream>
#include <stack>
#include <string>

using namespace std;

class BigNum;

ostream &operator<<(ostream &os, BigNum &bn);
istream &operator>>(istream &is, BigNum &bn);

BigNum &operator+(BigNum &la, BigNum &ra);

class BigNum
{
stack<char> value;
public:
BigNum(const BiugNum &bn)
{
this->value = bn.value;
}
friend ostream &operator<<(ostream &os, BigNum &bn);
friend istream &operator>>(istream &is, BigNum &bn);
friend BigNum &operator+(BigNum &la, BigNum &ra);
};

ostream &operator<<(ostream &os, BigNum &bn)
{
stack<char> temp(bn.value), out;

while(temp.size()>0)
{
out.push(temp.top());
temp.pop();
};

while(out.size()>0)
{
os << out.top();
out.pop();
};

return os;
}

istream &operator>>(istream &is, BigNum &bn)
{
string temp;
is >> temp;
int s = temp.size();
string::iterator it = temp.begin();
for(;it<temp.end();it++)
{
bn.value.push(*it);
}

return is;
}

bool addChar(char a, char b, char &c,int i)
{
char t;
c = a+b-'0'+i;
if((t=c-'9')>0)
{
c = c - 9;
return true;
}
else
{
return false;
}
}

BigNum operator+(BigNum &la, BigNum &ra)
{
bool b;
BigNum resualt;
stack<char> ta, tb, tr;
char a, b;
int i=0, j;

la.value().size>ra.value.size()?
{ta=la.value;tb=ra.value}:{ta=ra.value;tb=la.value}

while(ta.size())
{
if(tb.size())
i = addChar(ta.top(),tb.top(),a,i)?1:0;
else
i = addChar(ta.top(),'0',a,i)?1:0;

ta.pop();
tb.pop();

tr.push(a);
}
if(i)
tr.push('1');

while(tr.size())
{
resualt.value.push(tr.top());
tr.pop();
}
return resualt;
}

#endif //_ADD_H_

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值