【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
68 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
92 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
83 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
79 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
82 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
86 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
141 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
62 0
|
2月前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
60 2
|
2月前
|
存储 编译器 C++
【c++】类和对象(下)(取地址运算符重载、深究构造函数、类型转换、static修饰成员、友元、内部类、匿名对象)
本文介绍了C++中类和对象的高级特性,包括取地址运算符重载、构造函数的初始化列表、类型转换、static修饰成员、友元、内部类及匿名对象等内容。文章详细解释了每个概念的使用方法和注意事项,帮助读者深入了解C++面向对象编程的核心机制。
111 5