PAT甲級 1088

題目 Rational Arithmetic

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.
Input Specification:
Each input file contains one test case, which gives in one line the two rational numbers in the format a1/b1 a2/b2. The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.
Output Specification:
For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is number1 operator number2 = result. Notice that all the rational numbers must be in their simplest form k a/b, where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output Inf as the result. It is guaranteed that all the output integers are in the range of long int.

解析

分數的四則運算

代碼

#include<bits/stdc++.h>

#define INF 1<<29

using namespace std;

struct fraction {
    long long up, down;
};


fraction reduction(fraction result);

void print(fraction f);

fraction fraction_add(fraction a, fraction b) {
    fraction c;
    c.up = a.up * b.down + a.down * b.up;
    c.down = a.down * b.down;
    return reduction(c);
}

fraction fraction_sub(fraction a, fraction b) {
    fraction c;
    c.up = a.up * b.down - a.down * b.up;
    c.down = a.down * b.down;
    return reduction(c);
}

fraction fraction_multiply(fraction a, fraction b) {
    fraction c;
    c.up = a.up * b.up;
    c.down = a.down * b.down;
    return reduction(c);
}

fraction fraction_divide(fraction a, fraction b) {
    fraction c;
    c.up = a.up * b.down;
    c.down = a.down * b.up;
    return reduction(c);
}

int gcd(long long a, long long b) {
    return !b ? a : gcd(b, a % b);
}

void pat1088() {
    fraction a{}, b{};
    scanf("%lld/%lld %lld/%lld", &a.up, &a.down, &b.up, &b.down);
    a = reduction(a);
    b = reduction(b);

    print(a);cout << " + ";print(b);cout << " = ";print(fraction_add(a, b));cout << endl;
    print(a);cout << " - ";print(b);cout << " = ";print(fraction_sub(a, b));cout << endl;
    print(a);cout << " * ";print(b);cout << " = ";print(fraction_multiply(a, b));cout << endl;
    print(a);cout << " / ";print(b);cout << " = ";
    if (b.up != 0) {
        print(fraction_divide(a, b));
    } else {
        printf("Inf");
    }
}


void print(fraction f) {
    f = reduction(f);
    if (f.up < 0) printf("(");
    if (f.down == 1) {
        printf("%lld", f.up);
    } else if (abs(f.up) > f.down) {
        printf("%lld %lld/%lld", f.up / f.down, abs(f.up) % f.down, f.down);
    } else {
        printf("%lld/%lld", f.up, f.down);
    }
    if (f.up < 0) printf(")");
}

fraction reduction(fraction result) {
    if (result.down < 0) {
        result.up = -result.up;
        result.down = -result.down;
    }
    if (result.up == 0) {
        result.down = 1;
    } else {
        int d = gcd(abs((result.up)), abs(result.down));
        result.up /= d;
        result.down /= d;
    }
    return result;
}


int main() {
    pat1088();
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章