Source:

PAT A1088 Rational Arithmetic (20 分)

Description:

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:

  1. 2/3 -4/2

Sample Output 1:

  1. 2/3 + (-2) = (-1 1/3)
  2. 2/3 - (-2) = 2 2/3
  3. 2/3 * (-2) = (-1 1/3)
  4. 2/3 / (-2) = (-1/3)

Sample Input 2:

  1. 5/3 0/6

Sample Output 2:

  1. 1 2/3 + 0 = 1 2/3
  2. 1 2/3 - 0 = 1 2/3
  3. 1 2/3 * 0 = 0
  4. 1 2/3 / 0 = Inf

Keys:

Code:

  1. /*
  2. Data: 2019-07-05 20:10:35
  3. Problem: PAT_A1088#Rational Arithmetic
  4. AC: 21:04
  5.  
  6. 题目大意:
  7. 分数的加减乘除
  8. */
  9. #include<cstdio>
  10. #include<algorithm>
  11. using namespace std;
  12. struct fr
  13. {
  14. long long up,down;
  15. }s1,s2;
  16.  
  17. long long GCD(long long a, long long b)
  18. {
  19. return !b ? a : GCD(b,a%b);
  20. }
  21.  
  22. fr Reduct(fr r)
  23. {
  24. if(r.down < )
  25. {
  26. r.up = -r.up;
  27. r.down = -r.down;
  28. }
  29. if(r.up==)
  30. r.down=;
  31. else
  32. {
  33. int gcd = GCD(abs(r.up),abs(r.down));
  34. r.up /= gcd;
  35. r.down /= gcd;
  36. }
  37. return r;
  38. }
  39.  
  40. fr Add(fr f1, fr f2)
  41. {
  42. fr r;
  43. r.up = f1.up*f2.down+f1.down*f2.up;
  44. r.down = f1.down*f2.down;
  45. return Reduct(r);
  46. }
  47.  
  48. fr Dif(fr f1, fr f2)
  49. {
  50. fr r;
  51. r.up = f1.up*f2.down-f1.down*f2.up;
  52. r.down = f1.down*f2.down;
  53. return Reduct(r);
  54. }
  55.  
  56. fr Pro(fr f1, fr f2)
  57. {
  58. fr r;
  59. r.up = f1.up*f2.up;
  60. r.down = f1.down*f2.down;
  61. return Reduct(r);
  62. }
  63.  
  64. fr Quo(fr f1, fr f2)
  65. {
  66. fr r;
  67. r.up = f1.up*f2.down;
  68. r.down = f1.down*f2.up;
  69. return Reduct(r);
  70. }
  71.  
  72. void Show(fr r)
  73. {
  74. r = Reduct(r);
  75. if(r.up < )
  76. printf("(");
  77. if(r.down == )
  78. printf("%lld", r.up);
  79. else if(abs(r.up) > r.down)
  80. printf("%lld %lld/%lld", r.up/r.down,abs(r.up)%r.down,r.down);
  81. else
  82. printf("%lld/%lld", r.up,r.down);
  83. if(r.up < )
  84. printf(")");
  85. }
  86.  
  87. int main()
  88. {
  89. #ifdef ONLINE_JUDGE
  90. #else
  91. freopen("Test.txt", "r", stdin);
  92. #endif // ONLINE_JUDGE
  93.  
  94. scanf("%lld/%lld %lld/%lld", &s1.up,&s1.down,&s2.up,&s2.down);
  95. char op[] = {'+','-','*','/'};
  96. for(int i=; i<; i++)
  97. {
  98. Show(s1);printf(" %c ", op[i]);Show(s2);printf(" = ");
  99. if(i==) Show(Add(s1,s2));
  100. else if(i==) Show(Dif(s1,s2));
  101. else if(i==) Show(Pro(s1,s2));
  102. else
  103. {
  104. if(s2.up==) printf("Inf");
  105. else Show(Quo(s1,s2));
  106. }
  107. printf("\n");
  108. }
  109.  
  110. return ;
  111. }

PAT_A1088#Rational Arithmetic的更多相关文章

  1. PAT1088:Rational Arithmetic

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

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

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

  3. pat1088. Rational Arithmetic (20)

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

  4. A1088. Rational Arithmetic

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

  5. 1088 Rational Arithmetic(20 分)

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

  6. PAT Rational Arithmetic (20)

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

  7. PAT 1088. Rational Arithmetic

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

  8. PAT甲级——A1088 Rational Arithmetic

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

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

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

随机推荐

  1. PHP面试 PHP基础知识 十一(开发环境及相关配置)

    开发环境及相关配置 版本控制软件 集中式和分布式 集中式:集中式版本控制工具,版本库集中存放在中央服务器,团队成员里的每个人工作时从中央服务器下载代码,个人修改后再提交到中央服务器 分布式:分布式版本 ...

  2. 怎么知道dll文件是哪个net版本

    有时候经常需要查看.dll所使用的.net版本, 因为根本不知道它是使用了1.1还是2.0, 或者是3.0, 这个时候如果需要打开vs.net那又太麻烦, 所以经过长久的摸索, 我找到了一个比较简便的 ...

  3. Windows7下移植Qt4.8.4项目到QT5.2上时遇到的一些问题

        最近在Windows7下将Qt4.8.4+MSVC2008的项目移植到QT5.2下时,遇到了一些小问题: 问题一:错误:C1083: 无法打开包括文件:"QApplication&q ...

  4. MVC5+EF6 完整教程

    随笔分类 - MVC ASP.NET MVC MVC5+EF6 完整教程17--升级到EFCore2.0 摘要: EF Core 2.0上周已经发布了,我们也升级到core 文章内容基于vs2017, ...

  5. Python-数字类型补充

    Python第五节数字类型补充 数字类型转换 int(x) float(x) complex(x) complex(x, y) 数学常量 pi e PS 数字类型不允许改变 也就是说,当我们对数字类型 ...

  6. 【牛客网-剑指offer】变态跳台阶

    题目: 一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 考点: 递归和循环 分析: 台阶数 跳法 1 1 2 2 3 4 4 8 5 1 ...

  7. Grafana的安装配置 和 使用nginx反向代理grafana

    grafana安装和配置 grafana安装非常简单:(https://grafana.com/grafana/download) 对于有apt的服务器: # apt install -y softw ...

  8. windows server 2016 支持多用户远程登录

    服务器设置多用户同时远程桌面,可以提高访问效率,避免人多抢登服务器. 1. 首先需要先安装远程桌面服务  配置组策略,运行框输入gpedit.msc,打开计算机配置–>管理模板—>wind ...

  9. virtualbox ubuntu 磁盘大小扩容

    前言 虚拟机软件: virtuabox 虚拟机运行的系统: Ubuntu 18.04.3 desktop 如果需要扩容的虚拟机在运行中,请将需要扩容的虚拟机进行关机 方法 先设置虚拟机的虚拟硬盘的大小 ...

  10. vue-cli 项目配置

    vue viewport <meta name="viewport" content="width=device-width,initial-scale=1,min ...