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. 弹出框中的AJAX分页

    $(function() { $("body").on("click",".set-topic",function(){ /*获取所有题目接 ...

  2. upc组队赛5 Assembly Required【思维】

    Assembly Required 题目描述 Princess Lucy broke her old reading lamp, and needs a new one. The castle ord ...

  3. firefox error downloading

    转自:http://blog.csdn.net/feigeswjtu/article/details/42146285 做过互联网开发的都知道,firefox是我们互联网开发必备浏览器之一,浏览器是载 ...

  4. VMware新建虚拟机之后的初始化工作

    一.开启网络功能(后面的ifcfg-ens33自身系统不同) vi /etc/sysconfig/network-scripts/ifcfg-ens33 ONBOOT=yes systemctl re ...

  5. 递归,装饰器,python常用内置方法

    **递归**        def calc(n):            print(n)            if int(n / 2) == 0:  条件判断                r ...

  6. Notepad++中直接运行python

    点击进入幕布视图浏览 https://mubu.com/doc/a8VGCUfqqw 一.使用Notepad++编辑python文件,并直接运行 1.用notepad++打开python文件.或者新建 ...

  7. php常见的验证方法

    php常见的验证方法 干货文章 ·2018-03-16 23:50:36 <?php /** * @param $id * @return false|int * 检测id */ functio ...

  8. 【教程】CentOS 7安装 最新版本Docker

    博主最近需要安装Docker,步骤如下: Docker安装官方地址:https://docs.docker.com/install/linux/docker-ce/centos/ 以下命令都是在roo ...

  9. MOS管知识大集

    MOS管 增强型:就是UGS=0V时漏源极之间没有导电沟道,只有当UGS>开启电压(N沟道)或UGS<开启电压(P沟道)才可能出现导电沟道.耗尽型:就是UGS=0V时,漏源极之间存在导电沟 ...

  10. winform程序登陆后关闭登录窗体

    用winform做程序的时候,我们一般都是在Program先启动登录窗体,然后登录成功后才创建主窗体,结果这就导致了登录窗体无法关闭 所以如果我们不在Program的程序入口先创建登录窗体的话就能完美 ...