#include #include #include #define MAX 520 #define max(a,b) ((a)>(b)?(a):(b)) class Cislo { public: int len; char buf[MAX]; Cislo(char *, int); Cislo():len(1) { buf[0] = 0; }; Cislo sum(Cislo &other); Cislo subtr(Cislo &other); void plus(Cislo &other); void minus(Cislo &other); void asterisk(Cislo &other); void dump(int size, char stromek = ' ', int doplnit=0); void shift(int amount) { memmove(buf+amount, buf, len); memset(buf, 0, amount); len += amount; } void normalize() { while ((this->buf[this->len-1]==0) && this->len) { this->len--; } if (!this->len) { this->len=1; this->buf[0] = 0; } } int get(int o) { return (o>=this->len)?0:this->buf[o]; }; }; /*CONSTR*/ Cislo::Cislo(char *orisek, int len) { this->len = len; for (int i = this->len; i >= 0; i--) buf[i] = orisek[len-i-1]; } void Cislo::dump(int size, char stromek, int doplnit) { int i; #if 1 for (i = size-1; i>=0; i--) { if (i == this->len) { putchar(stromek); } else if ((i > this->len)) putchar(' '); else putchar(this->buf[i]+'0'); } while (size < doplnit) { size++; putchar(' '); } #else for (i = 0; i < this->len; i++) putchar(this->buf[i]+'0'); #endif putchar('\n'); } Cislo Cislo::subtr(Cislo &other) { Cislo ret; int prenos = 0; int u = 0; ret.len = max(this->len, other.len); for (int i = 0; i < ret.len; i++) { int sum = this->get(i) - other.get(i) - prenos; ret.buf[i] = (sum+10) % 10; prenos = (sum<0)?1:0; } if (prenos) u = u / u; ret.normalize(); return ret; } Cislo Cislo::sum(Cislo &other) { Cislo ret; int prenos = 0; ret.len = max(this->len, other.len); for (int i = 0; i < ret.len; i++) { int sum = prenos + this->get(i) + other.get(i); ret.buf[i] = sum % 10; prenos = sum/10; } if (prenos) ret.buf[ret.len++] = prenos; return ret; } void cara(int stromecek, char vetvicka = '-') { for (int i = 0; i < stromecek; i++) putchar(vetvicka); if (vetvicka == '-') putchar('\n'); } void Cislo::plus(Cislo &other) { Cislo ret = this->sum(other); int delka = max(this->len, other.len+1); delka = max(delka, ret.len); this->dump(delka); other.dump(delka, '+'); cara(delka); ret.dump(delka); } void Cislo::minus(Cislo &other) { Cislo ret = this->subtr(other); int delka = max(this->len, other.len+1); delka = max(delka, ret.len); this->dump(delka); other.dump(delka, '-'); cara(delka); ret.dump(delka); } void Cislo::asterisk(Cislo &other) { Cislo sumy[500]; int i, j, fdelka, delka; fdelka = max(this->len, other.len+1); delka = fdelka; Cislo vysledek; for (i = 0; i < other.len; i++) { Cislo mezisoucet, ughu; for (j = 0; j < other.get(i); j++) { mezisoucet = mezisoucet.sum(*this); } sumy[i] = mezisoucet; ughu = mezisoucet; ughu.shift(i); vysledek = vysledek.sum(ughu); delka = max(delka, mezisoucet.len + i); } delka = max(delka, vysledek.len); fdelka = max (fdelka, sumy[0].len); /* VYPIS :-) */ this->dump(delka); other.dump(delka, '*'); cara(delka-fdelka, ' '); cara(fdelka); if (other.len > 1) { for (i = 0; i < other.len; i++) { sumy[i].dump(delka-i, ' '); } cara(delka); } vysledek.dump(delka); } void bagr() { char BUF[MAX]; int i = 0; char c = 0, op; while (1) { c = getc(stdin); if ((c >= '0') && (c <= '9')) { BUF[i++] = c-'0'; } else break; } BUF[i] = 0; Cislo a(BUF, i); i = 0; op = c; while (1) { c = getc(stdin); if ((c >= '0') && (c <= '9')) { BUF[i++] = c-'0'; } else break; } Cislo b(BUF, i); switch(op) { case '+': a.plus(b); break; case '-': a.minus(b); break; case '*': a.asterisk(b); break; } puts(""); } int main() { int c, i; scanf("%i", &c); getc(stdin); for (i = 0; i < c; i++) bagr(); return 0; }