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的更多相关文章
-
PAT甲级——A1088 Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
-
PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
-
PAT1088:Rational Arithmetic
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
-
PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
-
pat1088. Rational Arithmetic (20)
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
-
1088 Rational Arithmetic(20 分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
-
PAT Rational Arithmetic (20)
题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...
-
PAT 1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
-
PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
随机推荐
-
linux install matlab2014a
https://pan.baidu.com/s/1qYJ9tNm#list/path=%2F下载镜像文件 2#开始安装,全程最好断网 sudo mkdir /media/matlab sudo mou ...
-
C#自定义大小与改变大下的方法
在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小.怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴! using System; using System.Wind ...
-
ARM Compiler toolchain Compiler -- Supported ARM architectures
--cpu=name This option enables code generation for the selected ARM processor or architecture. Synta ...
-
C#中静态方法和非静态方法的区别(二)
一.引言 在C#中,静态和非静态的特征对于我们来说是再熟悉不过了,但是很少看到有一篇文章去好好地总结静态和非静态它们之间的不同,为了帮助大家更好地去理解静态和非静态特征, 所以将在这篇文章中帮大家全面 ...
-
第5章 原型模式(Protype Pattern)
原文 第5章 原型模式(Protype Pattern) 定义:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. 原型图: 原型模式主要用于对象的复制,它的核心是就是类图中的原型类Pro ...
-
[DNS]ACL、also-notify、视图服务器及安全设置
1. ACL :访问控制列表放在named.conf的头部,如果acl的内容太多,可以另创建一个文件,将acl放在该文件中,再在主配置文件named.conf用include 将文件加载进来(记得放在 ...
-
浅谈js中的this关键字
---恢复内容开始--- this是JavaScript中的关键字之一,在编写程序的时候经常会用到,正确的理解和使用关键字this尤为重要.接下来,笔者就从作用域的角度粗谈下自己对this关键字的理解 ...
-
C/C++结构体成员偏移量获取
分析代码节选自muduo. 以下代码通过offsetof获取sin_family在sockaddr_in6中的字段偏移量. static_assert(offsetof(sockaddr_in6, s ...
-
MT【201】折线计数
甲乙两人参加竞选,结果甲得n票,乙得m票(n > m) . 则在唱票过程中,甲的累计票数始终超过乙的累计票数的概率是_____________. 答案:$\dfrac{n-m}{n+m}$
-
使用emma时遇到的一些问题
今天在用使用emma的过程中遇到了几个问题,记录一下. 1.跑junit过程中没办法产生coverage data文件,导致最后没办法出emma报告,上官网查了一下原因如下: I have instr ...