• pat每日刷题计划--day75


    分数运算

    reduction:约分

    add:加法

    minu:减法

    multi:乘法

    divide:除法

    showresult:输出结果

    需要注意的部分:

    对于化简:

      1、先修改正负号,保证up用来存正负数,down是永远正数的

      2、如果0,规定up是0,down1

      3、之后求最大公因数然后约分即可。

    对于输出结果:

      1、如果是整数,需要判断输出。不输出分母,里面包括了0的。

      2、如果是小于0的,需要先输出一个负号,up变成abs up

      3、如果是大于1,带分数先输出整数部分,修改up成小于1

      4、后面正常输出。

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<stdlib.h>
    using namespace std;
    struct fraction
    {
        int up,down;
    };
    int gcd(int a,int b)
    {
        if(a<b)
        {
            int temp=b;
            b=a;
            a=temp;
        }
        if(b==0) return a;
        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
        {
            int temp=gcd(abs(result.up),result.down);
            result.up=result.up/temp;
            result.down=result.down/temp;
        }
        return result;
    }
    fraction add(fraction f1,fraction f2)
    {
        fraction temp;
        temp.up=f1.up*f2.down+f2.up*f1.down;
        temp.down=f1.down*f2.down;
        temp=reduction(temp);
        return temp;
    }
    fraction minu(fraction f1,fraction f2)
    {
        fraction temp;
        temp.up=f1.up*f2.down-f2.up*f1.down;
        temp.down=f1.down*f2.down;
        temp=reduction(temp);
        return temp;
    }
    fraction multi(fraction f1,fraction f2)
    {
        fraction temp;
        temp.up=f1.up*f2.up;
        temp.down=f1.down*f2.down;
        temp=reduction(temp);
        return temp;
    }
    fraction divide(fraction f1,fraction f2)
    {
        fraction temp;
        temp.up=f1.up*f2.down;
        temp.down=f1.down*f2.up;
        temp=reduction(temp);
        return temp;
    }
    void showresult(fraction f1)
    {
        f1=reduction(f1);
        if(f1.down==1)
            printf("%d
    ",f1.up);
        else
        {
            if(f1.up<0)
            {
                printf("-");
                f1.up=abs(f1.up);
            }
            if(f1.up/f1.down)
            {
                int f=abs(f1.up)/f1.down;
                f1.up=abs(f1.up)%f1.down;
                printf("%d ",f);
                //printf("%d %d/%d
    ",abs(f1.up)/f1.down,abs(f1.up)%f1.down,f1.down);
            }
            printf("%d/%d
    ",f1.up,f1.down);
        }
    }
    int main()
    {
        fraction a,b;
        a.up=3;
        a.down=4;
        b.up=10;
        b.down=-12;
        showresult(reduction(a));
        showresult(reduction(b));
        showresult(add(a,b));
        showresult(minu(a,b));
        showresult(multi(a,b));
        showresult(divide(a,b));
        return 0;
    }
    View Code
    时间才能证明一切,选好了就尽力去做吧!
  • 相关阅读:
    LayoutInflater作用及使用--自定义EditText,自带清除内容按钮
    SSL backend error when using OpenSSL pycurl install error
    pypyodbc 的坑
    mongo aggregate 删除重复数据
    如何验证代理ip的正确性
    python的非官方的一个下载lib的地方
    rabbitmq web 管理系统的信息
    内存泄漏分析
    readme 语法
    logging dictconfig
  • 原文地址:https://www.cnblogs.com/tingxilin/p/12257117.html
Copyright © 2020-2023  润新知