1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include <cstdio> #include <cstring>
static const int LEN = 1004;
int a[LEN], b[LEN], c[LEN], d[LEN];
void clear(int a[]) { for (int i = 0; i < LEN; ++i) a[i] = 0; }
void read(int a[]) { static char s[LEN + 1]; scanf("%s", s);
clear(a);
int len = strlen(s); for (int i = 0; i < len; ++i) a[len - i - 1] = s[i] - '0'; }
void print(int a[]) { int i; for (i = LEN - 1; i >= 1; --i) if (a[i] != 0) break; for (; i >= 0; --i) putchar(a[i] + '0'); putchar('\n'); }
void add(int a[], int b[], int c[]) { clear(c);
for (int i = 0; i < LEN - 1; ++i) { c[i] += a[i] + b[i]; if (c[i] >= 10) { c[i + 1] += 1; c[i] -= 10; } } }
void sub(int a[], int b[], int c[]) { clear(c);
for (int i = 0; i < LEN - 1; ++i) { c[i] += a[i] - b[i]; if (c[i] < 0) { c[i + 1] -= 1; c[i] += 10; } } }
void mul_short(int a[], int b, int c[]) { clear(c);
for (int i = 0; i < LEN - 1; ++i) { c[i] += a[i] * b;
if (c[i] >= 10) { c[i + 1] += c[i] / 10; c[i] %= 10; } } }
void mul(int a[], int b[], int c[]) { clear(c);
for (int i = 0; i < LEN - 1; ++i) { for (int j = 0; j <= i; ++j) c[i] += a[j] * b[i - j];
if (c[i] >= 10) { c[i + 1] += c[i] / 10; c[i] %= 10; } } }
bool greater_eq(int a[], int b[], int last_dg, int len) { if (a[last_dg + len] != 0) return true; for (int i = len - 1; i >= 0; --i) { if (a[last_dg + i] > b[i]) return true; if (a[last_dg + i] < b[i]) return false; } return true; }
void div(int a[], int b[], int c[], int d[]) { clear(c); clear(d);
int la, lb; for (la = LEN - 1; la > 0; --la) if (a[la - 1] != 0) break; for (lb = LEN - 1; lb > 0; --lb) if (b[lb - 1] != 0) break; if (lb == 0) { puts("> <"); return; }
for (int i = 0; i < la; ++i) d[i] = a[i]; for (int i = la - lb; i >= 0; --i) { while (greater_eq(d, b, i, lb)) { for (int j = 0; j < lb; ++j) { d[i + j] -= b[j]; if (d[i + j] < 0) { d[i + j + 1] -= 1; d[i + j] += 10; } } c[i] += 1; } } }
int main() { read(a);
char op[4]; scanf("%s", op);
read(b);
switch (op[0]) { case '+': add(a, b, c); print(c); break; case '-': sub(a, b, c); print(c); break; case '*': mul(a, b, c); print(c); break; case '/': div(a, b, c, d); print(c); print(d); break; default: puts("> <"); }
return 0; }
|