简单题。

注意:读入的分数可能不是最简的。输出时也需要转换成最简。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std; struct FS
{
long long fz,fm;
FS(long long a,long long b)
{
fz=a;
fm=b;
}
}; long long gcd(long long a,long long b)
{
if(b==) return a;
return gcd(b,a%b);
} FS change(FS res)
{
if(res.fz!=)
{
long long GCD=gcd(abs(res.fz),abs(res.fm));
res.fz=res.fz/GCD;
res.fm=res.fm/GCD;
} else
{
res.fz=;
res.fm=;
} return res;
} FS ADD(FS a,FS b)
{
FS res(,); res.fz=a.fz*b.fm+b.fz*a.fm;
res.fm=a.fm*b.fm; res=change(res);
return res;
} FS SUB(FS a,FS b)
{
FS res(,); res.fz=a.fz*b.fm-b.fz*a.fm;
res.fm=a.fm*b.fm; res=change(res);
return res;
} FS MUL(FS a,FS b)
{
FS res(,); res.fz=a.fz*b.fz;
res.fm=a.fm*b.fm; res=change(res);
return res;
} FS DIV(FS a,FS b)
{
FS res(,); if(b.fz==)
{
res.fz=;
res.fm=;
return res;
} if(a.fz==) return res; res.fz=a.fz*b.fm;
res.fm=a.fm*b.fz; if(res.fm<)
{
res.fm=-res.fm;
res.fz=-res.fz;
} res=change(res);
return res;
} void output(FS a)
{
if(a.fm==)
{
printf("Inf");
return;
} if(a.fz==)
{
printf("");
return;
} a=change(a);
if(abs(a.fz)<a.fm)
{
if(a.fz<) printf("("); printf("%lld/%lld",a.fz,a.fm);
if(a.fz<) printf(")");
return;
} if(a.fz>)
{
if(a.fz%a.fm==)
{
printf("%lld",a.fz/a.fm);
return;
}
else
{
printf("%lld %lld/%lld",a.fz/a.fm,a.fz%a.fm,a.fm);
return;
}
} else
{
a.fz=-a.fz; printf("(");
if(a.fz%a.fm==)
{
printf("-%lld",a.fz/a.fm);
printf(")");
return;
}
else
{
printf("-%lld %lld/%lld",a.fz/a.fm,a.fz%a.fm,a.fm);
printf(")");
return;
} }
} int main()
{
long long s1,s2,s3,s4;
scanf("%lld/%lld %lld/%lld",&s1,&s2,&s3,&s4); FS a(s1,s2);
FS b(s3,s4); output(a); cout<<" + "; output(b); cout<<" = "; output(ADD(a,b));
cout<<endl;
output(a); cout<<" - "; output(b); cout<<" = "; output(SUB(a,b));
cout<<endl;
output(a); cout<<" * "; output(b); cout<<" = "; output(MUL(a,b));
cout<<endl;
output(a); cout<<" / "; output(b); cout<<" = "; output(DIV(a,b));
cout<<endl; return ;
}

PAT (Advanced Level) 1088. Rational Arithmetic (20)的更多相关文章

  1. PAT (Advanced Level) 1081. Rational Sum (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  2. 【PAT甲级】1088 Rational Arithmetic (20 分)

    题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...

  3. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  4. PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642 题目描述: The highest building in our city has ...

  5. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...

  6. PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

    输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...

  7. PAT (Advanced Level) Practice 1035 Password (20 分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  8. 【PAT Advanced Level】1008. Elevator (20)

    没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...

  9. PAT (Advanced Level) 1112. Stucked Keyboard (20)

    找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include ...

随机推荐

  1. Encryption-基础:MD5加密

    环境:vc2003 .h /* MD5.H - header file for MD5C.C */ /* Copyright (C) 1991-2, RSA Data Security, Inc. C ...

  2. c++ 读取一行的2个数

    #include <iostream> using namespace std; double harmonicMean(double x, double y); int main() { ...

  3. GIMP做成颜色蒙板

    效果图: 原始的美女图片上盖了一层的颜色,这个是想出来的效果,只是用来实践学到的技能,具体的场景还没有确定. 1/首先打开原始的美女图片: 2/然后在添加一张新的图片,作为新的图层添加进来: 这样的话 ...

  4. Python Third Day-函数

    ''' 为什么要有函数?没有函数带来的困扰? 组织结构不清晰,可读性差 代码冗余 可扩展性差 什么是函数 具备某一个功能的工具--->函数 事先准备工具->函数的定义 拿来就用.重复使用- ...

  5. Flask扩展实现HTTP令牌token认证HTTPTokenAuth

    Token认证 在restful设计中,用户认证模式通常使用json web token,而不会使用传统的HTTP Basic认证(传入账号密码) token认证模式如下:在请求header中加入to ...

  6. Python9-继承2-day25(大年初二)

    继承:什么是什么关系组合:什么有什么关系单继承 先抽象再继承,几个类直接的相同代码抽象出来,成为父类 子类自己没有的名字可以使用父类的方法和属性 如果子类自己有,一定先用自己的 在类中使用self的时 ...

  7. ORACLE 查询所有用户调度作业

    --查询所有用户调度作业:select * from ALL_SCHEDULER_JOBS; --查询当前用户调度作业:select * from USER_SCHEDULER_JOBS; --查询当 ...

  8. vue 的 scroller 使用

    一 安装 使用npm 安装npm install vue-scroller -d 二 引入 import VueScroller from "vue-scroller" Vue.u ...

  9. 【LeetCode】ZigZag Conversion(Z 字形变换)

    这道题是LeetCode里的第6道题. 题目要求: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...

  10. zoj 2830 Champion of the Swordsmanship

    Champion of the Swordsmanship Time Limit: 2 Seconds      Memory Limit: 65536 KB In Zhejiang Universi ...