Cậu post trong thẻ code giùm, dùng AStyle format lại code của cậu đã rồi sẽ có người giúp.
em cũng ko đc thông lắm về con trỏ và bộ nhớ động, mong các sư phụ chỉ giáo giùm...Code:#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct number { int cso; struct number *next; } number; number *num1=NULL,*num2=NULL,*sum=NULL; int pop(number* top); number *push(number *top,int x); int notempty(number *top); int main() { int i,j,k=0; char s[100],tem[2]; printf("please input the first number:\n"); gets(s); for(i=0;i<strlen(s);i++) { tem[0]=s[i];tem[1]='\0'; j=atoi(tem); num1=push(num1,j); //bo qua ktra ki tu ko hop le } printf("please input the second number:\n"); gets(s); for(i=0;i<strlen(s);i++) { tem[0]=s[i];tem[1]='\0'; j=atoi(tem); num2=push(num2,j); //bo qua ktra ki tu ko hop le } while(notempty(num1)&¬empty(num2)) { i=pop(num1)+pop(num2)+k; sum=push(sum,i%10); if(i<10) k=0; else k=1; } while(notempty(num1)) { i=pop(num1)+k; sum=push(sum,i%10); if(i<10) k=0; else k=1; } while(notempty(num2)) { i=pop(num2)+k; sum=push(sum,i%10); if(i<10) k=0; else k=1; } printf("sum of two number were inputed is: \n"); while(notempty(sum)) { i=pop(sum);printf("%d",i); } return 0; } number *push(number* top,int x) { number *new; new=calloc(1,sizeof(number));//bo qua buoc ktra bo nho new->cso=x;new->next=top; top=new; return top; } int pop(number* top) { number *ptr; int i; ptr=top; i=top->cso;top=top->next;free(ptr); return i; } int notempty(number *top) { if(top==NULL) return 0; else return 1; }![]()
![]()
------------------------------------
Nhắc nhở : Đặt Code vào Tag [CODE].Alviss
Đã được chỉnh sửa lần cuối bởi Alviss : 07-12-2007 lúc 10:53 PM.
Cậu post trong thẻ code giùm, dùng AStyle format lại code của cậu đã rồi sẽ có người giúp.
Để biên dịch đúng, bạn phải sửa như sau:
number *new_t = new number;
number *new_t = new number;
calloc(1,sizeof(number));//bo qua buoc ktra bo nho
new_t->cso=x;
new_t->next=top;
top = new_t;
return top;
xin giải thích rõ hơn có đc ko? thật sự mình ko thấy gì khác cả???
Ah làm sao cho code vào thẻ code đc nhỉ, mình ko hiểu mấy hi`hi`hi`
Xin cảm ơn các bạn đã chỉ giúp
sau đây mình xin đc gửi code của bài tập trên, về cơ bản thì nó là hoàn chỉnh(nếu ko xét tới 1 số đkiện như ktra dữ liệu, bộ nhớ,...)
Mong các bạn đánh giá và cải tiến giùm nhé
Code:#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct number { int cso; struct number *next; } number; number *pop(number* top,int *i); number *push(number *top,int x); int notempty(number *top); int main() { int x,y; number *num1=NULL,*num2=NULL,*sum=NULL; int i,j,k=0; char *s; printf("please input the first number:\n"); gets(s); for(i=0;i<strlen(s);i++) { j=s[i]-'0'; num1=push(num1,j); //bo qua ktra ki tu ko hop le } printf("\nplease input the second number:\n"); gets(s); for(i=0;i<strlen(s);i++) { j=s[i]-'0'; num2=push(num2,j); //bo qua ktra ki tu ko hop le } while(notempty(num1)&¬empty(num2)) { num1=pop(num1,&x);num2=pop(num2,&y); i=x+y+k; sum=push(sum,i%10); if(i<10) k=0; else k=1; } while(notempty(num1)) { num1=pop(num1,&x); i=x+k; sum=push(sum,i%10); if(i<10) k=0; else k=1; } while(notempty(num2)) { num2=pop(num2,&y); i=y+k; sum=push(sum,i%10); if(i<10) k=0; else k=1; } if(k==1) sum=push(sum,k); printf("sum of two number were inputed is: \n"); while(notempty(sum)) { sum=pop(sum,&x);printf("%d",x); } return 0; } number *push(number* top,int x) { number *new; new=calloc(1,sizeof(number));//bo qua buoc ktra bo nho new->cso=x;new->next=top; top=new; return top; } number *pop(number* top,int *i) { number *ptr; *i=top->cso; ptr=top; top=top->next;free(ptr); return top; } int notempty(number *top) { if(top==NULL) return 0; else return 1; }
vẫn chưa quét hết trường hợp.
nếu số nhập vào <0
-> doushitara ?
Đoạn này rườm rà wa!Code:while(notempty(num1)&¬empty(num2)) { num1=pop(num1,&x);num2=pop(num2,&y); i=x+y+k; sum=push(sum,i%10); if(i<10) k=0; else k=1; } while(notempty(num1)) { num1=pop(num1,&x); i=x+k; sum=push(sum,i%10); if(i<10) k=0; else k=1; } while(notempty(num2)) { num2=pop(num2,&y); i=y+k; sum=push(sum,i%10); if(i<10) k=0; else k=1; } if(k==1) sum=push(sum,k);
chưa test thử, nhưng chắc là okieCode:while(notempty(num1)||notempty(num2)) { x=y=0; if(!notempty(num1)) num1=pop(num1,&x); if(!notempty(num2)) num2=pop(num2,&y); i=x+y+k;if(i>9)k=1;else k=0; num=push(num,i%10); }
Đúng thế, đúng thế, cảm ơn nhiều...
Nhưng do hàm notempty của mình trả về 1 khi stack khác rỗng, nên 2 lệnh sau:
Phải đc sửa là:Code:if(!notempty(num1)) num1=pop(num1,&x); if(!notempty(num2)) num2=pop(num2,&y);
Còn lệnh sau đây phải đc giữ nguyên vì khi đó k sẽ lưu phần phải nhớ ở phép cộng cuối cùng (vd đối với 987+23=1010, nếu ko có lệnh này kết quả thu đc là 010)Code:if(notempty(num1)) num1=pop(num1,&x); if(notempty(num2)) num2=pop(num2,&y);
Anh Linh làm hay vãi....cảm ơn nhaCode:if(k==1) sum=push(sum,k);
Còn đối với trường hợp số âm, thật sự chưa nghĩ tới, khi đó chắc thành bài toán trừ 2 số rùi, và để tiếp tục dùng stack thì chắc là sẽ khác đúng ko nào???
Mọi người cùng nhau thảo luận nhé!!!