【PAT甲级 - C++题解】1088 Rational Arithmetic

简介: 【PAT甲级 - C++题解】1088 Rational Arithmetic

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.


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


题意

给定两个有理数,你的任务是实现基本算术,即计算它们的和,差,积和商。

输入共一行,以 a1/b1 a2/b2 的形式给出两个有理数。

分子和分母都在 long int 范围内,如果存在负号,则只能出现在分子前面,分母保证为非零数字。


思路

这道题就是模拟分数的加减乘除运算即可,假设有两个分数 a/b 和 c/d ,则运算公式分别如下:


【加法】a = a ∗ d + c ∗ b a = a * d + c * ba=a∗d+c∗b,b = b ∗ d b = b * db=b∗d


【减法】a = a ∗ d − c ∗ b a = a * d - c * ba=a∗d−c∗b,b = b ∗ d b = b * db=b∗d


【乘法】a = a ∗ c a = a * ca=a∗c,b = b ∗ d b = b * db=b∗d


【除法】a = a ∗ d a = a * da=a∗d,b = b ∗ c b = b * cb=b∗c(另外,如果 c 为 0 ,则直接输出 Inf )


输出分数的时候需要先对分子分母进行化简,求它们的最大公约数。同时,要注意负数情况,如果分母为负数,则需要转移到分子上再输出,且输出负数时要加括号。


代码

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
//求最大公约数
LL gcd(LL a, LL b)
{
    return b ? gcd(b, a % b) : a;
}
//打印运算结果
void print(LL a, LL b)
{
    //化简
    LL t = gcd(a, b);
    a /= t, b /= t;
    //将分母上的负数移到分子上,并判断分子是否为负数
    if (b < 0) a *= -1, b *= -1;
    bool is_minus = a < 0;
    //输出结果
    if (is_minus) cout << "(";
    if (b == 1)    printf("%lld", a);
    else
    {
        if (abs(a) >= b) printf("%lld ", a / b), a = abs(a) % b;
        printf("%lld/%lld", a, b);
    }
    if (is_minus) cout << ")";
}
//加法运算
void add(LL a, LL b, LL c, LL d)
{
    print(a, b), cout << " + ", print(c, d), cout << " = ";
    a = a * d + c * b;
    b = b * d;
    print(a, b), cout << endl;
}
//减法运算
void sub(LL a, LL b, LL c, LL d)
{
    print(a, b), cout << " - ", print(c, d), cout << " = ";
    a = a * d - c * b;
    b = b * d;
    print(a, b), cout << endl;
}
//乘法运算
void mul(LL a, LL b, LL c, LL d)
{
    print(a, b), cout << " * ", print(c, d), cout << " = ";
    a = a * c;
    b = b * d;
    print(a, b), cout << endl;
}
//除法运算
void div(LL a, LL b, LL c, LL d)
{
    print(a, b), cout << " / ", print(c, d), cout << " = ";
    if (c == 0)    puts("Inf");
    else
    {
        a = a * d;
        b = b * c;
        print(a, b), cout << endl;
    }
}
int main()
{
    LL a, b, c, d;
    scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
    add(a, b, c, d);
    sub(a, b, c, d);
    mul(a, b, c, d);
    div(a, b, c, d);
    return 0;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
64 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
81 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
76 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
76 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
77 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
76 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
131 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
55 0
|
19天前
|
存储 编译器 对象存储
【C++打怪之路Lv5】-- 类和对象(下)
【C++打怪之路Lv5】-- 类和对象(下)
21 4
|
19天前
|
编译器 C语言 C++
【C++打怪之路Lv4】-- 类和对象(中)
【C++打怪之路Lv4】-- 类和对象(中)
18 4