高级程序员教程上的一个问题,长整数除普通整数,结果似乎不对。

menghui1 2003-08-20 06:02:58
长整数除普通整数函数
长整数除普通整数函数div(int *a,int j)实现将存于a中德长整数除普通整数j,并将商存于a中,函数返回整除后的余数。

int divint(int *a,int j)
{ int p,k;
k=p=a[0];a[0]=0;
while(p>0)
{
a[p-1]+=a[p]%j*10;
a[p]=a[p]/j;
if(a[k]==0) k--;
p--;
}
p=a[0]; /* 是否应为p=a[0]/10?
a[0]=k;
formal(a); /* 长整数规整函数
retrun p;
}

我认为函数返回的整除以后的余数不对,为应该值的10倍,不知是书上错了,还是我考虑不全,请指教!在线等。
...全文
72 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Chrisma 2003-08-21
  • 打赏
  • 举报
回复
#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);
}

mmmcd 2003-08-20
  • 打赏
  • 举报
回复
我也感觉书上错了。

当p=1,
a[p-1]+=a[p]%j*10; => a[0]= 0 + a[1]%j * 10;

其实 a[1]%j 就已经是余数了

wangmin_yjitx 2003-08-20
  • 打赏
  • 举报
回复
A[0]代表的是位数。

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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