#include int max(int a, int b) { return (a>b)?a:b; } void indent(int n, char c = ' ') { for(int i = 0; i < n; ++i) cout << c; } void trunc(char *s) { int i, j; for(i = 0; s[i] == '0' && s[i] != 0; ++i); if(s[i] == 0) s[1] = 0; else { for(j = 0; s[i+j] != 0; ++j) s[j] = s[i+j]; s[j] = 0; } } void sub(char *s1, char *s2, char *s3) { int s1l = strlen(s1); int sh = s1l - strlen(s2); int i, cry = 0, sr; for(i = 0; i < s1l; ++i) s3[i] = '0'; for(i = s1l - 1; i >= 0; --i) { if(i - sh >= 0) sr = s1[i] - s2[i-sh] - cry; else sr = s1[i] - '0' - cry; if(sr < 0) { sr += 10; cry = 1; } else { cry = 0; } s3[i] = sr + '0'; } s3[s1l] = 0; trunc(s3); } void add(char *s1, char *s2, char *s3, int shift = 0) { int s1l = strlen(s1), s2l = strlen(s2); int s3l = max(s1l, s2l+shift) + 1; // int sh = s1l - strlen(s2) - shift; int i, cry = 0, sr; for(i = 0; i < s3l; ++i) s3[i] = '0'; for(i = s3l - 1; i >= 0; --i) { sr = ((i-(s3l-s1l)>=0 && i-(s3l-s1l) < s1l)?s1[i-(s3l-s1l)]-'0':0) + ((i - (s3l-s2l-shift) >= 0 && i - (s3l-s2l-shift) < s2l)? s2[i-(s3l-s2l-shift)]-'0':0) + cry ; // cout << "xx" << i << ' ' << sr << endl; if(sr >= 10) { sr -= 10; cry = 1; } else { cry = 0; } s3[i] = sr + '0'; } s3[s3l] = 0; trunc(s3); } void mult1(char *s1, char m, char *s3) { int s1l = strlen(s1); int s3l = s1l + 1, cry = 0, sr; for(int i = s3l-1; i > 0; --i) { sr = (m-'0')*(s1[i-1]-'0') + cry; cry = sr / 10; sr = sr % 10; s3[i] = sr + '0'; } s3[0] = cry + '0'; s3[s3l] = 0; trunc(s3); } int main() { char s0[1002], *s1, *s2, s3[2000], op; int n; cin >> n; cin.getline(s0, 10); while(n > 0) { cin.getline(s0, 1002); int i; for(i = 0; s0[i] != 0; ++i) if(s0[i] > '9' || s0[i] < '0') { op = s0[i]; s0[i] = 0; s1 = s0; s2 = s0 + i + 1; } trunc(s1); trunc(s2); int s1l, s2l, s3l, smax; s1l = strlen(s1); s2l = strlen(s2); switch(op) { case '-': sub(s1, s2, s3); s3l = strlen(s3); smax = max(s1l, max(s2l+1, s3l)); indent(smax - s1l); cout << s1 << endl; indent(smax - s2l - 1); cout << '-' << s2 << endl; indent(smax, '-'); cout << endl; indent(smax - s3l); cout << s3 << endl << endl; break; case '+': add(s1, s2, s3, 0); s3l = strlen(s3); smax = max(s1l, max(s2l+1, s3l)); indent(smax - s1l); cout << s1 << endl; indent(smax - s2l - 1); cout << '+' << s2 << endl; indent(smax, '-'); cout << endl; indent(smax - s3l); cout << s3 << endl << endl; break; case '*': /*if(strcmp(s1, "0") == 0) { strcpy(s3, "0"); s3l = strlen(s3); smax = max(s1l, max(s2l+1, s3l)); indent(smax - s1l); cout << s1 << endl; indent(smax - s2l - 1); cout << '*' << s2 << endl; indent(smax, '-'); cout << endl; indent(smax - s3l); cout << s3 << endl << endl; } else */if(s2l == 1) { mult1(s1, s2[0], s3); s3l = strlen(s3); smax = max(s1l, max(s2l+1, s3l)); indent(smax - s1l); cout << s1 << endl; indent(smax - s2l - 1); cout << '*' << s2 << endl; indent(smax, '-'); cout << endl; indent(smax - s3l); cout << s3 << endl << endl; } else { // cout << "XXX" << endl; char s4[2][1500], s5[1500]; strcpy(s5, "0"); strcpy(s4[0], "0"); strcpy(s4[1],"0"); int k = 0; for(k = 0; k < s2l; ++k) { mult1(s1, s2[s2l-k-1], s4[k%2]); add(s4[(k+1)%2], s4[k%2], s5, k); strcpy(s4[(k)%2], s5); } smax = max(strlen(s5), s2l+1); strcpy(s5, "0"); strcpy(s4[0], "0"); strcpy(s4[1],"0"); indent(smax - s1l); cout << s1 << endl; indent(smax - s2l - 1); cout << '*' << s2 << endl; mult1(s1, s2[s2l-1], s5); int umax = max(s1l, max(s2l+1, strlen(s5))); strcpy(s5, "0"); indent(smax-umax); indent(umax, '-'); cout << endl; for(k = 0; k < s2l; ++k) { mult1(s1, s2[s2l-k-1], s4[k%2]); add(s4[(k+1)%2], s4[k%2], s5, k); indent(smax - k - strlen(s4[k%2])); cout << s4[(k)%2] << endl; strcpy(s4[(k)%2], s5); // cout << "xx" << s5 << "xx" << s4[k%2] << "xx" << s4[(k+1)%2] << endl; } indent(smax, '-'); cout << endl; indent(smax - strlen(s5)); cout << s5 << endl << endl; } } // cout << s1 << " " << op << " " << s2 << endl; n--; } return 0; }