整个程序的源码: #include<stdio.h> #include<string.h> #include<malloc.h>
#define MAX 255
typedef struct {int a[MAX]; int flag; }shu;
void strtoi(char *q,int a[]) {int i; for(i=0;i<strlen(q);i++) a[i]=q[i]-'0'; }
int youyi(int n,int a[],int L) {int i,t; t=a[L-1]; for(i=L-1;i>0;i--) a[i]=a[i-1]; a[0]=n; return t; }
void clear(int a[],int L) {int i; for(i=0;i<L;i++) a[i]=0; }
void tjinwei(int a[],int L) {int i; for(i=L-1;i>0;i--) {a[i-1]=a[i-1]+a[i]/10; a[i]=a[i]%10; } }
void weijia(int a[],int b[],int L) {int i; for(i=0;i<L;i++) a[i]=a[i]+b[i]; tjinwei(a,L); }
char * chen(char *p,char *q) {int l1,l2,*s,*p1,*q1,*t,i,j,k,L,d; char *str; l1=strlen(p); l2=strlen(q); L=l1+l2+2; s=(int *)malloc(L*sizeof(int)); q1=(int *)malloc(l2*sizeof(int)); p1=(int *)malloc(l1*sizeof(int)); strtoi(p,p1); strtoi(q,q1); clear(s,L); t=(int *)malloc(L*sizeof(int)); for(i=0;i<l2;i++) {k=q1[l2-1]; clear(t,L); for(j=0;j<l1;j++) t[L-1-j]=k*p1[l1-1-j]; tjinwei(t,L); weijia(s,t,L); d=youyi(0,s,L); youyi(d,q1,l2); } str=(char *)malloc((L+l2)*sizeof(char)); i=0;j=0; while(s[i]==0) i++; while(i<L) str[j++]=s[i++]+'0'; for(i=0;i<l2;i++) str[j++]=q1[i]+'0'; str[j]='\0'; free(s); free(q1); free(p1); return str; }
void init(char *ch,shu *l) {int i,len,j; if(*ch=='-') l->flag=1; else l->flag=0; clear(l->a,MAX); len=strlen(ch); j=len-l->flag; for(i=0;i<j;i++) l->a[MAX-1-i]=ch[len-1-i]-'0'; }
int cmp(int a[],int b[]) {int i=0; while(a[i]==b[i]&&i<MAX) i++; if(i>=MAX) return 0; return a[i]-b[i] ; }
void weijian(shu *s1,shu *s2) {shu *m,*n; int i,d; m=s1;n=s2; d=cmp(m->a,n->a); if(d==0) {clear(m->a,MAX); m->flag=0;} else if(d<0) {m->flag=n->flag; for(i=MAX-1;i>=0;i--) {if(n->a[i]<m->a[i]) {n->a[i-1]--; n->a[i]+=10; } m->a[i]=n->a[i]-m->a[i]; } } else for(i=MAX-1;i>=0;i--) {if(m->a[i]<n->a[i]) {m->a[i-1]--; m->a[i]+=10; } m->a[i]=m->a[i]-n->a[i]; } }
char * jia(char *ch1,char *ch2) {shu *s1,*s2; char *str,*m,*n; int i,j; m=ch1;n=ch2; s1=(shu *)malloc(sizeof(shu)); s2=(shu *)malloc(sizeof(shu)); init(m,s1);init(n,s2); if(s1->flag==s2->flag) weijia(s1->a,s2->a,MAX); else weijian(s1,s2); str=(char *)malloc((MAX+1)*sizeof(char)); i=0;j=0; if(s1->flag==1) str[j++]='-'; while(s1->a[i]==0&&i<MAX-1) i++; while(i<MAX) str[j++]=s1->a[i++]+'0'; /*È«ÁãÒ²ÒªÁôһλ×÷Ϊ½á¹û*/ str[j]='\0'; free(s1); free(s2); return str; }
char *jian(char *ch1,char *ch2) {int i,l,j; char *s; if(*ch2=='-') s=ch2+1; else { l=strlen(ch2); s=(char *)malloc((l+3)*sizeof(char)); s[0]='-'; j=1;i=0; while(i<l) s[j++]=ch2[i++]; s[j]='\0'; } return jia(ch1,s); }
char *chu(char *ch1,char *ch2) {int l1,l2,i,j,f1,f2,b[100]; char *s,*r,*p,*q,*t,*tt,a[2],*rr; f1=0;f2=0;a[1]='\0';j=0; clear(b,100); if(*ch1=='-') f1=1; if(*ch2=='-') f2=1; l1=strlen(ch1); l2=strlen(ch2); if(l2>l1) return "0"; l2=l2-f2; s=(char *)malloc((l2+2)*sizeof(char)); p=ch1+f1; i=0; while(p<ch1+f1+l2) s[i++]=*p++; s[i]='\0'; r=(char *)malloc((l1-l2+3)*sizeof(char)); if(f1!=f2) {r[0]='-';j=1;} q=ch2+f2; while(l2<=l1) {for(i=1;i<10;i++) {a[0]=i+'0'; t=chen(q,a); tt=jian(s,t); if(*tt=='-') break; } a[0]=i-1+'0'; r[j++]=a[0]; s=jian(s,chen(q,a)); a[0]=*p++; strcat(s,a); l2++; } r[j]='\0'; rr=r; while(*rr=='0') rr++; return rr; }
int error(char *s) {int a=0,i=0,j; j=strlen(s); if(*s=='-') i=1; for(;(s[i]>='0'&&s[i]<='9')&&i<j;i++); if(i<j) a=1; return a; }
void main() {char *s,a[100],b[100],c;int n; printf("Input the first number:"); scanf("%s",a); printf("Input the second number:"); scanf("%s",b); if(error(a)||error(b)) {printf("Input error!\n");exit(0);} Lp:printf("\n\n\nThe menu:\n\n"); printf("\t1.jia\n"); printf("\t2.jian\n"); printf("\t3.cheng\n"); printf("\t4.chu\n"); printf("\n\ninput choose(1~4):"); scanf("%d",&n);printf("\n\n"); if(n<1||n>4) {printf("Input error!\n"); getch();goto Lp;} switch(n) {case 1:c='+'; s=jia(a,b);break; case 2:c='-'; s=jian(a,b); break; case 3: c='*';s=chen(a,b); break; case 4: c='/';s=chu(a,b);break; default: s=NULL; } printf("%s %c %s = %s\n",a,c,b,s);
printf("\n\nThanks for your using!\n\n"); printf("Press any key close!\n");
getch();
}
----------------解决方案--------------------------------------------------------
#include<stdio.h> #include<string.h> #include<malloc.h>
#define MAX 255
typedef struct {int a[MAX]; int flag; }shu;
void strtoi(char *q,int a[]) {int i; for(i=0;i<strlen(q);i++) a[i]=q[i]-'0'; }
int youyi(int n,int a[],int L) {int i,t; t=a[L-1]; for(i=L-1;i>0;i--) a[i]=a[i-1]; a[0]=n; return t; }
void clear(int a[],int L) {int i; for(i=0;i<L;i++) a[i]=0; }
void tjinwei(int a[],int L) {int i; for(i=L-1;i>0;i--) {a[i-1]=a[i-1]+a[i]/10; a[i]=a[i]%10; } }
void weijia(int a[],int b[],int L) {int i; for(i=0;i<L;i++) a[i]=a[i]+b[i]; tjinwei(a,L); }
char * chen(char *p,char *q) {int l1,l2,*s,*p1,*q1,*t,i,j,k,L,d; char *str; l1=strlen(p); l2=strlen(q); L=l1+l2+2; s=(int *)malloc(L*sizeof(int)); q1=(int *)malloc(l2*sizeof(int)); p1=(int *)malloc(l1*sizeof(int)); strtoi(p,p1); strtoi(q,q1); clear(s,L); t=(int *)malloc(L*sizeof(int)); for(i=0;i<l2;i++) {k=q1[l2-1]; clear(t,L); for(j=0;j<l1;j++) t[L-1-j]=k*p1[l1-1-j]; tjinwei(t,L); weijia(s,t,L); d=youyi(0,s,L); youyi(d,q1,l2); } str=(char *)malloc((L+l2)*sizeof(char)); i=0;j=0; while(s[i]==0) i++; while(i<L) str[j++]=s[i++]+'0'; for(i=0;i<l2;i++) str[j++]=q1[i]+'0'; str[j]='\0'; free(s); free(q1); free(p1); return str; }
void init(char *ch,shu *l) {int i,len,j; if(*ch=='-') l->flag=1; else l->flag=0; clear(l->a,MAX); len=strlen(ch); j=len-l->flag; for(i=0;i<j;i++) l->a[MAX-1-i]=ch[len-1-i]-'0'; }
int cmp(int a[],int b[]) {int i=0; while(a[i]==b[i]&&i<MAX) i++; if(i>=MAX) return 0; return a[i]-b[i] ; }
void weijian(shu *s1,shu *s2) {shu *m,*n; int i,d; m=s1;n=s2; d=cmp(m->a,n->a); if(d==0) {clear(m->a,MAX); m->flag=0;} else if(d<0) {m->flag=n->flag; for(i=MAX-1;i>=0;i--) {if(n->a[i]<m->a[i]) {n->a[i-1]--; n->a[i]+=10; } m->a[i]=n->a[i]-m->a[i]; } } else for(i=MAX-1;i>=0;i--) {if(m->a[i]<n->a[i]) {m->a[i-1]--; m->a[i]+=10; } m->a[i]=m->a[i]-n->a[i]; } }
char * jia(char *ch1,char *ch2) {shu *s1,*s2; char *str,*m,*n; int i,j; m=ch1;n=ch2; s1=(shu *)malloc(sizeof(shu)); s2=(shu *)malloc(sizeof(shu)); init(m,s1);init(n,s2); if(s1->flag==s2->flag) weijia(s1->a,s2->a,MAX); else weijian(s1,s2); str=(char *)malloc((MAX+1)*sizeof(char)); i=0;j=0; if(s1->flag==1) str[j++]='-'; while(s1->a[i]==0&&i<MAX-1) i++; while(i<MAX) str[j++]=s1->a[i++]+'0'; /*è?á?ò2òaá?ò???×÷?a?á1?*/ str[j]='\0'; free(s1); free(s2); return str; }
char *jian(char *ch1,char *ch2) {int i,l,j; char *s; if(*ch2=='-') s=ch2+1; else { l=strlen(ch2); s=(char *)malloc((l+3)*sizeof(char)); s[0]='-'; j=1;i=0; while(i<l) s[j++]=ch2[i++]; s[j]='\0'; } return jia(ch1,s); }
char *chu(char *ch1,char *ch2) {int l1,l2,i,j,f1,f2,b[100]; char *s,*r,*p,*q,*t,*tt,a[2],*rr; f1=0;f2=0;a[1]='\0';j=0; clear(b,100); if(*ch1=='-') f1=1; if(*ch2=='-') f2=1; l1=strlen(ch1); l2=strlen(ch2); l2=l2-f2; l1=l1-f1; if(l2>l1) return "0";
s=(char *)malloc((l2+2)*sizeof(char)); p=ch1+f1; i=0; while(p<ch1+f1+l2) s[i++]=*p++; s[i]='\0'; r=(char *)malloc((l1-l2+3)*sizeof(char)); if(f1!=f2) {r[0]='-';j=1;} q=ch2+f2; while(l2<=l1) {for(i=1;i<10;i++) {a[0]=i+'0'; t=chen(q,a); tt=jian(s,t); if(*tt=='-') break; } a[0]=i-1+'0'; if(!(f1!=f2&&j==1&&a[0]=='0')) r[j++]=a[0]; s=jian(s,chen(q,a)); a[0]=*p++; strcat(s,a); l2++; } r[j]='\0'; rr=r; while(*rr=='0') rr++; return rr; }
int error(char *s) {int a=0,i=0,j; j=strlen(s); if(*s=='-') i=1; for(;(s[i]>='0'&&s[i]<='9')&&i<j;i++); if(i<j) a=1; return a; }
void main() {char *s,a[100],b[100],c;int n; printf("Input the first number:"); scanf("%s",a); printf("Input the second number:"); scanf("%s",b); if(error(a)||error(b)) {printf("Input error!\n");exit(0);} Lp:printf("\n\n\nThe menu:\n\n"); printf("\t1.jia\n"); printf("\t2.jian\n"); printf("\t3.cheng\n"); printf("\t4.chu\n"); printf("\n\ninput choose(1~4):"); scanf("%d",&n);printf("\n\n"); if(n<1||n>4) {printf("Input error!\n"); getch();goto Lp;} switch(n) {case 1:c='+'; s=jia(a,b);break; case 2:c='-'; s=jian(a,b); break; case 3: c='*';s=chen(a,b); break; case 4: c='/';s=chu(a,b);break; default: s=NULL; } printf("%s %c %s = %s\n",a,c,b,s);
printf("\n\nThanks for your using!\n\n"); printf("Press any key close!\n");
getch();
}
[此贴子已经被作者于2005-4-17 15:48:52编辑过]
----------------解决方案--------------------------------------------------------
收藏本帖咯~~
----------------解决方案--------------------------------------------------------
空前好强!
----------------解决方案--------------------------------------------------------
厉害!! 收藏!建议:输入时大数一个一个的输麻烦,不支持小数工程上不实用,
----------------解决方案--------------------------------------------------------
太强了,佩服!
能不能用栈与链表写一下除除法呢,小弟先谢谢了,太需要了
----------------解决方案--------------------------------------------------------