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 ;
}

最新文章

  1. Netty(二)入门
  2. MVC架构模式分析与设计(一)---简单的mvc架构
  3. 4-Spark高级数据分析-第四章 用决策树算法预测森林植被
  4. ArrayList用法
  5. sql存储过程传入ID集合,和临时表的使用
  6. Cocos2d-x 3.1.1 学习日志9--一“上一下其乐无穷”游戏开发系列一
  7. 利用MyEclipse的ant插件生成Hibernate的映射文件
  8. jquery 让指定导航隐藏
  9. mysql 查询重复的(不区分大小写)数据的SQL优化
  10. WPF 绑定
  11. Log4j2 — Log4j2导入、LogEvent、配置文件编写及路径
  12. 用一个简单的例子比较SVM,MARS以及BRUTO(R语言)
  13. java(MyEclipse)创建webservice和测试webservice
  14. ffdshow 源代码分析 9: 编解码器有关类的总结
  15. loj2880「JOISC 2014 Day3」稻草人
  16. JavaScript学习笔记之数组(二)
  17. Lua基本语法-lua与C#的交互(相当简单详细的例子)
  18. Linux下分析磁盘镜像
  19. python 字体颜色的设置
  20. Linux实战教学笔记22:企业级NFS网络文件共享服务

热门文章

  1. IK的整个分词处理过程
  2. StringBuilder 和 StringBuffer类
  3. smb中继攻击
  4. 随笔记录 MBR扇区故障系统备份与还原 2019.8.7
  5. Aop 简单实例
  6. 【学术篇】NOIP2017 d2t3 列队phalanx splay做法
  7. linux dmesg命令使用
  8. 登录操作(方法一:设置flag标志位)
  9. MySQL Download
  10. thinkphp 静态路由