1088 Rational Arithmetic(20 分)(C++)

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

解题思路:这个20分有点吃力(。-ω-)zzz强烈建议先去食用1081

分数的化简:simplify2——将分数化简为不可约分的假分数,simplify1——化为最终结果带分数.因为带分数在运算时很不方便,所以在运算前只把分数变为假分数,运算后再变成带分数

分数的运算:模拟平时的通分,主要是除法的时候,要注意两点,不能把符号带到分母上,所以遇到负分数要先转换,其次除数为0的情况。

#include <iostream>
#include <cmath>
#include <vector> 
using namespace std;
long long int gcd(long long int a, long long int b){
    return b == 0 ? a : gcd(b,a % b);
}
typedef struct 
{
    long long int ing = 0, num = 0,dem = 1;   
}ration;
void simplify2(ration &a){
    long long int temp = gcd(abs(a.num),a.dem);
    a.num /= temp;
    a.dem /= temp;
}
void simplify1(ration &a){
    a.ing += (a.num/a.dem);
    a.num = a.num%a.dem;
    simplify2(a);
}
void printration(ration ans){
    if(ans.dem == 0){
        printf("Inf");
        return;
    }
    if(ans.ing < 0 || ans.num < 0)
        printf("(");
    if(ans.ing != 0 || ans.num == 0)
        printf("%lld",ans.ing);
    if(ans.num != 0 && ans.ing != 0)
        printf(" %lld/%lld",abs(ans.num),ans.dem);
    else if(ans.num != 0 && ans.ing == 0)
        printf("%lld/%lld",ans.num,ans.dem);
    if(ans.ing < 0 || ans.num < 0)
        printf(")");
}
ration add(ration a, ration b){
    ration ans;
    ans.num = a.num*b.dem + a.dem*b.num;
    ans.dem = a.dem * b.dem;
    simplify1(ans);
    return ans;
}
ration mius(ration a, ration b){
    ration ans;
    ans.num = a.num*b.dem - a.dem*b.num;
    ans.dem = a.dem * b.dem;
    simplify1(ans);
    return ans;
}
ration muli(ration a, ration b){
    ration ans;
    ans.num = a.num * b.num;
    ans.dem = a.dem * b.dem;
    simplify1(ans);
    return ans;
}
ration div(ration a, ration b){
    if(b.num < 0){
        b.dem = -b.dem;
        b.num = abs(b.num);
    }
    ration ans;
    ans.num = a.num * b.dem;
    ans.dem = a.dem * b.num;
    if(ans.dem != 0) 
        simplify1(ans);
    return ans;
}
int main(){
    string op = "+-*/";
    ration a,b;
    std::vector<ration> result(4);
    scanf("%lld/%lld %lld/%lld",&a.num,&a.dem,&b.num,&b.dem);
    simplify2(a); simplify2(b);
    result[0] = add(a,b);
    result[1] = mius(a,b);
    result[2] = muli(a,b);
    result[3] = div(a,b);
    simplify1(a); simplify1(b);
    for(int i = 0; i < 4; i++){
        printration(a); printf(" %c ",op[i]);
        printration(b); printf(" = ");
        printration(result[i]); printf("\n");
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值