【PAT】1088. Rational Arithmetic (20)
2015-10-13 21:12
232 查看
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.
Input Specification:
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.
Output Specification:
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.
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
涉及到的知识点
(1)求最大公约数;
(2)分数的四则运算;
(3)分数形式的输出;
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
struct node{
node(){
flag = 1;
zh=0;
fm=1;
fz=0;
}
node(int f,int _zh, int _fz, int _fm){
flag = f;
zh = _zh;
fz = _fz;
fm = _fm;
}
int flag;
long fz, fm, zh;
};
long gcd(long a, long b){
long n = max(a,b);
long m = min(a,b);
long r;
while(m!=0){
r = n%m;
n = m;
m = r;
}
return n;
}
void initial(node &a){
if(a.fz<0){
a.flag = -1;
a.fz = abs(a.fz);
}
long gys = gcd(a.fz, a.fm);
a.fz /= gys;
a.fm /= gys;
if(a.fz >= a.fm){
a.zh = a.fz/a.fm;
a.fz %= a.fm;
}
}
node calSum(node a, node b){
long fz = a.flag*(a.zh*a.fm+a.fz)*b.fm + b.flag*(b.zh*b.fm+b.fz)*a.fm;
long fm = a.fm*b.fm;
long gys = gcd(abs(fz),fm);
fz /= gys;
fm /= gys;
node n;
n.fz = fz;
n.fm = fm;
initial(n);
return n;
}
node calDif(node a, node b){
long fz = a.flag*(a.zh*a.fm+a.fz)*b.fm - b.flag*(b.zh*b.fm+b.fz)*a.fm;
long fm = a.fm*b.fm;
long gys = gcd(abs(fz),fm);
fz /= gys;
fm /= gys;
node n;
n.fz = fz;
n.fm = fm;
initial(n);
return n;
}
node calPro(node a, node b){
long fz = a.flag*b.flag*(a.zh*a.fm+a.fz) * (b.zh*b.fm+b.fz);
long fm = a.fm*b.fm;
long gys = gcd(abs(fz),fm);
fz /= gys;
fm /= gys;
node n;
n.fz = fz;
n.fm = fm;
initial(n);
return n;
}
node calQuo(node a, node b){
if(b.zh==0 && b.fz==0){
return node(1,0,0,1);
}
long fz = (a.zh*a.fm+a.fz) * b.fm;
long fm = (b.zh*b.fm+b.fz) * a.fm;
fz *= a.flag*b.flag;
long gys = gcd(abs(fz),fm);
fz /= gys;
fm /= gys;
node n;
n.fz = fz;
n.fm = fm;
initial(n);
return n;
}
void showPositive(node a){
if(a.zh!=0){
printf("%d",a.zh);
if(a.fz!=0){
printf(" %ld/%ld",a.fz, a.fm);
}
}else{
if(a.fz!=0){
printf("%ld/%ld",a.fz, a.fm);
}else{
printf("0");
}
}
}
void showNegative(node a){
printf("(-");
showPositive(a);
printf(")");
}
void show(node a){
if(a.flag==-1){
showNegative(a);
}else{
showPositive(a);
}
}
int main(int argc, char** argv) {
long fza, fma, fzb, fmb;
node a, b;
scanf("%ld/%ld %ld/%ld",&a.fz,&a.fm,&b.fz,&b.fm);
initial(a);
initial(b);
//加法
node sum = calSum(a,b);
show(a); cout<<" + "; show(b); cout<<" = "; show(sum); cout<<endl;
//减法
node dif = calDif(a,b);
show(a); cout<<" - "; show(b); cout<<" = "; show(dif); cout<<endl;
//乘法
show(a); cout<<" * "; show(b); cout<<" = ";
if( (a.zh==0&&a.fz==0)||(b.zh==0&&b.fz==0)){
printf("0\n");
}else{
node pro = calPro(a,b);
show(pro); cout<<endl;
}
//除法
show(a); cout<<" / "; show(b); cout<<" = ";
if(b.zh==0 && b.fz==0){
printf("Inf\n");
return 0;
}
node quo = calQuo(a,b);
show(quo); cout<<endl;
return 0;
}
Input Specification:
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.
Output Specification:
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.
Sample Input 1:
2/3 -4/2
Sample Output 1:
2/3 + (-2) = (-1 1/3) 2/3 - (-2) = 2 2/3 2/3 * (-2) = (-1 1/3) 2/3 / (-2) = (-1/3)
Sample Input 2:
5/3 0/6
Sample Output 2:
1 2/3 + 0 = 1 2/3 1 2/3 - 0 = 1 2/3 1 2/3 * 0 = 01 2/3 / 0 = Inf
涉及到的知识点
(1)求最大公约数;
(2)分数的四则运算;
(3)分数形式的输出;
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
struct node{
node(){
flag = 1;
zh=0;
fm=1;
fz=0;
}
node(int f,int _zh, int _fz, int _fm){
flag = f;
zh = _zh;
fz = _fz;
fm = _fm;
}
int flag;
long fz, fm, zh;
};
long gcd(long a, long b){
long n = max(a,b);
long m = min(a,b);
long r;
while(m!=0){
r = n%m;
n = m;
m = r;
}
return n;
}
void initial(node &a){
if(a.fz<0){
a.flag = -1;
a.fz = abs(a.fz);
}
long gys = gcd(a.fz, a.fm);
a.fz /= gys;
a.fm /= gys;
if(a.fz >= a.fm){
a.zh = a.fz/a.fm;
a.fz %= a.fm;
}
}
node calSum(node a, node b){
long fz = a.flag*(a.zh*a.fm+a.fz)*b.fm + b.flag*(b.zh*b.fm+b.fz)*a.fm;
long fm = a.fm*b.fm;
long gys = gcd(abs(fz),fm);
fz /= gys;
fm /= gys;
node n;
n.fz = fz;
n.fm = fm;
initial(n);
return n;
}
node calDif(node a, node b){
long fz = a.flag*(a.zh*a.fm+a.fz)*b.fm - b.flag*(b.zh*b.fm+b.fz)*a.fm;
long fm = a.fm*b.fm;
long gys = gcd(abs(fz),fm);
fz /= gys;
fm /= gys;
node n;
n.fz = fz;
n.fm = fm;
initial(n);
return n;
}
node calPro(node a, node b){
long fz = a.flag*b.flag*(a.zh*a.fm+a.fz) * (b.zh*b.fm+b.fz);
long fm = a.fm*b.fm;
long gys = gcd(abs(fz),fm);
fz /= gys;
fm /= gys;
node n;
n.fz = fz;
n.fm = fm;
initial(n);
return n;
}
node calQuo(node a, node b){
if(b.zh==0 && b.fz==0){
return node(1,0,0,1);
}
long fz = (a.zh*a.fm+a.fz) * b.fm;
long fm = (b.zh*b.fm+b.fz) * a.fm;
fz *= a.flag*b.flag;
long gys = gcd(abs(fz),fm);
fz /= gys;
fm /= gys;
node n;
n.fz = fz;
n.fm = fm;
initial(n);
return n;
}
void showPositive(node a){
if(a.zh!=0){
printf("%d",a.zh);
if(a.fz!=0){
printf(" %ld/%ld",a.fz, a.fm);
}
}else{
if(a.fz!=0){
printf("%ld/%ld",a.fz, a.fm);
}else{
printf("0");
}
}
}
void showNegative(node a){
printf("(-");
showPositive(a);
printf(")");
}
void show(node a){
if(a.flag==-1){
showNegative(a);
}else{
showPositive(a);
}
}
int main(int argc, char** argv) {
long fza, fma, fzb, fmb;
node a, b;
scanf("%ld/%ld %ld/%ld",&a.fz,&a.fm,&b.fz,&b.fm);
initial(a);
initial(b);
//加法
node sum = calSum(a,b);
show(a); cout<<" + "; show(b); cout<<" = "; show(sum); cout<<endl;
//减法
node dif = calDif(a,b);
show(a); cout<<" - "; show(b); cout<<" = "; show(dif); cout<<endl;
//乘法
show(a); cout<<" * "; show(b); cout<<" = ";
if( (a.zh==0&&a.fz==0)||(b.zh==0&&b.fz==0)){
printf("0\n");
}else{
node pro = calPro(a,b);
show(pro); cout<<endl;
}
//除法
show(a); cout<<" / "; show(b); cout<<" = ";
if(b.zh==0 && b.fz==0){
printf("Inf\n");
return 0;
}
node quo = calQuo(a,b);
show(quo); cout<<endl;
return 0;
}
相关文章推荐
- libdvbpsi源码分析(四)PAT表解析/重建
- PAT配置
- 什么是端口复用动态地址转换(PAT) 介绍配置实例
- MikroTik layer7-protocol
- PAT是如何工作的
- PAT 乙级题:1002. 写出这个数 (20)
- PAT (Advanced Level) Practise 1001-1010
- 数据结构学习与实验指导(一)
- PAT Basic Level 1001-1010解题报告
- 1001. 害死人不偿命的(3n+1)猜想
- 1002. 写出这个数
- 1032. 挖掘机技术哪家强
- 1001. 害死人不偿命的(3n+1)猜想 (PAT basic)
- 1002. 写出这个数(PAT Basic)
- 1004. 成绩排名(PAT Basic)
- 1006. 换个格式输出整数(PAT Basic)
- 1007. 素数对猜想(PAT Basic)
- 1008. 数组元素循环右移问题
- 1009. 说反话(PAT Basic)
- 1011. A+B和C(PAT Basic)