PAT 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.
输入描写叙述:
- 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)的更多相关文章
- pat1088. Rational Arithmetic (20)
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算
输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...
- PAT (Advanced Level) 1088. Rational Arithmetic (20)
简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...
- 【PAT甲级】1088 Rational Arithmetic (20 分)
题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...
- 1088. Rational Arithmetic (20)
1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- PAT1088:Rational Arithmetic
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
随机推荐
- tab切换组件nz-tab
<nz-card [nzBordered]="true" nzTitle="卡片标题"> <nz-card style="width ...
- 【传智播客】Libevent学习笔记(一):简介和安装
目录 00. 目录 01. libevent简介 02. Libevent的好处 03. Libevent的安装和测试 04. Libevent成功案例 00. 目录 @ 01. libevent简介 ...
- cssrefresh.js-CSS文件自动刷新
一.如何使用cssrefresh.js 使用很简单,类似下面的代码: <head> <link rel="stylesheet" type="text/ ...
- [kuangbin带你飞]专题四 最短路练习
对于最短路,我主要使用的就是dijkstra,Floyd,SPFA这三个算法.先来介绍一下这三个算法. 1. dijkstra算法.它适用于边权为正的情况,它是单源最短路,就是从单个源点出发到所有的结 ...
- 页面jsp向后端发送:HTTP 400错误 - 请求无效(Bad request)
HTTP 400错误 - 请求无效(Bad request) jsp页面有误 在ajax请求后台数据时有时会报 HTTP 400 错误 - 请求无效 (Bad request);出现这个请求无效报错说 ...
- Linux下启动tomcat报java.lang.OutOfMemoryError: PermGen space
一.错误信息 java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke ...
- centos passwo文件被删除
错误提示 该问题一般由/etc/passwd被清空,删除,移动,改名等造成,需要通过救援模式恢复,操作步骤如下 真实环境已经解决,这里使用vmware模拟.光盘启动,选择救援模式: 语言选择,键盘布局 ...
- c++基础_矩阵乘法
#include <iostream> using namespace std; int main(){ int a,b; cin>>a>>b; long c[a] ...
- Django之模板引擎(母版)
Django之模板引擎(母版) 母版:存放所有页面的基本信息,基本样式 子班:继承母版 自定义当前页面私有的样式信息 母版的样式: {% block xxx(名称) %} xxxxxxx(数据) {% ...
- poj 2186 强连通分量
poj 2186 强连通分量 传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33414 Acc ...