请帮忙测试:一个“不受机器字长限制”的INT类。

Chrisma 2003-07-08 10:17:38
本人刚完成了华中科技大学新版的教材《C++程序设计实践教程》的习题,请大家
帮忙测试以下是否正确?关于乘法和除法,哪位大虾给出更高效的算法。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <exception.h>
class DEC{
char *n; //存放十进制数
public:
DEC(long);
DEC(char *d);
DEC(DEC const &d);
virtual ~DEC( );
virtual DEC operator-( )const;
virtual DEC operator+(const DEC &d) const;
virtual DEC operator-(const DEC &d) const;
virtual DEC operator*(const DEC &d) const;
virtual DEC operator/(const DEC &d) const;
virtual DEC operator%(const DEC &d)const;
virtual int operator>(const DEC &d)const;
virtual int operator<(const DEC &d)const;
virtual int operator==(const DEC &d)const;
virtual DEC &operator=(const DEC &d);
virtual DEC operator++(int);
virtual DEC operator--(int);
virtual DEC &operator--( );
virtual DEC &operator++( );
virtual operator const char *( )const;
};
DEC::DEC(long m){
char s[100];
ltoa(m, s, 10);
DEC::n=new char[strlen(s)+1];
strcpy(DEC::n, s);
}
DEC::DEC(char *n){
DEC::n=new char[strlen(n)+1];
if(n[0]=='+') strcpy(DEC::n, n+1);
else{
if(n[0]=='-'&&n[1]=='0') strcpy(DEC::n, n+1);
else strcpy(DEC::n, n);
}
}
DEC::DEC(const DEC &d){
if(n=new char[strlen(d.n)+1]) strcpy(n, d.n);
}
DEC::~DEC( ){ if(n) { delete n; n=0; } }
DEC DEC::operator-( )const{
char *t=new char [strlen(n)+2];
if(n[0]=='-') strcpy(t, n+1);
else strcat(strcpy(t,"-"),n);
DEC r(t);
delete t;
return r;
}
DEC &DEC::operator=(const DEC &d){
delete n;
if(n=new char[strlen(d.n)+1]) strcpy(n, d.n);
return *this;
}
int DEC::operator>(const DEC &d)const{
int h=strlen(n),k=strlen(d.n);
switch(n[0]){
case '-':
switch(d.n[0]){
case '-':
if(h==k){
for(k=0; k<h; k++){
if(n[k]==d.n[k]) continue;
if(n[k]<d.n[k]) return 1;
return 0;
}
return 0;
}
if(h<k) return 1;
return 0;
default :
return 0;
};
default :
switch(d.n[0]){
case '-':
return 1;
default :
if(h==k){
for(k=0; k<h; k++){
if(n[k]==d.n[k]) continue;
if(n[k]<d.n[k]) return 0;
return 1;
}
return 0;
}
if(h<k) return 0;
return 1;
}
}
}
int DEC::operator< (const DEC &d)const{ return d>*this || d==*this; }
int DEC::operator==(const DEC &d)const{ return strcmp(n, d.n)==0;}

DEC DEC::operator+(const DEC &d) const{
switch(n[0]){
case '-':
switch(d.n[0]){
case '-':
return -(-*this+(-d));
default :
return d-(-*this);
};
default :
switch(d.n[0]){
case '-':
return *this-(-d);
default :
int m, c=0, p=strlen(n), q=strlen(d.n);
if(p>q) m=p; else m=q; m+=2;
char *t=new char[m]; t[--m]=0;
while (p>0 && q>0){
c=c+n[--p]+d.n[--q]-2*'0';
t[--m]=((c>=10)?c-10: c)+'0';
c=(c>=10)? 1: 0;
}
while (p>0){
c=c+n[--p]-'0';
t[--m]=((c>=10)?c-10: c)+'0';
c=(c>=10)?1: 0;
}
while (q>0){
c=c+d.n[--q]-'0';
t[--m]=((c>=10)?c-10: c)+'0';
c=(c>=10)?1: 0;
}
if(c>0) {t[--m]='1'; }
DEC x(t+m);
delete t;
return x;
};
};
}
DEC DEC::operator-(const DEC &d) const{
switch(n[0]){
case '-':
switch(d.n[0]){
case '-':
return (-d)-(-*this);
default :
return -(-*this+d);
};
default :
switch(d.n[0]){
case '-':
return *this+(-d);
default :
if(d>*this) return -(d-*this);
int m, k, c=0, p=strlen(n), q=strlen(d.n);
if(p>q) m=p; else m=q; k=m+=2;
char *t=new char[m]; t[--m]=0;
while (p>0 && q>0){
c=n[--p]-d.n[--q]-c;
t[--m]=((c<0)?c+10: c)+'0';
c=(c<0)? 1: 0;
}
while (p>0){
c=n[--p]-'0'-c;
t[--m]=((c<0)?c+10: c)+'0';
c=(c<0)? 1: 0;
}
while (q>0){
c='0'-d.n[--q]-c;
t[--m]=((c<0)?c+10: c)+'0';
c=(c<0)? 1: 0;
}
if(c>0) {t[--m]='-'; }
else for(k=k-2; m<k && t[m]=='0'; m++);
DEC x(t+m);
delete t;
return x;
};
};
}
DEC DEC::operator*(const DEC &d) const{
if(n[0]=='0' ||d.n[0]=='0') return DEC("0");
switch(n[0]){
case '-':
switch(d.n[0]){
case '-':
return (-d)*(-*this);
default :
return -(-*this*d);
};
default :
switch(d.n[0]){
case '-':
return -(*this*(-d));
default :
int v,w, c, p=strlen(n), q=strlen(d.n);
DEC r("0");
DEC b((p>q)?n:d.n);
DEC m((p>q)?d.n:n);
for(v=0, w=((p>q)?q:p); v<w; v++){
char *t=new char[strlen(r.n)+2];
r=strcat(strcpy(t, r.n), "0");
delete t;
for(c=m.n[v]-'0'; c>0; c--) r=r+b;
}
if (strlen(r.n)==0) return DEC("0");
return r;
}
}
}
DEC DEC::operator/(const DEC &d) const{
if(d.n[0]=='0') throw new exception;
switch(n[0]){
case '-':
switch(d.n[0]){
case '-':
return (-*this)/(-d);
default :
return -(-*this/d);
};
default :
switch(d.n[0]){
case '-':
return -(*this/(-d));
default :
if(d>*this) return DEC("0");
int v,w, c, p=strlen(n), q=strlen(d.n);
char *r=new char[p+1], *t=new char[p+1];
w=p-q+1;
for(v=0; v<q; v++) t[v]=n[v]; t[q]=0;
for(v=0; v<w; v++){
c=0;
while (DEC(t)>d) {strcpy(t, (DEC(t)-d).n); c++; }
r[v]=c+'0'; r[v+1]=0;
if(t[0]=='0') t[0]=n[q+v];
else{
c=strlen(t);
t[c]=n[q+v];
t[c+1]=0;
}
}
for(v=0; v<p && r[v]=='0'; v++);
DEC s(r+v);
delete r;
delete t;
return s;
}
}
}
DEC DEC::operator%(const DEC &d)const{ return *this-(*this/d)*d; }
DEC::operator const char *( ) const{ return n; }
DEC &DEC::operator++( ){ return *this=*this+"1"; }
DEC DEC::operator++(int){ DEC r(*this); ++(*this); return r; }
DEC &DEC::operator--( ){ return *this=*this-DEC("1"); }
DEC DEC::operator--(int){ DEC r(*this); --(*this); return r; }

void main(){
DEC w("1"), x("0"), y("1"), z("0");
for(int k=1; k<5; k++) w=w*k;
printf("%s\n", w);
for(int k=1; k<5; k++) x=x+DEC(k);
printf("%s\n", x);
for(x=1; x<DEC("1000000000000000000000000"); x++) y=y*x;
printf("%s\n", y);
for(x="1"; x<DEC("1000000000000000000000000"); x++) z=z+x;
printf("%s\n", z);
}
...全文
27 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiger999 2003-07-17
  • 打赏
  • 举报
回复
google BigInteger.cpp
liao2001 2003-07-17
  • 打赏
  • 举报
回复
sdfsdfsdfsdfsvcvxcvsdfdsfcvcxvsdf
xenix 2003-07-17
  • 打赏
  • 举报
回复
你没明白我的意思
这个程序用的是十进制,一个字节只能表示 0...9十个数字,如果换 256禁止,1字节可以表示 0...255共256个数字,你说那个效率高呢?
禁止的转换不用说了吧?
Chrisma 2003-07-09
  • 打赏
  • 举报
回复
没有考虑正负数
Chrisma 2003-07-08
  • 打赏
  • 举报
回复
上述程序是定长的。乘法效率高。
xenix 2003-07-08
  • 打赏
  • 举报
回复
效率低,为啥不用256进制呢?
ZhangYv 2003-07-08
  • 打赏
  • 举报
回复
程序我没看,0分也叫别人测也太小气了点.这是我写的高精度运算,自己看吧
#include <iostream.h>
#include <mem.h>
const int MAXSIZE = 20; //max length of the number
const int K = 2; //baniry system
class hp{
int len; //length of number
int s[MAXSIZE]; //store high precistion number
public:
hp();
hp hp::operator = (hp C);
};
hp::hp()
{
len = 0;
memset(s, 0, MAXSIZE*sizeof(int));
}
istream &operator >> (istream &in, hp &HP)
{
char s[MAXSIZE];
int i;
cout << "Input Number = ";
cin >> s;
HP.len = strlen(s);
for (i = 0; i < HP.len; i++)
HP.s[i] = s[HP.len-i-1] - '0'; //change string to high precisition
return in;
}
ostream &operator << (ostream &out, hp &HP)
{
int i;
for (i = HP.len-1; i >= 0; i--)
cout << HP.s[i];
return out;
}
hp operator +(hp A, hp B)
{
int i, len;
hp C;
if (A.len > B.len)
len = A.len;
else
len = B.len; //get the bigger length of A,B
for (i = 0; i < len; i++){
C.s[i] += A.s[i] + B.s[i];
if (C.s[i] >= K){
C.s[i] -= K;
++C.s[i+1]; //add 1 to a higher position
}
}
if (C.s[len] > 0)
C.len = len+1;
else
C.len = len;
return C;
}
hp operator - (hp A, hp B) //different of the two HighPrecision Numbers
{
int len, i;
hp C;
if (A.len > B.len)
len = A.len;
else
len = B.len;C.len = 4;
for (i = 0; i < len; i++){
C.s[i] += A.s[i] - B.s[i];
if (C.s[i] < 0){
C.s[i] += K;
--C.s[i+1]; //subtract 1 to higher position
}
}
while (C.s[len-1] == 0 && len > 1)
--len;
C.len = len;
return C;
}
hp operator * (const hp &A, const hp &B)
{
int len, i, j;
hp C;
for (i = 0; i < A.len; i++)
for (j = 0; j < B.len; j++){
len = i+j;
C.s[len] += A.s[i] * B.s[j];
C.s[len+1] += C.s[len] / K;
C.s[len] %= K;
}
len = A.len + B.len + 1;
/*
the product of a number with i digits and a number with j digits
can only have at most i+j+1 digits
*/
while (len > 1 && C.s[len-1] == 0)
--len;
C.len = len;
return C;
}
hp hp::operator = (hp C)
{
int i;
len = C.len;
for (i = 0; i < MAXSIZE; i++)
s[i] = C.s[i];
return *this;
}
int main()
{
hp A, B, C;
cin >> A >> B;
C = A+B;
cout << A << ‘+’ << B << “ = “ << C << endl;
C = A-B;
cout << A << ‘-’ << B << “ = “ << C << endl;
C = A*B;
cout << A << '*' << B << " = " << C << endl;
return 0;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧