|
楼主 |
发表于 2003-6-22 23:03:27
|
显示全部楼层
我把源程序中必要的部分传上来
const int BASE=10;
class hInt
{
private:
int sign;
int length;
char * number;
void new_number(int);
void check();
void abs_add(const hInt &,const hInt &);
void abs_sub(const hInt &,const hInt &);
int abs_bes(const hInt &);
public:
hInt();
hInt(char *);
~hInt();
void to_str(char *);
void operator=(const hInt &);
void operator=(char *);
hInt friend & operator+(const hInt &,const hInt &);
};
//private
void hInt::new_number(int i)
{
delete []number;
number=new char[i+1]; //note:char is not necessary
number=0; //but let char=0 can make it easy to watch
}
void hInt::check()
{
if(length<0) return; //error, maybe no chance to happen
if(length==0) { sign=0; return; }
char * p=number+length-1;
while((*(p--)==0)&&(length>0)) length--;
if(length==0) sign=0;
}
void hInt::abs_add(const hInt & ha,const hInt & hb)
{
//length and number[] will be made up here, but nothing to do with sign
int l1,l2;
char * p1, * p2;
if(ha.length>hb.length)
{ l1=ha.length; l2=hb.length; p1=ha.number; p2=hb.number; }
else
{ l2=ha.length; l1=hb.length; p2=ha.number; p1=hb.number; }
length=l1+1;
new_number(length);
char * p=number;
int i;
char t=0;
for(i=0;i<l2;i++)
{
t+=*(p1++)+*(p2++);
if(t>=BASE) { *(p++)=t-BASE; t=1; }
else { *(p++)=t ; t=0; }
}
for(;i<l1;i++)
{
t+=*(p1++);
if(t>=BASE) { *(p++)=t-BASE; t=1; }
else { *(p++)=t ; t=0; }
}
*p=t;
}
void hInt::abs_sub(const hInt & ha,const hInt & hb)
{
//abs(ha) mustn't be smaller than abs(hb)
int l1=ha.length, l2=hb.length;
char * p1=ha.number, * p2=hb.number;
length=l1;
new_number(length);
char * p=number;
int i;
char t=0;
for(i=0;i<l2;i++)
{
t+=*(p1++)-*(p2++);
if(t>=0) { *(p++)=t ; t= 0; }
else { *(p++)=t+BASE; t=-1; }
}
for(;i<l1;i++)
{
t+=*(p1++);
if(t>=0) { *(p++)=t ; t= 0; }
else { *(p++)=t+BASE; t=-1; }
}
}
int hInt::abs_bes(const hInt & h)
{
if(length>h.length) return 1;
if(length<h.length) return -1;
char * p1=number+length-1, * p2=h.number+length-1;
for(int i=0;i<length;i++)
{
if(*p1>*p2) return 1;
if(*p1<*p2) return -1;
p1--; p2--;
}
return 0;
}
//public
hInt::hInt()
{
number=new char[1];
}
hInt::hInt(char * s)
{
number=new char[1];
*this=s;
}
hInt::~hInt()
{
delete []number;
}
void hInt::to_str(char * s)
{
char * p=number+length-1;
if(sign==-1) *(s++)='-';
for(int i=0;i<length;i++) *(s++)=*(p--)+'0';
if(i==0) *(s++)='0';
*s=0;
}
void hInt:perator=(const hInt & h)
{
sign=h.sign;
length=h.length;
new_number(length);
char * p1=number;
char * p2=h.number;
for(int i=0;i<length;i++) *(p1++)=*(p2++);
}
void hInt:perator=(char * s)
{
while(*s==' ') s++;
sign=1;
if(*s=='-') { sign=-1; s++; }
if(*s=='+') s++;
length=0;
while((s[length]>='0')&&(s[length]<='9')) length++;
if(length==0) return; //error
new_number(length);
for(int i=0;i<length;i++) number[length-i-1]=s-'0';
check();
}
hInt & operator+(const hInt & ha,const hInt & hb)
{
hInt * h;
h=new hInt;
if(hb.sign==0) { *h=ha; return *h; }
if(ha.sign==0) { *h=hb; return *h; }
if(ha.sign==hb.sign)
{
h->sign=ha.sign;
h->abs_add(ha,hb);
h->check();
return *h;
}
else
{
int t=ha.abs_bes(hb);
if(t==0)
{
h->sign=0;
h->length=1;
h->new_number(1);
*h->number=0;
return *h;
}
if(t==1)
{
h->sign=ha.sign;
h->abs_sub(ha,hb);
h->check();
return *h;
}
h->sign=hb.sign;
h->abs_sub(hb,ha);
h->check();
return *h;
}
}
//---------------------------------------------------------------------------
#include <iostream.h>
void set_str(char * t,const char * f)
{
while(*(t++)=*(f++)) ;
}
void main()
{
cout<<"test begin"<<endl;
char s[256];
hInt h1("1"),h2("9"),h3;
h3=h1+h2;
h3.to_str(s);
cout<<s<<endl;
cout<<"test end"<<endl;
}
//--------------------------------------------------------------------------- |
|