关于蝎子朋友的回复,大整数加减程序的不足尤其是蝎子请进!

winplum 2003-07-09 04:56:01
//减法时,result[0]是符号位;

/*
* 1、只考虑正整数
*/

#include <stdio.h>
#include <stdlib.h>

int a[100], b[100], result[101];

void parseInt( char *strNum, int array[] ) {
int ix;
for( ix=0; strNum[ix]!='\0'; ++ix )
array[ix]=strNum[ix]-'0';
array[ix]=-1; /*数字结束*/
}

void displayNum( int array[] ) {
int ix;
for( ix=0; array[ix]!=-1; ++ix )
printf( "%d", array[ix] );

printf("[%d]", ix);
}

int getStartPos( int array[] ) {
int ix=0;
while( array[ix]!=-1 )
++ix;

return ix-1;
}

void doCal( int numOne[], int numTwo[], int numResult[], char oper ) {
/* 从个位开始运算 */
int onePos, twoPos, resultPos; /* 记录位置 */
int carry; /* 进位 */
int *tp; /* 减法时可能用到 */

onePos = getStartPos( numOne );
twoPos = getStartPos( numTwo );
resultPos = (onePos>twoPos)?onePos:twoPos + 1; /* 多一位,以备进位用;

或保存符号 */
numResult[ resultPos+1 ] = -1;
carry = 0;

switch( oper ) {
case '+':
while( onePos>=0 || twoPos>=0 ) {
if( onePos>=0 ) {
numResult[ resultPos ] = numOne[onePos];
--onePos;
}
else
numResult[ resultPos ] = 0;

if( twoPos>=0 ) {
numResult[ resultPos ] += numTwo[twoPos]+carry;
--twoPos;
}
else
numResult[ resultPos ] += carry;

if( numResult[resultPos]>10 ) {
carry = numResult[resultPos]/10;
numResult[resultPos]=numResult[resultPos]%10;
} else
carry = 0;
--resultPos;
}
if( carry!=0 )
numResult[ resultPos ] = carry;
else
numResult[ resultPos ] = 0;
break;
case '-':
if( onePos<twoPos || (onePos==twoPos && numOne[0]<numTwo[0]) ) {
numResult[0]=1;
// doCal(numTwo, numOne, numResult, '-');
tp = numOne;
numOne = numTwo;
numTwo = tp;

carry=onePos;
onePos = twoPos;
twoPos = carry;
carry = 0;
} else
numResult[0]=0;

while( onePos>=0 || twoPos>=0 ) {
if( onePos>=0 ) {
numResult[ resultPos ] = numOne[onePos];
--onePos;
}
else
numResult[ resultPos ] = 0;

if( twoPos>=0 ) {
numResult[ resultPos ] -= numTwo[twoPos]+carry;
--twoPos;
}
else
numResult[ resultPos ] -= carry;

if( numResult[resultPos]<0 ) {
carry = 1;
numResult[resultPos] += 10;
} else
carry = 0;
--resultPos;
}
// numResult[ resultPos ] = 0;
break;
default:
break;
}

displayNum( a );
printf(" %c ", oper);
displayNum( b );
printf(" = ");
displayNum( result );
printf("\n");
}

void main() {
char *strNum;

a[0]=-1;
b[0]=-1;
result[0]=-1;

strNum=(char*)malloc( 100*sizeof(char) );
printf("Enter number a: ");
scanf("%s", strNum);
parseInt( strNum, a );

printf("Enter number b: ");
scanf("%s", strNum);
parseInt( strNum, b );


// doCal( a, b, result, '+' );
doCal( a, b, result, '-' );
}

蝎子朋友的程序很好,但我经过测试后发现两个主要问题:其一,当相同位数相加为整十进位有问题;二是相减被减数大余减数时有错,如果还在或是其他朋友有兴趣的话,烦请指教!
...全文
76 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ftp123 2003-07-12
  • 打赏
  • 举报
回复
大整数加减要这么多的代码吗
不超过20行啊 如果是用数组保存的话
你们想想看
idontlikenickname 2003-07-09
  • 打赏
  • 举报
回复


呵呵~我不是高程,
拿来看看~

robin97 2003-07-09
  • 打赏
  • 举报
回复
不用这么长吧?在高级程序员的教程上有一个很短的
idontlikenickname 2003-07-09
  • 打赏
  • 举报
回复


试试下边的大整数加法程序

#include<stdio.h>
#include<malloc.h>

#define HUN 10000

typedef struct node
{
int data;
struct node *next;
}NODE;

NODE *insert(NODE *u,int num) /*声明返回指针函数*/
{
NODE *v; /*声明结构指针*/
v=(NODE*)malloc(sizeof(NODE));
v->data=num;
u->next=v;
return(v);
}

NODE *addint(NODE *p,NODE *q)
{
NODE *pp,*qq,*r,*s,*t;
int total,number,carry;
pp=p->next;
qq=q->next;
s=(NODE*)malloc(sizeof(NODE));
s->data=-1;
t=s;
carry=0;
while(pp->data!=-1&&qq->data!=-1)
{
total=pp->data+qq->data+carry;
number=total%HUN;
carry=total/HUN;
t=insert(t,number);
pp=pp->next;
qq=qq->next;
}
r=(pp->data!=-1)?pp:qq;
while(r->data!=-1)
{
total=r->data+carry;
number=total%HUN;
carry=total/HUN;
t=insert(t,number);
r=r->next;
}
if(carry)
t=insert(t,1);
t->next=s;
return(s);
}

NODE *inputint(void)
{
NODE *s,*ps,*qs;
struct number
{
int num;
struct number *np;
}*p,*q;

int i,j,k;
long sum;
char c;
p=NULL;
while((c=getchar())!='\n')
if(c>='0'&& c<='9')
{
q=(struct number *)malloc(sizeof(struct number));
q->num=c-'0';
q->np=p;
p=q;
}

s=(NODE*)malloc(sizeof(NODE));
s->data=-1;
ps=s;
while(p!=NULL)
{
sum=0;i=0;k=1;
while(i<4&&p!=NULL)
{
sum=sum+k*(p->num);
i++;p=p->np;k=k*10;
}
qs=(NODE*)malloc(sizeof(NODE));
qs->data=sum;
ps->next=qs;
ps=qs;
}
ps->next=s;
return(s);
}

printint(NODE *s)
{
if(s->next->data!=-1)
{
printint(s->next);
if(s->next->next->data==-1)
printf("%d",s->next->data);
else
{
int i,k=HUN;
for(i=1;i<=4;i++,k/=10)
putchar('0'+s->next->data%(k)/(k/10));
}
}
}

main()
{
NODE *s1,*s2,*s;
NODE *inputint(),*addint(),*insert_after();
printf("S1=");
s1=inputint();
printf("S2=");
s2=inputint();
printf("S1=");
printint(s1);
printf("\n");
printf("S2=");
printint(s2);
printf("\n");
s=addint(s1,s2);
printf("S1+S2=");printint(s);printf("\n");
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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