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
| int find_Gcd(int a, int b) { if (a == b) return a; else { if (a < b) { int c = b; b = a; a = c;
} return a - b > b ? find_Gcd(a - b, b) : find_Gcd(b, a - b); } }
int Lcm(int a, int b) { return a * b / find_Gcd(a, b); }
void yuefen(int& a, int& b) { int i = find_Gcd(a, b); a = a / i; b = b / i; }
void add(int a, int b, int c, int d) { printf("%d/%d + %d/%d = ", a, b, c, d); int n = Lcm(b, d); int i = n / b; int j = n / d; a = a * i; c = c * j; int s = a + c; yuefen(s, d); printf("%d/%d\n", s, d); }
void sub(int a, int b, int c, int d) { printf("%d/%d - %d/%d = ", a, b, c, d); int n = Lcm(b, d); int i = n / b; int j = n / d; a = a * i; c = c * j; int s = a - c; yuefen(s, d); printf("%d/%d\n", s, d); }
void mul(int a, int b, int c, int d) { printf("%d/%d * %d/%d = ", a, b, c, d); a = a * c; b = b * d; yuefen(a, b); printf("%d/%d\n", a, b); }
void div(int a, int b, int c, int d) { printf("%d/%d * %d/%d = ", a, b, c, d); a = a * d; b = b * c; yuefen(a, b); printf("%d/%d\n", a, b); }
int main() { char op; int a, b, c, d; scanf("%d,%d,%c,%d,%d", &a, &b, &op, &c, &d); switch (op) { case '+': add(a, b, c, d); break; case '*': mul(a, b, c, d); break; case '-': sub(a, b, c, d); break; case '/': div(a, b, c, d); break; } }
|