#include #include #include int c_ulohy = 0, p_uloh; char a[1024], b[1024], *s, c; char *dashes(int dash) { char *res; int i; res=malloc(dash+3); for (i=0; ij) return i; else return j; } #define MAX(a, b) (a>b?a:b) char * plus(char *s1, char *s2) { int xfer = 0; char bufx[10240]; int il, jl, i, len, sl; il = strlen(s1); jl = strlen(s2); len = MAX(il, jl); if (il=sl) bufx[i] = s1[i] + xfer; else bufx[i] = s1[i] + s2[i] - '0' + xfer; xfer = 0; if (bufx[i]>'9') bufx[i] -= 10, xfer = 1; } if (xfer) bufx[i++] = '1'; bufx[i++] = 0; return strdup(bufx); } char * minus(char *s1, char *s2) { int xfer = 0; char bufx[10240]; int il, jl, i, len, sl; il = strlen(s1); jl = strlen(s2); len = MAX(il, jl); sl = jl; for (i=0; i=sl) bufx[i] = s1[i] - xfer; else bufx[i] = s1[i] - s2[i] + '0' - xfer; xfer = 0; if (bufx[i]<'0') bufx[i] += 10, xfer = 1; } bufx[i++] = 0; return strdup(bufx); } char *output[2048]; int no; char *result; void krat(char *s1, char *s2) { char buf[10240]; int i; result = strdup("0"); for (i=0; i=1 && s[i] == '0'; i--) if ((s[i-1] >= '0') && (s[i-1] <= '9')) s[i] = 0; l = strlen(s); for (i=0; i=0; i--) putchar(s[i]); putchar('\n'); } int main(void) { scanf("%d\n", &p_uloh); for (c_ulohy=0; c_ulohy < p_uloh; c_ulohy++) { int i,j,k; char buf[10240]; gets(buf); /* printf("got %s\n", buf);*/ j = strlen(buf); s = b; for (i=j-1; i>=0; i--) { if (isdigit(buf[i])) *s++ = buf[i]; else { *s++ = 0; c = buf[i]; s = a; } } *s++ = 0; /* printf("%s %c %s\n", a, c, b);*/ for (i=0; i<2048; i++) output[i] = NULL; output[0]=strdup(a); output[1]=malloc(strlen(b)+5); strcpy(output[1],b); output[2]=dashes(MAX(strlen(output[0]), strlen(output[1])+1)); no = 3; switch(c) { case '+': output[3] = plus(output[0], output[1]); no++; strcat(output[1], "+"); break; case '-': output[3] = minus(output[0], output[1]); no++; strcat(output[1], "-"); break; case '*': krat(output[0], output[1]); strcat(output[1], "*"); break; } { int i, maxlen = 0; for (i=0; imaxlen) maxlen=l; } for (i=0; i