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<algorithm>
using namespace std;
typedef long long ll;
struct Fraction{
ll up,down;
}a,b; ll gcd(ll a,ll b){
return b == ? a : gcd(b,a%b);
} Fraction reduction(Fraction result){
if(result.down < ){
result.down = -result.down;
result.up = -result.up;
}
if(result.up == ){
result.down = ;
}else{
int d = gcd(abs(result.up),abs(result.down));
result.down /= d;
result.up /= d;
}
return result;
} Fraction add(Fraction f1,Fraction f2){
Fraction result;
result.up = f1.up*f2.down + f1.down*f2.up;
result.down = f1.down*f2.down;
return reduction(result);
} Fraction muli(Fraction f1,Fraction f2){
Fraction result;
result.up = f1.up*f2.down - f1.down*f2.up;
result.down = f1.down*f2.down;
return reduction(result);
} Fraction multi(Fraction f1,Fraction f2){
Fraction result;
result.up = f1.up*f2.up;
result.down = f1.down*f2.down;
return reduction(result);
} Fraction divide(Fraction f1,Fraction f2){
Fraction result;
result.up = f1.up * f2.down;
result.down = f1.down * f2.up;
return reduction(result);
} void showResult(Fraction r){
r = reduction(r);
if(r.up < ) printf("(");
if(r.down == ) printf("%lld",r.up);
else if(abs(r.up) > r.down){
printf("%lld %lld/%lld",r.up/r.down,abs(r.up)%r.down,r.down);
}else{
printf("%lld/%lld",r.up,r.down);
}
if(r.up < ) printf(")");
} int main(){
scanf("%lld/%lld %lld/%lld",&a.up,&a.down,&b.up,&b.down); showResult(a);
printf(" + ");
showResult(b);
printf(" = ");
showResult(add(a,b));
printf("\n"); showResult(a);
printf(" - ");
showResult(b);
printf(" = ");
showResult(muli(a,b));
printf("\n"); showResult(a);
printf(" * ");
showResult(b);
printf(" = ");
showResult(multi(a,b));
printf("\n"); showResult(a);
printf(" / ");
showResult(b);
printf(" = ");
if(b.up == ) printf("Inf");
else showResult(divide(a,b)); return ;
}

1088 Rational Arithmetic(20 分)的更多相关文章

  1. 【PAT甲级】1088 Rational Arithmetic (20 分)

    题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...

  2. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...

  3. PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

    输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...

  4. PAT (Advanced Level) 1088. Rational Arithmetic (20)

    简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...

  5. 1088. Rational Arithmetic (20)

    1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...

  6. pat1088. Rational Arithmetic (20)

    1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...

  7. PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]

    1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...

  8. PAT 1088 三人行(20 分)(暴力破解+流程分析)

    1088 三人行(20 分) 子曰:"三人行,必有我师焉.择其善者而从之,其不善者而改之." 本题给定甲.乙.丙三个人的能力值关系为:甲的能力值确定是 2 位正整数:把甲的能力值的 ...

  9. PAT Rational Arithmetic (20)

    题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...

随机推荐

  1. Poj 2533 Longest Ordered Subsequence(LIS)

    一.Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...

  2. Poj1218_THE DRUNK JAILER(水题)

    一.Description A certain prison contains a long hall of n cells, each right next to each other. Each ...

  3. Lua常用API

    转自:http://www.cnblogs.com/ringofthec/archive/2010/10/22/lua.html 1.  建一个新表 void lua_createtable (lua ...

  4. js点滴知识(1) -- 获取DOM对象和编码

    在今天的工作中发现了一些小的问题,在网上查了一下,才知道自己的js才是冰山一角,以后要虚心向他人学习,要虚怀若谷. 发现一:js获取DOM对象与jquery的区别 先前总以为,二者是一样的,最近才知道 ...

  5. 问题:C# Dictionary嵌套使用;结果:嵌套Dictionary添加 , C#2.0泛型详细介绍

    Dictionary<int, Dictionary<string, string>> dict1 = new Dictionary<int, Dictionary< ...

  6. javaScript之Array方法

    Array类型和其他语言一样,是数据的有序列表,但不同的是数组的每一项们可以保存任何类型的数据. 1.检测方法(确定某个对象是不是数组) (1)value instanceof Array (2)Ar ...

  7. Flask06 地址约定、利用falsk提供的方法渲染模板

    1 访问地址约定 在访问路径的末尾是否需要加 / -> 可以加,也可以不加 前端的访问路径必须和后端的路径完全匹配才能够访问成功,如果我们在后台的路径都是以 / 结尾,但是我们的访问路径是没有以 ...

  8. Asp.net 微信企业号网页开发流程

    一.在pageload方法中获取code var code = GetCode(); private string GetCode() { return HttpContext.Current.Req ...

  9. 使用mahout fpgrowth算法求关联规则

    使用mahout fpgrowth 首先,这篇文章的内容大部分取自国外一篇博客Finding association rules with Mahout Frequent Pattern Mining ...

  10. 通过sessionid提取这个用户在服务器中保存的session变量