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 ...
随机推荐
- CGContextAddArc
这个函数让我在纸上画了半天才搞明白,把我的理解给大家分享下. void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat ra ...
- [Luogu] P1131 [ZJOI2007]时态同步
题目描述 题目描述 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3…进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何 ...
- 当执行计划中出现BITMAP CONVERSION TO ROWIDS关键字时,需要注意了。
前言 前些天优化了一些耗费buffers较多的SQL,但系统CPU降低的效果不明显,于是又拉了awr报告,查看了SQL ordered by Gets排名前列的SQL. 分析 SQL代码: selec ...
- canvas学习--准备
一)canvas标签 属性: 1.width 和 height 控制canvas宽高: 2.style添加基本样式 3.class,id属性 4.标签内添加一行文本,主要用于浏览器不支持canvas标 ...
- Android Studio + Genymotion模拟器安装与配置
一.Android studio 下载与安装 https://developer.android.google.cn/studio/index.html 进入谷歌官方链接下载Android studi ...
- Mvc系统学习9——Areas学习
在Mvc2.0中,新增加了一个特性就是Areas.在没有有使用Areas的情况下,我们的Mvc项目组织是下面这样的.当项目庞大的时候,Controllers,Model,View文件下下面势必会有很多 ...
- 【区间DP+好题】String painter
https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/G [题意] 给定两个长度相同的字符串A,B.每次操作都能把A中的任意一个子段变成 ...
- codeforces365A
#include<stdio.h> #include<string.h>//刚做codeforces上的比赛题我都没看懂啊啊啊啊啊啊 int main() { int n,m, ...
- Mayor's posters POJ - 2528
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- Path Sum(参考别人,二叉树DFS)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...