万里长征:我终于迈出了第0.1步!初中就和靳柯讨论过这种无限位的大数加法怎么实现。当时啥都不懂啊,今天自己能实现出来了!虽然类还很不完善,但是已经有雏形了,高兴~~~~~
h
/*-------------------------------------------------------*
* CopyRight 2007 LiHeYuan at BeiJing JiaoTong University *
* *
* 日期:2007-3-23 * * *
* 文件名:HugeInt.h *
* *
* 描述:HugeInt类的定义文件,定义了加法接口 *
* *
---------------------------------------------------------*/
const int len=200;
class HugeInt
{
public:
//构造函数
HugeInt(int R);
HugeInt(){};
//加法接口
HugeInt operator +(HugeInt &R);
//输出接口
Print();
//公用接口
int Len(){return m_len;}
private:
int m_sign; //符号
int m_len; //长度
char m_num[len]; //存储空间
};
cpp
/*-------------------------------------------------------*
* CopyRight 2007 LiHeYuan at BeiJing JiaoTong University *
* *
* 日期:2007-3-23 *
* *
* *
* 文件名:HugeInt.cpp *
* *
* 描述:HugeInt类的实现文件,实现了加法接口 *
* *
---------------------------------------------------------*/
#include "HugeInt.h"
#include <iostream>
#include <cmath>
using namespace std;
HugeInt::Print()
{
int i;
for(i=m_len;i!=0;i--)
cout<<m_num[i]+0;
cout<<endl;
}
HugeInt::HugeInt(int R)
{
if(R!=0)
{
if(R>0)
m_sign=1;
else
m_sign=-1;
int i=0,k=1;
int abs_R=abs(R);
do
{
i++;
m_num[i]=abs_R%10;
abs_R/=10;
}while(abs_R);
m_len=i;
}
else
{
m_num[1]=0;
m_len=1;
m_sign=0;
}
}
HugeInt HugeInt::operator +(HugeInt &R)
{
HugeInt Result(0);
char *p,*q,*r;
p=q=r=NULL;
int len1,len2;
if(Len()>R.Len())
{
p=this->m_num;
q=R.m_num;
r=Result.m_num;
len1=Len();
len2=R.Len();
}
else
{
p=R.m_num;
q=this->m_num;
r=Result.m_num;
len1=R.Len();
len2=Len();
}
int i=1,j=1,k=1,carry=0;
while(j<=len2)
{
r[k]=p[i++]+q[j++]+carry;
carry=r[k]/10;
r[k]%=10;
k++;
}
while(i<=len1)
{
r[k]=p[i++]+carry;
carry=r[k]/10;
r[k]%=10;
k++;
}
if(carry>0)
{
r[k]=carry;
Result.m_len=k;
}
else
{
Result.m_len=k-1;
}
Result.m_sign=1;
return Result;
}
main
#include <iostream>
#include "HugeInt.h"
using namespace std;
int main()
{
HugeInt h1(99999),h2(99999),h3(0);
h3=h1+h2;
h3.Print();
return 0;
}