PAT-A-1088. Rational Arithmetic (20)
来源:互联网 发布:java特种兵 下册 pdf 编辑:程序博客网 时间:2024/05/22 03:31
1088. Rational Arithmetic (20)
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/2Sample Output 1:
2/3 + (-2) = (-1 1/3)2/3 - (-2) = 2 2/32/3 * (-2) = (-1 1/3)2/3 / (-2) = (-1/3)Sample Input 2:
5/3 0/6Sample Output 2:
1 2/3 + 0 = 1 2/31 2/3 - 0 = 1 2/31 2/3 * 0 = 01 2/3 / 0 = Inf
#include<iostream>#include<cstdio>#include<math.h>#include<stdio.h>using namespace std;typedef long long LL;struct Fraction{ LL up; LL down;};LL gcd(LL a, LL b){ if (b == 0) return a; else return gcd(b, a%b);}Fraction Reduction(Fraction f){ if (f.down < 0) { f.up = -f.up; f.down = -f.down; } if (f.up == 0) f.down = 1; else { LL d = gcd(abs(f.up), f.down); f.up = f.up / d; f.down = f.down / d; } return f; }Fraction Add(Fraction f1, Fraction f2){ Fraction ans; ans.up = f1.up*f2.down + f2.up*f1.down; ans.down = f2.down*f1.down; return Reduction(ans);}Fraction Reduce(Fraction f1, Fraction f2){ Fraction ans; ans.up = f1.up*f2.down - f2.up*f1.down; ans.down = f2.down*f1.down; return Reduction(ans);}Fraction Multi(Fraction f1, Fraction f2){ Fraction ans; ans.up = f1.up*f2.up; ans.down = f1.down*f2.down; return Reduction(ans);}Fraction Divide(Fraction f1, Fraction f2){ Fraction ans; ans.up = f1.up*f2.down; ans.down = f1.down*f2.up; return Reduction(ans);}void Show(Fraction f){ f = Reduction(f); if (f.up < 0) cout << "("; if (f.down == 1) cout << f.up; else if (abs(f.up)>f.down) { cout << f.up / f.down << " "; cout << abs(f.up) % f.down << "/" << f.down; } else { cout << f.up << "/" << f.down; } if (f.up<0) cout << ")";}int main(){ Fraction f1, f2; scanf("%lld/%lld", &f1.up, &f1.down); scanf("%lld/%lld", &f2.up, &f2.down); Show(f1); cout << " + "; Show(f2); cout << " = "; Show(Add(f1, f2)); cout << endl; Show(f1); cout << " - "; Show(f2); cout << " = "; Show(Reduce(f1, f2)); cout << endl; Show(f1); cout << " * "; Show(f2); cout << " = "; Show(Multi(f1, f2)); cout << endl; Show(f1); cout << " / "; Show(f2); cout << " = "; if (f2.up == 0) cout << "Inf"; else Show(Divide(f1, f2)); cout << endl; system("pause"); return 0;}
- PAT A 1088. Rational Arithmetic (20)
- PAT-A 1088. Rational Arithmetic (20)
- PAT-A-1088. Rational Arithmetic (20)
- Pat(A) 1088. Rational Arithmetic (20)
- PAT-A 1088. Rational Arithmetic
- PAT 1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- 【PAT】1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic
- 【PAT】1088. Rational Arithmetic
- PAT Rational Arithmetic (20)
- 【PAT】【Advanced Level】1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic (20) 模块化
- PAT 甲级 1088. Rational Arithmetic (20)
- Python实现Pat 1088. Rational Arithmetic (20)
- PAT(A) 1088. Rational Arithmetic & PAT(B) 1034. 有理数四则运算(20)
- pat-a1088. Rational Arithmetic (20)
- 你不能做宅男,因为你穷
- android——DrawerLayout
- spring的xml配置文件中获取jdbc.properties乱码导致连接数据库失败
- 在xcode下用openssl写dh密钥交换,遇到“ld: library not found for -lcrypto”问题(已解决)
- bash脚本
- PAT-A-1088. Rational Arithmetic (20)
- linux系统用户免登设置
- 指针练习
- 二叉树的最小深度
- Eum 枚举类的学习
- 剑指offer--从上往下打印二叉树
- android studio查看函数用法、函数解释、函数说明
- 使用libcurl库实现SMTP发送邮件
- NAT的简介