题目描写叙述

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, 

product and quotient.

输入描写叙述:

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.

输出描写叙述:

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.

输入样例:

5/3 0/6

输出样例:

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>
#include <cmath> using namespace std; //分数的结构体
typedef struct fraction
{
int isNegative; //正负号
long int i,numerator,denominator; //整数部分、分子、分母
//初始化分数
fraction():isNegative(1),i(0),numerator(0),denominator(1)
{ };
}fraction; //重载 == 操作符
bool operator==(fraction& f, long int x)
{
return f.numerator==0 && f.i*f.isNegative==x;
} //myabs_求绝对值
long int myabs(long int x)
{
if(x<0)
return -x;
return x;
} //将分数转换为要求的形式
fraction simply(long int lhs,long int rhs)
{
fraction temp; if(0==lhs*rhs)
return temp; if(lhs*rhs<0)
{
lhs=myabs(lhs);
rhs=myabs(rhs);
temp.isNegative=-1;
} //化简为整数+分数形式
temp.i=lhs/rhs;
lhs%=rhs; //假设化简后,分子为0。如12/3化简后为4,则返回
if(0==lhs)
return temp; temp.numerator=lhs;
temp.denominator=rhs; //找出最大公约数
long int t;
while(rhs%lhs)
{
t=rhs;
rhs=lhs;
lhs=t%lhs;
}
//化为最简形式
temp.numerator/=lhs;
temp.denominator/=lhs; return temp;
} void print(fraction& f)
{
if(f==0)
{
cout<<"0";
return;
}
if(f.isNegative==-1)
cout<<"(-";
if(f.i)
{
cout<<f.i;
if(f.numerator)
cout<<" "<<f.numerator<<"/"<<f.denominator;
}
else if(f.numerator)
{
cout<<f.numerator<<"/"<<f.denominator;
}
if(f.isNegative==-1)
cout<<")";
} void printop(fraction& f1, fraction& f2, fraction& f3,char o)
{
print(f1);
cout<<" "<<o<<" ";
print(f2);
cout<<" = ";
print(f3);
cout<<endl;
} fraction op(long int n1, long int n2,long int n3,long int n4,char o)
{
fraction temp;
long int numerator, denominator;
denominator=n2*n4;
switch(o)
{
case '+':
numerator=n1*n4+n3*n2;
break;
case '-':
numerator=n1*n4-n3*n2;
break;
case '*':
numerator=n1*n3;
break;
case '/':
denominator=n2*n3;
numerator=n1*n4;
break;
}
temp=simply(numerator,denominator);
return temp;
} int main()
{
long int a1,a2,a3,a4;
fraction f1,f2,f3;
char c1,c2;
cin>>a1>>c1>>a2>>a3>>c2>>a4; f1=simply(a1,a2);
f2=simply(a3,a4); a1=f1.isNegative*(f1.i*f1.denominator+f1.numerator);
a2=f1.denominator; a3=f2.isNegative*(f2.i*f2.denominator+f2.numerator);
a4=f2.denominator; //进行+、-、*、/、运算
f3=op(a1,a2,a3,a4,'+');
printop(f1,f2,f3,'+');
f3=op(a1,a2,a3,a4,'-');
printop(f1,f2,f3,'-');
f3=op(a1,a2,a3,a4,'*');
printop(f1,f2,f3,'*');
f3=op(a1,a2,a3,a4,'/');
if(f2==0)
{
print(f1);
cout<<" / ";
print(f2);
cout<<" = Inf"<<endl;
}
else
printop(f1,f2,f3,'/'); return 0;
}

贴个图

PAT Rational Arithmetic (20)的更多相关文章

  1. pat1088. Rational Arithmetic (20)

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

  2. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

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

  3. PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

    输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...

  4. PAT (Advanced Level) 1088. Rational Arithmetic (20)

    简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...

  5. 【PAT甲级】1088 Rational Arithmetic (20 分)

    题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...

  6. 1088. Rational Arithmetic (20)

    1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...

  7. PAT_A1088#Rational Arithmetic

    Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...

  8. PAT1088:Rational Arithmetic

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

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

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

随机推荐

  1. zabbix4.2学习笔记--zabbix安装

    环境 系统信息 发行版 版本 ip 关系 主机名 centos 7.5 192.168.181.135 服务端 server centos 7.5 192.168.181.136 客户端 client ...

  2. 【原】简单shell练习(三)

    1.软链 linux下的软链接类似于windows下的快捷方式 # ln -s /home/gamestat /gamestat  ln -s a b  中的 a 就是源文件(已经存在的文件),b是链 ...

  3. 模板BSGS(SDOI2011计算器) 模板EXBSGS

    BSGS和EXBSGS是OI中用于解决A^xΞB(mod C)的常用算法. 1.BSGS BSGS用于A,C互质的情况. 令m=sqrt(C),此时x可表示为i*m+j. 式中i和j都<=sqr ...

  4. 在git提交时忽略已提交过或从线上拉取下来但本地已修改的文件

    一.忽略: git update-index --assume-unchanged [file-path] 命令中的file-path 就是需要忽略提价的文件的路径 例子: git update-in ...

  5. 【数据库】DML-增删改查-SQL实现

    一.数据插入-Insert 1. 插入单条记录 insert into 表名(字段名,字段名,字段名) //当插入所有字段时,字段名可以省略 values('值1','值2','值3'); 2. 插入 ...

  6. 真正搞明白Python中Django和Flask框架的区别

    在谈Python中Django框架和Flask框架的区别之前,我们需要先探讨如下几个问题. 一.为什么要使用框架? 为了更好地阐述这个问题,我们把开发一个应用的过程进行类比,往往开发一个应用(web应 ...

  7. 如何设置路由器的MTU

    前几天搞了个ER-X,总觉得没有发挥其最大的能力.今天查了下如何设置MTU,罗列如下,备忘. 1. 目前都是PPPOE,这个不管网络如何复杂,均不要在路由后面计算封包大小.正确的是电脑直接连猫,直接拔 ...

  8. maven运行出现错误:Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]](xjl456852原创)

    maven在使用tomcat插件tomcat7-maven-plugin:2.2:run运行项目,出现下面错误: 严重: A child container failed during start j ...

  9. <<编程之美>> -- 队列中取最大值操作的问题

    不得不说编程之美是一本好书,虽然很多题目在做acm中的过程中遇到过,不过还是有很多值得思考的地方 这是今天在编程之美上看到的一个问题,对于栈转化成队列的一个思考 平时都太过依赖c++内函数库中的栈和队 ...

  10. hdu 4871 树的分治+最短路记录路径

    /* 题意:给你一些节点和一些边,求最短路径树上是k个节点的最长的路径数. 解:1.求出最短路径树--spfa加记录 2.树上进行操作--树的分治,分别处理子树进行补集等运算 */ #include& ...