【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
76 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
107 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
95 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
95 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
85 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
90 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
154 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
67 0
|
2天前
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)
|
1月前
|
C++ 芯片
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
68 19