문자열과 스택을 이용 큰수 더하기
Problem. Input two BIG integers and add them.
134984530247143098190
#include <stdio.h>
void push(int s[], int x){
//printf("push %d %d ", s[0], x);
s[++s[0]]=x;
}
int pop(int s[]) {
return s[s[0]--];
}
int isnotempty(int s[]) {
return s[0]>0;
}
int main() {
char stra[400];
char strb[400];
int stx[400]={0,};
int sty[400]={0,};
int stRes[400]={0,};
int i, x, y, z, carry;
printf("enter first number: ");
gets(stra);
printf("enter second number: ");
gets(strb);
i=0;
while(*(stra+i)) {
push(stx, *(stra+i)-'0');
i=i+1;
}
i=0;
while(*(strb+i)) {
push(sty, *(strb+i)-'0');
i=i+1;
}
carry=0;
x=0;
y=0;
while(isnotempty(stx) && isnotempty(sty)) {
x=pop(stx);
y=pop(sty);
z=x+y+carry;
carry=z/10;
z=z%10;
push(stRes, z);
}
while(isnotempty(stx)) {
x=pop(stx);
z=x+carry;
carry=z/10;
z=z%10;
push(stRes, z);
}
while(isnotempty(sty)) {
y=pop(sty);
z=y+carry;
carry=z/10;
z=z%10;
push(stRes, z);
}
if(carry) push(stRes, carry);
while(isnotempty(stRes)) {
z=pop(stRes);
printf("%d", z);
}
return 0;
}