【PAT】A1088 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

题意

求两个分式之间的加减乘除。对于可以化简成整数的要化简成整数,对于假分数化简成代分数,对于真分数如果分子分母有公因子的,需要化简使分子分母互素。

思路

使用一个结构体Fraction代表分数,对于输入的两个分式a和b,先进行化简。然后循环调用四项运算,输出前对结果进行化简。

代码

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
// 求最大公因数
ll gcd(ll a, ll b){
    return b == 0 ? a : gcd(b, a % b);
}
struct Fraction{
    ll up, down;
}a, b, r;
void reduction(Fraction& f){
    if(f.down < 0){//分母为负数时,分子分母取反
        f.up = - f.up;
        f.down = - f.down;
    }else if(f.down == 0){// 分母为0时,退出函数
        return;
    }
    // 如果分子为0,将分母置为1,否则用最大公因子化简分式
    if(f.up == 0){
        f.down = 1;
    }else{
        ll d = gcd(f.up < 0 ? - f.up : f.up, f.down < 0 ? - f.down : f.down);
        f.up /= d;
        f.down /= d;
    }
}
typedef void (*OPERATE)();
void add(){
    r.up = a.up * b.down + b.up * a.down;
    r.down = a.down * b.down;
}
void my_minus(){
    r.up = a.up * b.down - b.up * a.down;
    r.down = a.down * b.down;
}
void mutiply(){
    r.up = a.up * b.up;
    r.down = a.down * b.down;
}
void divide(){
    r.up = a.up * b.down;
    r.down = a.down * b.up;
}
void print(Fraction f){
    // 分母为0则输出Inf
    if(f.down == 0){
        printf("%s", "Inf");
        return;
    }
    
    // 如果分子是负数则将分子取反,标记为负数,后面输出括号和负号
    bool negative = false;
    if(f.up < 0){
        negative = true;
        f.up = -f.up;
    }
    
    if(negative) printf("%s", "(-");
    
    // 分别输出整数、代分数和真分数
    if(f.up % f.down == 0){
        printf("%lld", f.up / f.down);
    }else if(f.up > f.down){
        printf("%lld %lld/%lld", f.up / f.down, f.up % f.down, f.down);;
    }else{
        printf("%lld/%lld", f.up, f.down);
    }
    
    if(negative) printf("%s", ")");
    
}
int main() {
    OPERATE funs[] = {add, my_minus, mutiply, divide};
    char operators[4] = {'+', '-', '*', '/'};
    scanf("%lld/%lld %lld/%lld", &a.up, &a.down, &b.up, &b.down);
    reduction(a);
    reduction(b);
    for(int i = 0; i < 4; i++){
        funs[i]();
        reduction(r);
        print(a);
        printf(" %c ", operators[i]);
        print(b);
        printf("%s", " = ");
        print(r);
        putchar('\n');
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值