1088. Rational Arithmetic (20)
来源:互联网 发布:邪恶网站源码 编辑:程序博客网 时间:2024/05/16 12:46
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 oflong 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
被pat虐成渣了。。。卡第一题上,基础知识不过关,基本的求最大公约数的公式都不记得了,看来下一阶段还得好好补下基础才是啊。
算法没什么好说的,基本的分数运算,但是有几个要注意的地方,一个是注意范围,一个是基本的求最大公约数的公式
代码:
#include <iostream>#include <string>using namespace std;typedef struct fenshu{int sign ;//分数的符号 sign=-1表示负数long int z;//分数的整数部分long int up;//分子long int down;//分母fenshu():sign(1),z(0),up(0),down(1){};}FenShu;bool operator==(FenShu &f,long int x){//判断是否等于某个整数return f.up==0 && f.z*f.sign==x;}long int getPub(long int up,long int down){//最大公约数long int t=1;while(down){t = up % down ;up = down;down = t;}return up;}long int fab(long int x){//绝对值 if(x<0) return -x;else return x;}FenShu getFenShu(long int up,long int down){//化简分数 FenShu f; if(up * down == 0){//分数表示0 return f; } //以下表示分数非0 if(up * down < 0) {//确定符号 f.sign =-1; up = fab(up); down = fab(down); } f.z=up/down; up=up%down; if(up==0){//分数是一个整数 return f; } long int pub=getPub(up,down);//正宗的分数 f.up = up/pub; f.down = down/pub; return f;}void printFenShu(FenShu &f){//输出分数if(f==0) {//分数表示0cout<<"0"; return ;}if(f.sign==-1) cout<<"(-";if(f.z){//有整数cout<<f.z; if(f.up){cout<<" "<<f.up<<"/"<<f.down;}}else if(f.up){cout<<f.up<<"/"<<f.down;}if(f.sign==-1) cout<<")";}void printExpress(FenShu &f1,FenShu &f2,FenShu &f3,char op){//输出表达式printFenShu(f1);cout<<" "<<op<<" ";printFenShu(f2);cout<<" = ";printFenShu(f3);cout<<endl;}void printError(FenShu &f1,FenShu &f2,string &errorInf,char op){//错误的表达式输出printFenShu(f1);cout<<" "<<op<<" ";printFenShu(f2);cout<<" = ";cout<<errorInf;cout<<endl;}FenShu cal(long int a,long int b,long int c,long int d,char op){//计算+ - * /long int up = 0;long int down = b*d;switch(op){case '+':up = a*d+b*c;break;case '-':up = a*d - b*c;break;case '*':up = a*c;break;case '/':up = a*d;down = b*c;break;}FenShu f = getFenShu(up,down); return f;}int main(){long int a,b,c,d;char op;cin>>a>>op>>b>>c>>op>>d;FenShu f1 = getFenShu(a,b);FenShu f2 = getFenShu(c,d);a = f1.sign * (f1.z * f1.down + f1.up);b = f1.down;c = f2.sign * (f2.z * f2.down + f2.up);d = f2.down;FenShu f3 = cal(a,b,c,d,'+');printExpress(f1,f2,f3,'+'); f3 = cal(a,b,c,d,'-');printExpress(f1,f2,f3,'-'); f3 = cal(a,b,c,d,'*');printExpress(f1,f2,f3,'*'); if(f2==0){string error("Inf");printError(f1,f2,error,'/');}else{f3 = cal(a,b,c,d,'/');printExpress(f1,f2,f3,'/');}return 0;}
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- JAVA报Exception in thread "main" java.lang.NoClassDefFoundError的错误
- 体验异军突起APUS桌面启动器
- 第六届蓝桥杯校内选拔赛C/C++高职组解题(1)
- 开启hadoop之旅之搭建环境
- Linux tgtadm: Setup iSCSI Target ( SAN )
- 1088. Rational Arithmetic (20)
- Android中的沙箱模型
- 使 VC2013 编写的程序运行在其它电脑上
- myeclipse 和 eclipse ide for javaee 下的java web项目目录结构对比
- IOS开发---OC语言-③类的设计和简单练习
- 提供给开发者 10 款最好的 Python IDE
- 帧中继的本地管理接口类型(LMI)
- 桶排序及C语言实现
- 第十四周项目——oj平台一——求矩阵对角线元素之和