PAT A1088

  • 题目:
    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 <cstdio>

//#include <cmath>
#include <algorithm>
using namespace std;
struct Fraction{
    long long  up;
    long long  down;
};
long long  gcb(long long  a, long long  b){     //求最大公约数
    return b == 0 ? a : gcb(b, a % b);
}
Fraction reduction(Fraction a){                //化简
    if(a.down < 0){
        a.down = -a.down;
        a.up = -a.up;
    }
    if(a.up == 0)
        a.down = 1;
    else{
        long long  temp = gcb(abs(a.up), abs(a.down));    //求出上下系数的最大公约数
        a.up /= temp;
        a.down /= temp;
    }
    return a;
}
//分数加法
Fraction add(Fraction a, Fraction b){
    Fraction res;
    res.up = a.down * b.up + a.up * b.down;
    res.down = a.down * b.down;
    return reduction(res);
}

//分数减法
Fraction minu(Fraction a, Fraction b){
    Fraction res;
    res.up = a.up * b.down - a.down * b.up;
    res.down  = a.down * b.down;
    return reduction(res);
}

//分数乘法
Fraction multi(Fraction a, Fraction b){
    Fraction res;
    res.up = a.up * b.up;
    res.down = a.down * b.down;
    return reduction(res);
}

//分数除法
Fraction divid(Fraction a, Fraction b){
    Fraction res;
    res.up = a.up * b.down;
    res.down = a.down * b.up;
    return reduction(res);
}
void showRes(Fraction res){
    res = reduction(res);
    if(res.up < 0)                         //若为负数
        printf("(");
    if(res.down == 1){                                //整数
        printf("%lld", res.up);
    }
    else if(abs(res.up) > res.down){                 //假分数
        printf("%lld %lld/%lld",res.up/ res.down,  abs(res.up)%res.down, res.down);
    }
    else{
        printf("%lld/%lld", res.up, res.down);            //真分数
    }
    if(res.up < 0)
        printf(")");
}
int main()
{
    Fraction a, b, res1, res2, res3, res4;
    char temp1;
    scanf("%lld%c%lld", &a.up, &temp1, &a.down);
    scanf("%lld%c%lld", &b.up, &temp1, &b.down);
    res1 = add(a, b);
    res2 = minu(a,b);
    res3 = multi(a,b);
    res4 = divid(a,b);
    int temp = 1;
    while(temp <=4){
        showRes(a);
        if(temp == 1)
            printf(" + ");
        else if(temp == 2)
            printf(" - ");
        else if(temp == 3)
            printf(" * ");
        else
            printf(" / ");
        showRes(b);
        printf(" = ");
        if(temp == 1)
            showRes(res1);
        else if(temp == 2)
            showRes(res2);
        else if(temp == 3)
            showRes(res3);
        else {
            if(b.up == 0)
                printf("Inf");
            else
                showRes(res4);
        }
        printf("\n");
        temp++;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值