本题要求编写程序,计算 2 个有理数的和、差、积、商。

输入格式:

输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为 0。

输出格式:

分别在 4 行中按照 有理数1 运算符 有理数2 = 结果 的格式顺序输出 2 个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式 k a/b,其中 k 是整数部分,a/b 是最简分数部分;若为负数,则须加括号;若除法分母为 0,则输出 Inf。题目保证正确的输出中没有超过整型范围的整数。

输入样例 1:

2/3 -4/2

输出样例 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

输入样例 2:

5/3 0/6

输出样例 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>
#define LL long long // 数据超过int,必须用long longusing namespace std;LL gcd(LL a, LL b){return a%b==0 ? b : gcd(b, a%b);
}// 输出 a/b
void print(LL a, LL b, LL sign, LL isN, LL n){if (sign == -1) cout << "(-"; // 负数if (isN) cout << n; // 全整数else if (n > 0) cout << n << " " << a << "/" << b; // 带分数else cout << a << "/" << b; // 真分数if (sign == -1) cout << ")";
}int n1 = 0, n2 = 0, n3 = 0; // 整数部分
int sign1 = 1, sign2 = 1, sign3 = 1; // 正负号
bool isN1 = false, isN2 = false, isN3 = false; // 是否是全整数
char  tmp;
LL t, x, y; // 结果void init3(){sign3 = 1;n3 = 0;isN3 = false;
}// e/f + g/h
void add(LL e, LL f, LL g, LL h){x = e*h + f*g;y = f*h;if (x < 0) sign3 = -1, x = -x;if (x%y == 0) isN3 = true, n3 = x / y;else {t = gcd(x, y);x /= t;y /= t;if (x > y){n3 = x / y;x -= n3 * y;}}
}// e/f * g/h
void multiply(LL e, LL f, LL g, LL h){x = e * g; // 分子y = f * h; // 分母if (x < 0) sign3 = -1, x = -x;if (x%y == 0) isN3 = true, n3 = x / y;else {t = gcd(x, y);x /= t;y /= t;if (x > y){n3 = x / y;x -= n3 * y;}}
}LL a, b, c, d;
LL e, f, g, h;int main()
{cin >> a >> tmp >> b >> c >> tmp >> d;e = a, f = b, g = c, h = d;// 分析正负if (a < 0) sign1 = -1, a = -a;if (c < 0) sign2 = -1, c = -c;// 第一个分数 a/bif (a == 0) isN1 = true; // 零else if (a%b == 0) isN1 = true, n1 = a / b; // 整数else {t = gcd(a, b);a /= t;b /= t;e = a * sign1;f = b;if (a > b){ // 化为带分数n1 = a / b;a -= n1 * b;}}// 第二个分数if (c == 0) isN2 = true;else if (c%d == 0) isN2 = true, n2 = c / d;else {t = gcd(c, d);c /= t;d /= t;g = c * sign2;h = d;if (c > d){n2 = c / d;c -= n2 * d;}}if (e==0 && g==0){ // 两个分数都是零cout << "0 + 0 = 0" << endl;cout << "0 - 0 = 0" << endl;cout << "0 * 0 = 0" << endl;cout << "0 / 0 = Inf";} else if (g == 0){ // 第二个分数是零// 加法print(a, b, sign1, isN1, n1);cout << " + 0 = ";print(a, b, sign1, isN1, n1);cout << endl;// 减法print(a, b, sign1, isN1, n1);cout << " - 0 = ";print(a, b, sign1, isN1, n1);cout << endl;// 乘法print(a, b, sign1, isN1, n1);cout << " * 0 = 0" << endl;// 除法print(a, b, sign1, isN1, n1);cout << " / 0 = Inf";} else if (e == 0){ // 第一个分数是零// 加法cout << "0 + ";print(c, d, sign2, isN2, n2);cout << " = ";print(c, d, sign2, isN2, n2);cout << endl;// 减法cout << "0 - ";print(c, d, sign2, isN2, n2);cout << " = ";sign2 = -sign2;print(c, d, sign2, isN2, n2);cout << endl;// 乘法cout << "0 * ";print(c, d, sign2, isN2, n2);cout << " = 0" << endl;// 除法cout << "0 / ";print(c, d, sign2, isN2, n2);cout << " = 0";} else { // 都非零// 加法print(a, b, sign1, isN1, n1);cout << " + ";print(c, d, sign2, isN2, n2);cout << " = ";add(e, f, g, h);print(x, y, sign3, isN3, n3);cout << endl;// 减法init3();print(a, b, sign1, isN1, n1);cout << " - ";print(c, d, sign2, isN2, n2);cout << " = ";add(e, f, -g, h);print(x, y, sign3, isN3, n3);cout << endl;// 乘法init3();print(a, b, sign1, isN1, n1);cout << " * ";print(c, d, sign2, isN2, n2);cout << " = ";multiply(e, f, g, h);print(x, y, sign3, isN3, n3);cout << endl;// 除法init3();print(a, b, sign1, isN1, n1);cout << " / ";print(c, d, sign2, isN2, n2);cout << " = ";if (g < 0) g = -g, h = -h;multiply(e, f, h, g);print(x, y, sign3, isN3, n3);}return 0;
}

1034 有理数四则运算 (20分)相关推荐

  1. C++学习之路 | PTA乙级—— 1034 有理数四则运算 (20 分)(精简)

    1034 有理数四则运算 (20 分) 本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式: 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是 ...

  2. 【2020模拟考试T4】【PAT乙】1034 有理数四则运算 (20分) 测试点2

    problem 1034 有理数四则运算 (20分) 本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式: 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中 ...

  3. 1034 有理数四则运算 (20 分)(c语言)(测试点三)

    本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式: 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前 ...

  4. 【PAT乙级】1034 有理数四则运算 (20 分)

    题目地址 #include<bits/stdc++.h> using namespace std; typedef long long int LL; LL gcd(LL a,LL b) ...

  5. PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)

    PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)  http://www.patest.cn/contests/pat-b-practise/1034 ...

  6. 1034. 有理数四则运算(20)

    本题要求编写程序,计算2个有理数的和.差.积.商. 输入格式: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只 ...

  7. PAT 乙级 1034. 有理数四则运算(20) Java版

    本题要求编写程序,计算2个有理数的和.差.积.商. 输入格式: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只 ...

  8. 1034. 有理数四则运算(20)-PAT乙级真题

    本题要求编写程序,计算2个有理数的和.差.积.商. 输入格式: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只 ...

  9. Pat乙级 1034 有理数四则运算

    Pat乙级 1034 有理数四则运算 思路 代码 题目网址 https://pintia.cn/problem-sets/994805260223102976/problems/99480528762 ...

最新文章

  1. 【JavaSE】双向链表的实现与讲解
  2. 【STM32】端口复用和重映射,完全重映射,部分重映射
  3. public class UserServiceImpl extends ServiceImpl UserMapper, User implements UserService
  4. linux虚拟网络设备之,Linux虚拟网络设备
  5. -bash: lsof: 未找到命令
  6. mavonEditor 有序无序列表不能显示数字和小原点的问题
  7. Python2 包的安装
  8. NeurlPS2021 | 视觉语言导航的课程学习
  9. 网络通信数据传输原理
  10. 计算机毕业设计springboot家政管理系统
  11. 【路由器】Breed 介绍、刷入和使用
  12. 压力单位PSI,PSIG, PSIA的区别
  13. 分布式监控系统——Zabbix(2)部署
  14. C++ 求解最小公倍数
  15. Android Notification.setLatestEventInfo(...)
  16. Anaconda之通过可视化界面配置虚拟环境
  17. 打造最强终端之一:Fish shell简明教程
  18. 端午节书法作品楷书内容_端午节毛笔字
  19. 2022年在中国大陆通过Azure的学生认证方法指北
  20. 13.Python常用第三方库—tabulate库的使用

热门文章

  1. 我们究竟为什么上大学
  2. 解决SiamRPN预训练模型的问题
  3. 【Redis】五种数据类型及其使用场景
  4. leetcode 数组独数问题
  5. 这几款实用的电脑软件推荐给你
  6. linux tod时钟,什么是ToD服务器,“时间”(非NTP)
  7. 数据结构与算法-打印直角三角形算法
  8. 9-29微信功能思维导图
  9. Word2016设置护眼色
  10. 说说2020年3个最关键的技术趋势,得先机者,得天下机会唾手可得