1088 Rational Arithmetic (20分)

1 题目

1088 Rational Arithmetic (20分)
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.

Sample Input 1:
2/3 -4/2

      
    
Sample Output 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

      
    
Sample Input 2:
5/3 0/6

      
    
Sample Output 2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

2 解析

2.1 题意

求给出的两个分数的加减乘除运算。

2.2 思路

  • 除法,除数的分子为0,应该特判为Inf;
  • 数据范围为int,两数相乘最大可达long long;
  • 使用abs函数时,对于long long类型的绝对值,应该使用<\algorithm>头文件里面的,而不是<\cstring>里面的。

3 参考代码

#include <cstdio>
#include <algorithm>

using std::abs;

typedef long long ll;

struct Fraction
{
    ll up;
    ll down;
}f1, f2;


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

Fraction reduction(Fraction res){
    if(res.down < 0){
        res.up = -res.up;
        res.down = -res.down;
    }

    if(res.up == 0){
        res.down = 1;
    }else{
        int d = gcd(abs(res.up), abs(res.down));
        res.up /= d;
        res.down /= d;
    }

    return res;
}

Fraction add(Fraction f1, Fraction f2){
    Fraction res;
    res.up = f1.up * f2.down + f2.up * f1.down;
    res.down = f1.down * f2.down;
    return reduction(res);
}

Fraction minu(Fraction f1, Fraction f2){
    Fraction res;
    res.up = f1.up * f2.down - f2.up * f1.down;
    res.down = f1.down * f2.down;
    return reduction(res);
}

Fraction multi(Fraction f1, Fraction f2){
    Fraction res;
    res.up = f1.up * f2.up;
    res.down = f1.down * f2.down;
    return reduction(res);
}

Fraction divide(Fraction f1, Fraction f2){
    Fraction res;
    res.up = f1.up * f2.down;
    res.down = f1.down * f2.up;
    return reduction(res);
}

void showResult(Fraction res){
    res = reduction(res);

    if(res.up < 0) printf("(");

    if(res.down == 1){
        printf("%lld", res.up);
    }else if(abs(res.up) > res.down){
        printf("%lld %lld/%lld", res.up/res.down, abs(res.up)%res.down, res.down);
    }else{
        printf("%lld/%lld", res.up, res.down);
    }

    if(res.up < 0) printf(")");
}

int main(int argc, char const *argv[])
{
    scanf("%lld/%lld %lld/%lld", &f1.up, &f1.down, &f2.up, &f2.down);

        showResult(f1);
        printf(" + ");
        showResult(f2);
        printf(" = ");
        showResult(add(f1, f2));
        printf("\n");

        showResult(f1);
        printf(" - ");
        showResult(f2);
        printf(" = ");
        showResult(minu(f1, f2));
        printf("\n");

        showResult(f1);
        printf(" * ");
        showResult(f2);
        printf(" = ");
        showResult(multi(f1, f2));
        printf("\n");

        showResult(f1);
        printf(" / ");
        showResult(f2);
        printf(" = ");
        if(f2.up == 0){
            printf("Inf");
        }else{
            showResult(divide(f1, f2));
        }

    return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页