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. 剑指offer(12)

    来两道关于链表链接的题目: 题目一: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 本题要考虑到其中一条链表是空或者两个都是空的情况. 在每个链表安上一 ...

  2. CentOS7 网络NAT模式

    问题:安装完毕ping命令不能用,然后改为桥接模式,ping可以用. 先了解桥接,NAT 的含义. 桥接:在bridged模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它可以访 ...

  3. mysql定时任务event——清理过期数据

    需要删除数据的表名:t_req_log 建表sql CREATE TABLE `t_req_log` ( `id` ) NOT NULL AUTO_INCREMENT, `host` ) DEFAUL ...

  4. 集合转数组的toArray()和toArray(T[] a)方法

    参考:集合转数组的toArray()和toArray(T[] a)方法 1.ArrayList的toArray ArrayList提供了一个将List转为数组的一个非常方便的方法toArray.toA ...

  5. css 浮动问题 display显示 和 光标设置cursor

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>浮 ...

  6. 二、kubernetes

    一.kubernetes(简称k8s) 集群示意图 Kubernetes工作模式server-client,Kubenetes Master提供集中化管理Minions.部署1台Kubernetes ...

  7. XCTF 4th-WHCTF-2017 creakme

    exe文件 运行一下 随便输一下 ps.这个曹操身边的故事挺有意思的 但是没啥卵用....... 查一下壳无壳 ida载入 发现找不到main函数 直接看start感觉逻辑乱乱的(萌新求不喷..... ...

  8. react为按钮绑定点击事件和修改属性值

    注意点:1.事件名称由react提供,所以事件名首字母大写.比如onClick,onMouseOver. 2.为事件提供的处理函数,格式必须是onClick={function},没有小括号. 3.绑 ...

  9. SharePoint 2013 使用 RBS 功能将二进制大型对象 BLOB 存储在内容数据库外部。

    为每个内容数据库设置 BLOB 存储   启用并配置 FILESTREAM 之后,请按照以下过程在文件系统中设置 BLOB 存储.必须为要对其使用 RBS 的每个内容数据库设置 BLOB 存储. 设置 ...

  10. Mac下安装MySQL(Mac 10.12)

    系统:Mac OS 10.12 MySQL:5.7.15 前言: 安装mysql有两种方式:1为官方下载dmg安装包.2为使用brew进行安装. 安装步骤: 一.官方下载dmg安装包进行安装 1.登陆 ...