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>
#include<cstdio>
using namespace std;
typedef struct NODE{
long long up, dn;
int inf;
NODE(){
inf = ;
}
}node;
long long gcd(long long a, long long b){
if(b == )
return a;
else return gcd(b, a % b);
}
node add(node a, node b){
node temp;
temp.dn = a.dn * b.dn;
temp.up = a.up * b.dn + b.up * a.dn;
if(temp.dn < ){
temp.up *= -;
temp.dn *= -;
}
long long fac = gcd(abs(temp.up), abs(temp.dn));
if(fac != ){
temp.up = temp.up / fac;
temp.dn = temp.dn / fac;
}
return temp;
}
node multp(node a, node b){
node temp;
temp.dn = a.dn * b.dn;
temp.up = a.up * b.up;
if(temp.dn < ){
temp.up *= -;
temp.dn *= -;
}
long long fac = gcd(abs(temp.up), abs(temp.dn));
if(fac != ){
temp.up = temp.up / fac;
temp.dn = temp.dn / fac;
}
return temp;
}
node div(node a, node b){
node temp;
temp.dn = a.dn * b.dn;
temp.up = a.up * b.up;
if(temp.dn == ){
temp.inf = ;
return temp;
}
if(temp.dn < ){
temp.up *= -;
temp.dn *= -;
}
long long fac = gcd(abs(temp.up), abs(temp.dn));
if(fac != ){
temp.up = temp.up / fac;
temp.dn = temp.dn / fac;
}
return temp;
}
void show(node a){
if(a.inf == ){
printf("Inf");
}else{
if(a.up < ){
printf("(");
}
if(a.up == ){
printf("");
return;
}
if(abs(a.up) >= abs(a.dn)){
if(abs(a.up) % abs(a.dn) == ){
printf("%lld", a.up / a.dn);
}else{
printf("%lld %lld/%lld", a.up / a.dn, abs(a.up) % abs(a.dn), a.dn);
}
}else{
printf("%lld/%lld", a.up, a.dn);
}
if(a.up < )
printf(")");
}
}
int main(){
long long temp1;
node a, b, c, d;
scanf("%lld/%lld %lld/%lld", &a.up, &a.dn, &b.up, &b.dn);
long long fac = gcd(abs(a.up), abs(a.dn));
if(fac != ){
a.up = a.up / fac;
a.dn = a.dn / fac;
}
fac = gcd(abs(b.up), abs(b.dn));
if(fac != ){
b.up = b.up / fac;
b.dn = b.dn / fac;
}
c = b; c.up *= -;
d = b; swap(d.up, d.dn);
node re1 = add(a, b);
show(a); printf(" + "); show(b); printf(" = "); show(re1);
printf("\n");
node re2 = add(a, c);
show(a); printf(" - "); show(b); printf(" = "); show(re2);
printf("\n");
node re3 = multp(a, b);
show(a); printf(" * "); show(b); printf(" = "); show(re3);
printf("\n");
node re4 = div(a, d);
show(a); printf(" / "); show(b); printf(" = "); show(re4);
cin >> temp1;
return ;
}

总结:

1、只有除法需要检测INF, 乘法不需要。

2、本题需要输出 题目中输入的数字,所以当输入的分数不是最简分数时,需要先将输入化简,再计算、输出。

 

A1088. Rational Arithmetic的更多相关文章

  1. PAT甲级——A1088 Rational Arithmetic

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

  2. PAT_A1088#Rational Arithmetic

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

  3. PAT1088:Rational Arithmetic

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

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

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

  5. pat1088. Rational Arithmetic (20)

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

  6. 1088 Rational Arithmetic(20 分)

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

  7. PAT Rational Arithmetic (20)

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

  8. PAT 1088. 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. k8s使用Glusterfs动态生成pv

    一.环境介绍 [root@k8s-m ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4 ...

  2. Java并发编程之ThreadGroup

    ThreadGroup是Java提供的一种对线程进行分组管理的手段,可以对所有线程以组为单位进行操作,如设置优先级.守护线程等. 线程组也有父子的概念,如下图: 线程组的创建 public class ...

  3. fastjson与各类型的转换

    参考:https://www.cnblogs.com/ceshi2016/p/7381478.html http://www.cnblogs.com/goody9807/p/4244862.html ...

  4. js函数使用prototype和不适用prototype的区别

    js中类定义函数时用prototype与不用的区别 原创 2017年06月05日 12:25:41 标签: 函数 / prototype / class   首先来看一个实例: function Li ...

  5. 我对领域驱动设计(DDD)的学习成果

    领域驱动设计之领域模型 2004年Eric Evans发表Domain-Driven Design – Tackling Complexity in the Heart of Software (领域 ...

  6. 行为驱动开发(BDD) - 深入了解

    行为驱动开发(BDD) - 一个快速的描述和示例 BDD表示乙 ehavior ð里文ð才有发展.用于描述行为的语法是Gherkin. 这个想法是尽可能自然地描述一种语言应该发生什么. 如果你熟悉单元 ...

  7. bzoj1861

    bzoj1861[ZJOI2006]书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本 ...

  8. 学习Linux系统的态度及技巧

    Linux作为一种简单快捷的操作系统,现在被广泛的应用.也适合越来越多的计算机爱好者学习和使用.但是对于Linux很多人可能认为很难,觉得它很神秘,从而对其避而远之,但事实真的是这样么?linux真的 ...

  9. 双击jar文件运行程序

    Java应用程序jar文件可以由 JVM(Java虚拟机)直接执行,只要操作系统安装了JVM便可以运行作为Java应用程序的jar文件.可是,很多朋友遇到一个难题,那就是下载了jar文件以后在Wind ...

  10. PHP——判断数组中是否有重复值并找出重复值

    可以用来测试需要唯一凭据号码的,是否有重复值,不过一般直接使用uuid了,简单粗暴就解决问题,这个就简单的测试生成的数据是否有重复值吧 <?php /* * @Author: wyy * @Da ...