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. Neo4j和Elasticsearch

    Neo4j和Elasticsearch Neo4j和Elasticsearch是一种让人眼前一亮的组合,为什么需要把搜索和图表结合起来呢?它们是如何使用的呢? 在无处不在的互联网搜索引擎的推动下,全文 ...

  2. 在Logstash的配置文件中对日志事件进行区分

    1.多个日志文件作为输入源 input { # 通过给日志事件定义类型来区分 file { path => ["/var/log/nginx/access.log"] typ ...

  3. Ubuntu 最简单的方式安装chrome

    1.指定安装目录如下: cd opt/ 2.下载包: sudo wget https://dl.google.com/linux/direct/google-chrome-stable_current ...

  4. 2018-10-29-微软-Tech-Summit-技术暨生态大会课程-·-基于-Roslyn-打造高性能预编译框架...

    title author date CreateTime categories 微软 Tech Summit 技术暨生态大会课程 · 基于 Roslyn 打造高性能预编译框架 lindexi 2018 ...

  5. Leetcode274.H-IndexH指数

    原题的中文翻译不是很好,所以给出英文版. Given an array of citations (each citation is a non-negative integer) of a rese ...

  6. 能轻松背板子的FWT(快速沃尔什变换)

    FWT应用 我不知道\(FWT\)的严格定义 百度百科和维基都不知道给一坨什么****东西** FWT(Fast Walsh Fransform),中文名快速沃尔什变换 然后我也不知道\(FWT\)到 ...

  7. DEDE采集时自动生成摘要和关键字

    1.修改 include/dedecollection.class.php //自动分析关键字和摘要 preg_match("/<meta[\s]+name=['\"]key ...

  8. 用pymysql实现的注册登录公告练习

    import pymysql #1.连接服务器 conn=pymysql.connect( host='127.0.0.1', port=3306, user='root', password='12 ...

  9. 关于延迟加载(lazy)和强制加载(Hibernate.initialize(Object proxy) )

    PO 即Persistence Object VO 即Value Object PO 和VO 是Hibernate 中两个比较关键的概念. 首先,何谓VO,很简单,VO 就是一个简单的值对象. 如:  ...

  10. cordova开发笔记

    搜狐邮箱APP 使用了cordova框架,遇到了一些列问题,稍微总结记录下 扩展支持appInBrowser,用来以新窗口方式打开外链url 解决跨域问题(cordova默认当前域为localhost ...