美文网首页
A1088 Rational Arithmetic (20分)

A1088 Rational Arithmetic (20分)

作者: km15 | 来源:发表于2020-02-09 17:57 被阅读0次

    /*
    题意:
    1、给出两个分式,计算和差积商(保证所有给出的分母不存在0

    2、输出的格式:
    (1)必须是最贱
    (2)如果是负数,必须包括在一对圆括号
    (3)在除法中,如果除数是0,必须打印INF

    解题:
    1、分数
    2、分数化简
    3、加、减、乘、除(除需要改变)
    4、输出不需要改变

    learn && wrong:
    强调一点,其实dev跟vs完全一样的,只是有没有高亮字体跟自动显示参与运算的变量
    1、涉及负数了,记得公约数弄成绝对值
    2、除法不变,但是输出,加了给If_else
    3、注意公约数是绝对值啊!不然第一个数的乘法错了!
    4、加法总是错,错符号,错down跟up上题错加号
    5、加了两个if实现括号 ,注意一定要化简,负数加括号的实现
    6、原来如此,化为1,就是为了统一这种情况,分母为1,还有分子为0的情况 && 分母为1,但是分子不为1的情况
    */

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    struct fraction {
        long long up;
        long long down;
    } temp1, temp2;
    
    //求最大公约数
    long long gcd(long long a, long long b) {
        if (b == 0) return a;
        else return gcd(b, a % b);
    }
    
    //化简分数
    fraction reduction(fraction result) {
        if (result.down < 0) {
            result.up = -result.up;
            result.down = -result.down;
        }
    
        if (result.up == 0) result.down = 1;
        else {
            long long d = gcd(abs(result.up), abs(result.down));
            result.up = result.up / d;
            result.down = result.down / d;
        }
        return result;
    }
    
    //加法实现
    fraction add(fraction f1, fraction f2) {
        fraction result;
        result.up = f1.up * f2.down + f1.down * f2.up;//(!!!)加法总是错,错符号,错down跟up
        result.down = f1.down * f2.down;
        return reduction(result);
    }
    
    //减法实现
    fraction sub(fraction f1, fraction f2) {
        fraction result;
        result.up = f1.up * f2.down - f1.down * f2.up;
        result.down = f1.down * f2.down;
        return reduction(result);
    }
    
    //乘法实现
    fraction plus1(fraction f1, fraction f2) {
        fraction result;
        result.up = f1.up * f2.up;
        result.down = f1.down * f2.down;
        return reduction(result);
    }
    
    //除法实现(不知道如何实现除法) 
    fraction divide(fraction f1, fraction f2) {
        fraction result;
        result.up = f1.up * f2.down;
        result.down = f1.down * f2.up;
        return reduction(result);
        
    }
    
    
    //输出实现,!!!不用直接另外一个变量,直接原来传进来那个 !!!加了两个if实现括号 ,注意一定要化简 
    void show_fra(fraction result) {
        result = reduction(result);
    
        if (result.up < 0) {
            cout << "(";
        } 
    
        if (result.down == 1) cout << result.up; //!!!原来如此,化为1,就是为了统一这种情况,分母为1,还有分子为0的情况 
        else if (abs(result.up) > result.down) {
            printf("%lld %lld/%lld", result.up / result.down, abs(result.up) % result.down, result.down);
        }
        else {
            printf("%lld/%lld", result.up, result.down);
        }
    
        if (result.up < 0) {
            cout << ")";
        }
    }
    
    int main(int argc, char** argv) {
        scanf("%lld/%lld", &temp1.up, &temp1.down);
        scanf("%lld/%lld", &temp2.up, &temp2.down);
    
        //加法输出 
        show_fra(temp1);
        printf(" + ");
        show_fra(temp2);
        printf(" = ");
        show_fra(add(temp1, temp2));
        cout << endl;
    
        //减法实现
        show_fra(temp1);
        printf(" - ");
        show_fra(temp2);
        printf(" = ");
        show_fra(sub(temp1, temp2));
        cout << endl;
    
        //乘法实现
        show_fra(temp1);
        printf(" * ");
        show_fra(temp2);
        printf(" = ");
        show_fra(plus1(temp1, temp2));
        cout << endl;
    
        //除法实现 
        
        show_fra(temp1);
        printf(" / ");
        show_fra(temp2);
        printf(" = ");
        if (temp2.up == 0) printf("Inf");
        else show_fra(divide(temp1, temp2));
        cout << endl;
        
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:A1088 Rational Arithmetic (20分)

          本文链接:https://www.haomeiwen.com/subject/caqnxhtx.html