码迷,mamicode.com
首页 > 其他好文 > 详细

1088. Rational Arithmetic (20)

时间:2015-12-06 13:03:13      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:


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

 

本题要求编写程序,计算2个有理数的和、差、积、商。

输入格式:

输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为0。

输出格式:

分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中没有超过整型范围的整数。

输入样例1:
2/3 -4/2
输出样例1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
输入样例2:
5/3 0/6
输出样例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

 
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <math.h>
  5. #pragma warning(disable:4996)
  6. using namespace std;
  7. long long int Gongyue(long long int x, long long int y) {
  8. long long int m = x%y;
  9. while (m)
  10. {
  11. x = y;
  12. y = m;
  13. m = x%y;
  14. }
  15. return y;
  16. }
  17. void PrintRationalNum(long long int fenzi, long long int fenmu) {
  18. long long int min;
  19. int fuhaoweizheng = 0;
  20. if (fenzi*fenmu >= 0)
  21. fuhaoweizheng = 1;
  22. fenzi = abs(fenzi);
  23. fenmu = abs(fenmu);
  24. if (fenzi*fenmu != 0) {
  25. if (fenzi <= fenmu) {
  26. min = Gongyue(fenmu, fenzi);
  27. }
  28. else {
  29. min = Gongyue(fenzi, fenmu);
  30. }
  31. fenzi /= min;
  32. fenmu /= min;
  33. }
  34. if (fuhaoweizheng> 0) {
  35. if (fenzi%fenmu == 0) {
  36. cout << fenzi / fenmu;
  37. }
  38. else if (abs(fenzi) > abs(fenmu)) {
  39. cout << fenzi / fenmu;
  40. cout << ‘ ‘;
  41. cout << abs(fenzi % fenmu);
  42. cout << ‘/‘;
  43. cout << fenmu;
  44. }
  45. else {
  46. cout << fenzi << "/" << fenmu;
  47. }
  48. }
  49. else {
  50. cout << "(-";
  51. if (fenzi%fenmu == 0) {
  52. cout << fenzi / fenmu;
  53. }
  54. else if (abs(fenzi) > abs(fenmu)) {
  55. cout << fenzi / fenmu;
  56. cout << ‘ ‘;
  57. cout << abs(fenzi % fenmu);
  58. cout << ‘/‘;
  59. cout << fenmu;
  60. }
  61. else {
  62. cout << fenzi << "/" << fenmu;
  63. }
  64. cout << ")";
  65. }
  66. }
  67. int main(void) {
  68. char a[50];
  69. string s1, s2;
  70. long long int fenzi1, fenmu1;
  71. long long int fenzi2, fenmu2;
  72. scanf("%s", a);
  73. sscanf(a, "%lld/%lld", &fenzi1, &fenmu1);
  74. scanf("%s", a);
  75. sscanf(a, "%lld/%lld", &fenzi2, &fenmu2);
  76. long long int min;
  77. if (fenzi1*fenmu1 != 0) {
  78. if (abs(fenzi1) <= abs(fenmu1)) {
  79. min = Gongyue(abs(fenmu1), abs(fenzi1));
  80. }
  81. else {
  82. min = Gongyue(abs(fenzi1), abs(fenmu1));
  83. }
  84. fenzi1 /= min;
  85. fenmu1 /= min;
  86. }
  87. if (fenzi2*fenmu2 != 0) {
  88. if (abs(fenzi2) <= abs(fenmu2)) {
  89. min = Gongyue(abs(fenmu2), abs(fenzi2));
  90. }
  91. else {
  92. min = Gongyue(abs(fenzi2), abs(fenmu2));
  93. }
  94. fenzi2 /= min;
  95. fenmu2 /= min;
  96. }
  97. long long int hefenzi, hefenmu;
  98. long long int chafenzi, chafenmu;
  99. long long int jifenzi, jifenmu;
  100. long long int shangfenzi, shangfenmu;
  101. hefenzi = fenzi1*fenmu2 + fenzi2*fenmu1;
  102. hefenmu = fenmu1*fenmu2;
  103. chafenzi = fenzi1*fenmu2 - fenzi2*fenmu1;
  104. chafenmu = fenmu1*fenmu2;
  105. jifenzi = fenzi1*fenzi2;
  106. jifenmu = fenmu1*fenmu2;
  107. PrintRationalNum(fenzi1, fenmu1);
  108. cout << " + ";
  109. PrintRationalNum(fenzi2, fenmu2);
  110. cout << " = ";
  111. PrintRationalNum(hefenzi, hefenmu);
  112. cout << endl;
  113. PrintRationalNum(fenzi1, fenmu1);
  114. cout << " - ";
  115. PrintRationalNum(fenzi2, fenmu2);
  116. cout << " = ";
  117. PrintRationalNum(chafenzi, chafenmu);
  118. cout << endl;
  119. PrintRationalNum(fenzi1, fenmu1);
  120. cout << " * ";
  121. PrintRationalNum(fenzi2, fenmu2);
  122. cout << " = ";
  123. PrintRationalNum(jifenzi, jifenmu);
  124. cout << endl;
  125. if (fenzi2 == 0) {
  126. PrintRationalNum(fenzi1, fenmu1);
  127. cout << " / ";
  128. PrintRationalNum(fenzi2, fenmu2);
  129. cout << " = Inf" << endl;
  130. }
  131. else {
  132. shangfenzi = fenzi1*fenmu2;
  133. shangfenmu = fenzi2*fenmu1;
  134. PrintRationalNum(fenzi1, fenmu1);
  135. cout << " / ";
  136. PrintRationalNum(fenzi2, fenmu2);
  137. cout << " = ";
  138. PrintRationalNum(shangfenzi, shangfenmu);
  139. cout << endl;
  140. }
  141. return 0;
  142. }





1088. Rational Arithmetic (20)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!