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 ...
随机推荐
- Windows:32位程序运行在64位系统上注册表会重定向
参考资料 微软注册表英文文档 StackOverflow社区回答 1.注册表位置 64bit系统(Windows Server 2008 R2只有64bit系统)的注册表分32 位注册表项和64位注册 ...
- 单页vue路由router
Vue.js + vue-router 可以很简单的实现单页应用. <router-link> 是一个组件,该组件用于设置一个导航链接,切换不同 HTML 内容. to 属性为目标地址, ...
- 20面向对象三特征 之继承 方法重写 super
继承是:多个类有重复内容,把重复内容放到一个新类中,就可以通过extends关键词去让原来的类和新类产生继承关系,子类只能拿到父类一部分信息.通过extends关键词去指明类与类之间的关系,一个父类可 ...
- 用字符串对列表赋值,一个字符串对应一个列表元素,eg: my @escaped = "asteriskasterisk hash access unpack_func";
my @escaped = "asteriskasterisk hash access unpack_func"; # 是一个元素的赋值 25 unless( $escap ...
- pip install MySQL-python error "can't open config-win.h"
http://blog.csdn.net/xxm524/article/details/48754139
- linux下设置python3.x为默认版本
rm /usr/bin/python ln -s /usr/local/bin/python3.x /usr/bin/python sybomlic 安装目录 系统目录
- MFC模拟鼠标点击
MFC 工程 把以下代码放到你想要响应的函数里面就行 CPoint pt; GetCursorPos(&pt);//获取鼠标在屏幕的当前位置 SetCursorPos(100,200);//移 ...
- [Python3网络爬虫开发实战] 1.2.5-PhantomJS的安装
PhantomJS是一个无界面的.可脚本编程的WebKit浏览器引擎,它原生支持多种Web标准:DOM操作.CSS选择器.JSON.Canvas以及SVG. Selenium支持PhantomJS,这 ...
- win10下硬盘安装CentOS7
安装环境: 1.系统:Windows 10 2.硬盘:SSD(已装好Win 10) + HHD(用来装CentOS 7) 准备工作: 1.DiskGenius(分区工具):用来给硬盘做分区: 2.系统 ...
- 【Kafka问题解决】Connection to xxx could not be established. Broker may not be available.
请检查Kafka的config/server.properties 看看是否有填写 listeners=PLAINTEXT://kafka-host:9092 advertised.listeners ...