牛客网——Rational Arithmetic (20)

牛客网——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.

输入描述
每个输入文件包含一个测试用例,它在一行中给出了格式为“a1/b1 a2/b2”的两个有理数。
分子和分母都在long int的范围内。如果有负号,它必须只出现在分子前面。分母保证是非零数。

输出描述
对于每个测试用例,分别在4行中打印两个有理数的和、差、积和商。
每一行的格式是“number1操作符number2 = result”。注意所有有理数都必须是最简形式“k a/b”,其中k是整数部分,而a/b是最简分数部分。
如果数字是负数,它必须包含在一对括号中。
如果分母中除法为零,输出“Inf”作为结果。它保证所有输出整数都在long int的范围内。

#include <iostream>
#include <cmath>
using namespace std;

// 结构体表示分数
struct fraction
{
    long long up;
    long long down;
};

// 求最大公约数
long long gcd(long long a, long long b)
{
    if(b == 0) 
        return a;
    else 
        return gcd(b, a % b);
}

// 将结果化简为题目要求的形式
fraction reduction(fraction result)
{
    // 分母为负,将符号转移给分子
    if(result.down < 0)
    {
        result.up = -result.up;
        result.down = -result.down;
    }
    if(result.up == 0) 
        result.down = 1;
    else
    {
        // 约分
        long long d = gcd(abs(result.up), abs(result.down));
        result.up /= d;
        result.down /= d;
    }
    return result;
}

// 分数加法运算
fraction add(fraction a, fraction b)
{
    fraction result;
    result.up = a.up * b.down + b.up * a.down;
    result.down = a.down * b.down;
    return result;
}

// 分数减法运算
fraction minu(fraction a, fraction b)
{
    fraction result;
    result.up = a.up * b.down - b.up * a.down;
    result.down = a.down * b.down;
    return result;
}

// 分数乘法运算
fraction multi(fraction a, fraction b)
{
    fraction result;
    result.up = a.up * b.up;
    result.down = a.down * b.down;
    return result;
}

// 分数除法运算
fraction divide(fraction a, fraction b)
{
    fraction result;
    result.up = a.up * b.down;
    result.down = a.down * b.up;
    return result;
}

// 打印数字
void printresult(fraction a)
{
    a = reduction(a);
    // 负号需要带括号
    if(a.up < 0)
        cout << "(";
    // 整数
    if(a.down == 1)
        cout << a.up;
    // 假分数
    else if(abs(a.up) > a.down)
        cout << a.up / a.down << " " << abs(a.up) % a.down << "/" << a.down;
    // 真分数
    else
        cout << a.up << "/" << a.down;
    if(a.up < 0)
        cout << ")";
}

int main()
{
    fraction a, b;
    scanf("%lld/%lld %lld/%lld", &a.up, &a.down, &b.up, &b.down);
    printresult(a); cout << " + "; printresult(b); cout << " = "; printresult(add(a, b)); cout << endl;
    printresult(a); cout << " - "; printresult(b); cout << " = "; printresult(minu(a, b)); cout << endl;
    printresult(a); cout << " * "; printresult(b); cout << " = "; printresult(multi(a, b)); cout << endl;
    printresult(a); cout << " / "; printresult(b); cout << " = ";
    if(b.up == 0) 
        cout << "Inf" << '\n';
    else
    {
        printresult(divide(a,b));
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值