题目描写叙述

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. vue 常用功能和命令

    1. vue-cli 构建项目 # 全局安装 vue-cli $ npm install --global vue-clif # 创建一个基于 webpack 模板的新项目 $ vue init we ...

  2. jsencrypt加解密 &&cryptico

    npm install --save jsencrypt import {JSEncrypt} from 'jsencrypt'; //导入公钥if ( publicKey.indexOf('---- ...

  3. Spring Data Redis整体介绍 (一)

    为什么使用Spring Data Redis 首先Spring Data Redis 是Spring 框架提供的用于操作Redis的客户端. Spring框架是一个全栈Java程序框架,通过DI.AO ...

  4. 扫黑除恶Team第四次团队作业

    二.博客撰写要求 文章开头给出团队序号,开发的软件名称,仓库地址. 给出完成本次冲刺需要做的事情(Sprint Backlog)及相应说明. 本次冲刺总结. 三.评分规则 注意:本次作业总分61分.发 ...

  5. VIM基础操作方法汇总

    学习自小甲鱼的视频,快速入门vim 目录: 1.光标移动 2.进入插入模式 3.进入普通模式 4.进入命令行模式 5.退出 6.光标跳跃 7.快速跳转行号 8.删除 9.利用数字重复操作 10.撤回 ...

  6. MySQL与MyBatis中的查询记录

    1.时间段查询 MySQL:select * from table where ctime >= CURDATE() and ctime <DATE_SUB(CURDATE(),INTER ...

  7. [Python3网络爬虫开发实战] 2.4-会话和Cookies

    在浏览网站的过程中,我们经常会遇到需要登录的情况,有些页面只有登录之后才可以访问,而且登录之后可以连续访问很多次网站,但是有时候过一段时间就需要重新登录.还有一些网站,在打开浏览器时就自动登录了,而且 ...

  8. 使用js生成条形码以及二维码

    一.用js生成条形码这种业务场景不是很常见的,最近刚好又接到这种需求 Google一下,发现github还真有这方面的轮子,感谢github,省去了我们很多造轮子的过程, 好了言归正传,首先引入jsb ...

  9. 集训第四周(高效算法设计)E题 (区间覆盖问题)

    UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...

  10. pcb中几个层的解释

    阻焊层(Solder Mask):又称为绿油层,是PCB的非布线层,用于制成丝网漏印板,将不需要焊接的地方涂一层阻焊物质,防止焊接PCB时焊锡在高温下的流动性.在阻焊层上预留的焊盘大小,要比实际焊盘大 ...