Source:

PAT A1088 Rational Arithmetic (20 分)

Description:

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

Keys:

Code:

 /*
Data: 2019-07-05 20:10:35
Problem: PAT_A1088#Rational Arithmetic
AC: 21:04 题目大意:
分数的加减乘除
*/
#include<cstdio>
#include<algorithm>
using namespace std;
struct fr
{
long long up,down;
}s1,s2; long long GCD(long long a, long long b)
{
return !b ? a : GCD(b,a%b);
} fr Reduct(fr r)
{
if(r.down < )
{
r.up = -r.up;
r.down = -r.down;
}
if(r.up==)
r.down=;
else
{
int gcd = GCD(abs(r.up),abs(r.down));
r.up /= gcd;
r.down /= gcd;
}
return r;
} fr Add(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.down+f1.down*f2.up;
r.down = f1.down*f2.down;
return Reduct(r);
} fr Dif(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.down-f1.down*f2.up;
r.down = f1.down*f2.down;
return Reduct(r);
} fr Pro(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.up;
r.down = f1.down*f2.down;
return Reduct(r);
} fr Quo(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.down;
r.down = f1.down*f2.up;
return Reduct(r);
} void Show(fr r)
{
r = Reduct(r);
if(r.up < )
printf("(");
if(r.down == )
printf("%lld", r.up);
else if(abs(r.up) > r.down)
printf("%lld %lld/%lld", r.up/r.down,abs(r.up)%r.down,r.down);
else
printf("%lld/%lld", r.up,r.down);
if(r.up < )
printf(")");
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%lld/%lld %lld/%lld", &s1.up,&s1.down,&s2.up,&s2.down);
char op[] = {'+','-','*','/'};
for(int i=; i<; i++)
{
Show(s1);printf(" %c ", op[i]);Show(s2);printf(" = ");
if(i==) Show(Add(s1,s2));
else if(i==) Show(Dif(s1,s2));
else if(i==) Show(Pro(s1,s2));
else
{
if(s2.up==) printf("Inf");
else Show(Quo(s1,s2));
}
printf("\n");
} return ;
}

PAT_A1088#Rational Arithmetic的更多相关文章

  1. PAT1088:Rational Arithmetic

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

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

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

  3. pat1088. Rational Arithmetic (20)

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

  4. A1088. Rational Arithmetic

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

  5. 1088 Rational Arithmetic(20 分)

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

  6. PAT Rational Arithmetic (20)

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

  7. PAT 1088. Rational Arithmetic

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

  8. PAT甲级——A1088 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. jquery与其他js冲突

    var $j=JQuery.noConflict(); $j('#msg').hide();//此处$j就代表JQuery //重命名以下,把$改为$j

  2. 7.使用mysql_export监控mysql

    ok,docker监控,宿主机CPU.磁盘.网络.内存监控我们都已讲过,是时候讲一波mysql监控了.本次mysql部署在客户端. 架构 客户端 MySql安装 ##下载mysql的repo源: [r ...

  3. leetcode.矩阵.378有序矩阵中第K小的元素-Java

    1. 具体题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1,  5, ...

  4. WEB前端资源集

    原出处:http://www.cnblogs.com/zhengjialux/archive/2017/01/16/6291394.html 资源网站篇 CSDN:全球最大中文IT社区,为IT专业技术 ...

  5. 微信小程序生命周期、页面生命周期、组件生命周期

    1. 生命周期 App(全局) 位置:项目根目录app.js文件 App({ onLaunch (options) { // console.log('小程序初始化') }, onShow(optio ...

  6. Ansible批量自动化管理工具(二)

    Ansible批量自动化管理工具(二) 链接:https://pan.baidu.com/s/1A3Iq3gGkGS27L_Gt37_I0g 提取码:ncy2 复制这段内容后打开百度网盘手机App,操 ...

  7. Dubbox服务的提供方开发

    (1)创建Maven工程(WAR)dubboxdemo-service  ,在pom.xml中引入依赖 <project xmlns="http://maven.apache.org/ ...

  8. Java工程师面试linux操作选择面试题大全

    1.请写出常用的linux指令不低于10个,请写出linux tomcat启动.linux指令arch 显示机器的处理器架构(1)uname -m 显示机器的处理器架构(2)shutdown -h n ...

  9. express 使用art-template模板引擎

    下载express-art-template art-template - app.js中配置 - 注册一个模板引擎 - `app.engine('.html',express-art-templat ...

  10. node js实战:带数据库,加密的注册登录表单

    demo 注册效果: 登陆效果:        数据库截图: 数据库操作 db.js //这个模块里面封装了所有对数据库的常用操作 var MongoClient = require('mongodb ...