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>
using namespace std;
long long dv, res1, res2;
long long DIV(long long a, long long b)
{
if (b == )
return abs(a);
return DIV(b, a%b);
}
void print(long long a1, long long b1, long long a2, long long b2, char c)
{
if (a1 == )
printf("%d %c ", , c);
else
{
printf("%s", a1 > ? "" : "(");
dv = DIV(a1, b1);
a1 /= dv;
b1 /= dv; if (a1 / b1 != )
printf("%d", a1 / b1);
if (a1 - b1 * (a1 / b1) != )
printf("%s%d/%d", a1 / b1 != ? " " : "", a1 / b1 != ? abs(a1 - b1 * (a1 / b1)) : a1, b1);
printf("%s %c ", a1 > ? "" : ")", c);
} if(a2 == )
printf("%d %s ", , "=");
else
{
printf("%s", a2 > ? "" : "(");
dv = DIV(a2, b2);
a2 /= dv;
b2 /= dv;
if (a2 / b2 != )
printf("%d", a2 / b2);
if (a2 - b2 * (a2 / b2) != )
printf("%s%d/%d", a2 / b2 != ? " " : "", a2 / b2 != ? abs(a2 - b2 * (a2 / b2)) : a2, b2);
printf("%s %s ", a2 > ? "" : ")", "=");
} if (res1 == )
{
printf("%d\n",);
return;
}
else if (res2 == )
{
printf("Inf\n");
return;
}
printf("%s", res1 > ? "" : "(");
dv = DIV(res1, res2);
res1 /= dv;
res2 /= dv;
if (res1 / res2 != )
printf("%d", res1 / res2);
if (res1 - res2 * (res1 / res2) != )
printf("%s%d/%d", res1 / res2 != ? " " : "", res1 / res2 != ? abs(res1 - res2 * (res1 / res2)) : res1, res2);
printf("%s\n", res1 > ? "" : ")");
}
int main()
{
char c;
long long a1, b1, a2, b2;
cin >> a1 >> c >> b1 >> a2 >> c >> b2;
// +
res1 = a1 * b2 + a2 * b1;
res2 = b1 * b2;
print(a1, b1, a2, b2, '+');
// -
res1 = a1 * b2 - a2 * b1;
res2 = b1 * b2;
print(a1, b1, a2, b2, '-');
// *
res1 = a1 * a2;
res2 = b1 * b2;
print(a1, b1, a2, b2, '*');
// /
res1 = a2 > ? a1 * b2 : a1 * b2*-;
res2 = b1 * abs(a2);
print(a1, b1, a2, b2, '/');
return ;
}

PAT甲级——A1088 Rational Arithmetic的更多相关文章

  1. A1088. Rational Arithmetic

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

  2. PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)

    https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...

  3. PAT甲级——A1081 Rational Sum

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

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

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

  5. PAT_A1088#Rational Arithmetic

    Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...

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

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

  7. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT甲级题分类汇编——计算

    本文为PAT甲级分类汇编系列文章. 计算类,指以数学运算为主或为背景的题. 题号 标题 分数 大意 1058 A+B in Hogwarts 20 特殊进制加法 1059 Prime Factors ...

  9. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

随机推荐

  1. Unity中FSM有限状态机

    什么是FSM FSM 即有限状态机,它是一个状态管理系统,表示一个对象的几种状态在指定条件下转移行为,即随着条件的不断改变内部状态不断地切换. FSM用处或者使用背景 通常使用FSM去实现一些简单的A ...

  2. JDBC_数据库连接池c3p0

    /** * @Description: TODO(这里用一句话描述这个类的作用) * @Author aikang * @Date 2019/8/26 20:12 */ /* 1.数据库连接池: 1. ...

  3. Django ORM 之F、Q查询与事务

    返回ORM目录 Django ORM 内容目录 一.F.Q查询 二.事务 三.only与defer 一.F.Q查询 """ Product表中的数据: 1 橡皮 2 20 ...

  4. The linux command之环境

    一.环境 shell在环境中存储了两种数据类型:环境变量(environment variables )shell变量(shell variables).在shell中这两种变量基本没有什么不同. 此 ...

  5. js隐式类型转换,预编译、递归、作用域,作用域链、闭包、立即执行函数、继承圣杯模式

    隐式类型转换 调用Number()当有运算符(加减乘除,求余)时,会调用Number()转为数字再运算,除了 加 当 有字符串时就变身成拼接Boolean();String(); typeof()st ...

  6. Java集合框架(List,Set,Map)

    单列集合基本框架 List接口特点:1. 它是一个元素存取有序的集合.例如,存元素的顺序是11.22.33.那么集合中,元素的存储就是按照11.22.33的顺序完成的). 2. 它是一个带有索引的集合 ...

  7. Java Collection - HashMap

    HashMap源码解析 java.util.HashMap 类 https://www.cnblogs.com/ysocean/p/8711071.html HashMap线程不安全的原因 https ...

  8. 【JZOJ4905】【BZOJ4720】【luoguP1850】换教室

    description 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节内容相同的课 ...

  9. nginx反向代理时有无”/”的辨析

    nginx反向代理是日常使用nginx时最常用到的功能之一.在配置url的过程中,“/”的有无经常是影响我们配置成功的关键,也是困扰我们的问题所在.在此,结合实际例子,作简要辨析. 场景一:利用根目录 ...

  10. 阿里云图数据库GraphDB上线,助力图数据处理

    GraphDB简介 GraphDB图数据库适用于存储,管理,查询复杂并且高度连接的数据,图库的结构特别适合发现大数据集下数据之间的共性和特性,特别善于释放蕴含在数据关系之间的巨大价值.GraphDB引 ...