1088 Rational Arithmetic (20 分)(分数四则运算)

本文介绍了一种基于C++的有理数运算实现方法,包括加、减、乘、除等基本算术操作。通过输入两个有理数,程序能够计算并输出其最简形式的和、差、积及商,特别处理了除数为零的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cctype>
#include <string.h>
#include <cstdio>
#include <unordered_set>
using namespace std;
long long a,b,c,d;
long long gcd(long long t1,long long t2){
    return t2==0?t1:gcd(t2,t1%t2);
}
void func(long long m,long long n){
    int flag1=0,flag2=0,flag=0;
    if(n==0){
        printf("Inf");
        return;
    }
    if(m==0){
        printf("0");
        return;
    }
    if(m<0) flag1=1;
    if(n<0) flag2=1;
    m=abs(m);
    n=abs(n);
    if (flag1 == 1 && flag2 == 1) flag = 0;
    else if (flag1 == 1 || flag2 == 1) flag = 1;
    if (m == n) {
        if (flag == 1) printf("(-1)");
        else printf("1");
        return;
    }
    long long x=m%n,y=m/n;
    if (x == 0) {
        if (flag == 0) printf("%d", y);
        else printf("(-%d)", y);
        return ;
    }else{
        long long t1=m-y*n,t2=n,t=gcd(t1,t2);
        t1/=t;
        t2/=t;
        if (flag == 1) {
            printf("(-");
            if (y != 0) printf("%lld %lld/%lld)", y, t1, t2);
            else printf("%d/%d)", t1, t2);
        } else {
            if (y != 0) printf("%lld %lld/%lld", y, t1, t2);
            else printf("%lld/%lld", t1, t2);
        }
    }
}
void print() {
    func(a, b); printf(" + "); func(c, d); printf(" = "); func(a * d + b * c, b
                                                               * d); printf("\n");
    func(a, b); printf(" - "); func(c, d); printf(" = "); func(a * d - b * c, b
                                                               * d); printf("\n");
    func(a, b); printf(" * "); func(c, d); printf(" = "); func(a * c, b * d);
    printf("\n");
    func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c);
    printf("\n");
}
int main(){
    scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
    print();
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小火汁猛猛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值