题意:

给出两个分式(a1/b1 a2/b2),分子、分母的范围为int型,且确保分母不为0。计算两个分数的加减乘除,结果化为最简的形式,即"k a/b",其中若除数为0的话,输出Inf。

思路:

分数四则运算的模板题。分析详见:基础数学问题

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>//abs()
typedef long long LL;
struct Fraction{
    LL up;//分子
    LL down;//分母
    Fraction():up(),down(){}//默认初始化
    Fraction(int up_,int down_):up(up_),down(down_){}
};

//求最大公约数
int gcd(LL a,LL b){
    ) return a;
    else return gcd(b,a%b);
}

//分数化简
void simplify(Fraction& a){
    ) {
        a.down = -a.down;
        a.up = -a.up;
    }
    ){
        a.down=;
    }else{
        int commonFractor=gcd(abs(a.up), abs(a.down));//注意要加绝对值!!!
        a.up/=commonFractor;
        a.down/=commonFractor;
    }
}
//打印分数
void printFraction(Fraction a){
    simplify(a);
    ) printf("(");
    ) printf("%lld",a.up);//如果分母为1,则只打印分子即可
    else{
        if(abs(a.up)>abs(a.down))
            printf("%lld %lld/%lld",a.up/a.down,abs(a.up)%a.down,a.down);
        else
            printf("%lld/%lld",a.up,a.down);
    }
    ) printf(")");
}

//分数相加
Fraction add(Fraction a,Fraction b){
    Fraction c;
    c.down=a.down*b.down;
    c.up=a.up*b.down+a.down*b.up;
    simplify(c);
    return c;
}
//分数相减
Fraction sub(Fraction a,Fraction b){
    Fraction c;
    c.down=a.down*b.down;
    c.up=a.up*b.down-a.down*b.up;
    simplify(c);
    return c;
}
//分数相乘
Fraction multiply(Fraction a,Fraction b){
    Fraction c;
    c.down=a.down*b.down;
    c.up=a.up*b.up;
    simplify(c);
    return c;
}
//分数相除
Fraction divide(Fraction a,Fraction b){
    Fraction c;
    c.down=a.down*b.up;
    c.up=a.up*b.down;
    simplify(c);
    return c;
}

void showEquation(Fraction a,Fraction b,const char* op)
{
    bool flag=true;
    Fraction ans;
    printFraction(a);
    ){
        printf(" + ");
        ans=add(a,b);
    }){
        printf(" - ");
        ans=sub(a,b);
    }){
        printf(" * ");
        ans=multiply(a,b);
    }else {
        printf(" / ");
        ) flag=false;
        else ans=divide(a,b);
    }
    printFraction(b);
    printf(" = ");
    if(flag) printFraction(ans);
    else printf("Inf");
    printf("\n");
}

int main()
{
    LL a1,b1,a2,b2;
    scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2);
    Fraction a(a1,b1);
    Fraction b(a2,b2);
    showEquation(a,b,"add");
    showEquation(a,b,"sub");
    showEquation(a,b,"multiply");
    showEquation(a,b,"divide");
    ;
}

1088 Rational Arithmetic的更多相关文章

  1. PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]

    1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...

  2. 1088 Rational Arithmetic(20 分)

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

  3. PAT 1088. Rational Arithmetic

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

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

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

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

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

  6. PAT (Advanced Level) 1088. Rational Arithmetic (20)

    简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...

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

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

  8. 1088. Rational Arithmetic (20)

    1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...

  9. PAT1088:Rational Arithmetic

    1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...

随机推荐

  1. Kinect 2.0 默认姿势的中文意思

    RaiseRightHand/RaiseLeftHand 抬起左右手高于肩膀一秒Psi 举起双手高于肩膀一秒Tpose T姿势Stop 右手放下,左手缓慢贴住身侧(腰以下)或者左右调换Wave 挥手 ...

  2. python 图像库PIL详解

    PIL详细文档 The most important class in the Python Imaging Library is the Image class, defined in the mo ...

  3. N!含有多少个 2/5质因子

    编程之美127页,N!中含有质因数2的个数 = [N/2] + [N/4] + [N/8] + [N/16] + ..... 要理解上式,先看 编程之美126页,N!中含有质因数5的个数Z 举例:N ...

  4. 【Hive】窗口函数

    我们都知道在sql中有一类函数叫做聚合函数,例如sum().avg().max()等等, 这类函数可以将多行数据按照规则聚集为一行,一般来讲聚集后的行数是要少于聚集前的行数的. 但是有时我们想要既显示 ...

  5. git clone 时显示Filename too long的解决办法

    在git bash中,运行下列命令: git config --global core.longpaths true 就可以解决该问题. --global是该参数的使用范围,如果只想对本版本库设置该参 ...

  6. Ubuntu源更新

    Ubuntu12.04的源在 /etc/apt/sources.list  中, 进入 /etc/apt/ 先进行备份 然后用根用户权限打开sources.list. sudo gedit /etc/ ...

  7. 剑指offer--35.数组中只出现一次的数字

    时间限制:1秒 空间限制:32768K 热度指数:198150 本题知识点: 数组 题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. class ...

  8. 【python】命令行解析工具getopt用法

    处理命令行参数的模块 用法: opts, args = getopt.getopt( sys.args[1:],  shortStr,  longList) 输入: shortStr 形式如下: &q ...

  9. 【SQL查询】合并多个数据集_union

    SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...

  10. selenium webdriver入门

    写在前面:最近在研究UI自动化测试的过程中,发现公司里通常用的是AutomanX框架,而这个框架实际上是基于selenium webdriver框架的,所以在编写测试用例时,很多语法都是直接使用sel ...