A.1088 Rational Arithmetic (20)

Posted Yaxadu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A.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
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;

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

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}

Fraction Sum(Fraction f1, Fraction f2) {
    Fraction f;
    f.up = f1.up * f2.down + f2.up * f1.down;
    f.down = f1.down * f2.down;
    return f;
}

Fraction Difference(Fraction f1, Fraction f2) {
    Fraction f;
    f.up = f1.up * f2.down - f2.up * f1.down;
    f.down = f1.down * f2.down;
    return f;
}

Fraction Product(Fraction f1, Fraction f2) {
    Fraction f;
    f.up = f1.up * f2.up;
    f.down = f1.down * f2.down;
    return f;
}

Fraction Quotient(Fraction f1, Fraction f2) {
    Fraction f;
    f.up = f1.up * f2.down;
    f.down = f1.down * f2.up;
    return f;
}

Fraction Reduction(Fraction result) {
    if (result.down < 0) {
        result.up = -result.up;
        result.down = -result.down;
    }
    if (result.up == 0) result.down = 1;
    else {
        ll d = gcd(abs(result.up), abs(result.down));
        result.up /= d;
        result.down /= d;
    }
    return result;
}

void showResult(Fraction result) {
    result = Reduction(result);
    if (result.up < 0) printf("(");
    if (result.down == 1) printf("%lld", result.up);
    else if (abs(result.up) > result.down) printf("%lld %lld/%lld", result.up / result.down, abs(result.up % result.down), result.down);
    else printf("%lld/%lld", result.up, result.down);
    if (result.up < 0) printf(")");
}

int main() {
    scanf("%lld/%lld %lld/%lld", &f1.up, &f1.down, &f2.up, &f2.down);
    //加法
    showResult(f1);
    printf(" + ");
    showResult(f2);
    printf(" = ");
    showResult(Sum(f1, f2));
    printf("\n");
    //减法
    showResult(f1);
    printf(" - ");
    showResult(f2);
    printf(" = ");
    showResult(Difference(f1, f2));
    printf("\n");
    //乘法
    showResult(f1);
    printf(" * ");
    showResult(f2);
    printf(" = ");
    showResult(Product(f1, f2));
    printf("\n");
    //除法
    showResult(f1);
    printf(" / ");
    showResult(f2);
    printf(" = ");
    if (f2.up == 0) printf("Inf");
    else showResult(Quotient(f1, f2));
    printf("\n");
    return 0;
}

 

以上是关于A.1088 Rational Arithmetic (20)的主要内容,如果未能解决你的问题,请参考以下文章

Rational Rose

用rational rose 如何画数据流图

Rational Rose_2007的下载安装与破解--UML建模软件

ACdream1187-Rational Number Tree-模拟/找规律

Win10 安装Rational_Rose_2007 问题总结及解决方案

IBM Rational Software Architect V9.0安装图解