1034 有理数四则运算 (20 分)

原题

https://pintia.cn/problem-sets/994805260223102976/problems/994805287624491008

思路

注意点:
1、浮点错误——只要是除就有除0的情况。
2、判断正负

代码

抄的柳大佬的

#include <iostream>
#include <string>
using namespace std;

//判断是不是最简分数
long long gcd(long long a, long long b)
{
    return b == 0 ? a : gcd(b, a % b);
}
//化为分数
void fraction(long long m, long long d)
{
    long long temp, g;    
    if(m*d==0){printf("%s",d==0?"Inf":"0");return;}//若除法分母为 0

    // bool flag=false;
    // if(m<0||d<0){m=abs(m);d=abs(d);flag=true;} 错误——都小于0,为正的
    bool flag = ((m < 0 && d > 0) || (m > 0 && d < 0));
    m = abs(m); d = abs(d);
    if(flag) cout << "(-";
    temp = m / d;
    if (temp!=0) printf("%lld",temp);
    if (m % d == 0)//为整数
    {     
       if(flag) cout << ")";      
        return;
    }
    if (temp!=0) cout << " "; 

    //整数部分后面的分子
    m=m-temp*d;//变为分子小于分母
    g = gcd(m, d);
    m = m / g;d = d / g;//约分  
    printf("%lld/%lld",m,d);   
    if(flag)cout << ")";
}

int main()
{
    long long a, b, c, d;
    scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
    //相加
    fraction(a,b); cout<<" + ";fraction(c,d);cout<<" = ";fraction(a * d + c * b,b * d);cout<<endl;
    //相减
    fraction(a,b); cout<<" - ";fraction(c,d);cout<<" = ";fraction(a * d - c * b,b * d);cout<<endl;
    //相减
    fraction(a,b); cout<<" * ";fraction(c,d);cout<<" = ";fraction(a*c,b * d);cout<<endl;
     //相除
    fraction(a,b); cout<<" / ";fraction(c,d);cout<<" = ";fraction(a*d,b * c);
    return 0;
}

下面的第一次提交,部分对

#include <iostream>
#include <string>
using namespace std;

//判断是不是最简分数
int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}
//化为分数
void fraction(int m, int d)
{
    int temp, g;
    bool flag=false;
    if(m*d==0){printf("%s",d==0?"Inf":"0");return;}//若除法分母为 0
    if(m<0||d<0){m=abs(m);d=abs(d);flag=true;}
    if (m >= d)
    {
        if (m % d == 0)
        {
            temp = m / d;
            d = 0;
        }
        else
        {
            temp = m / d;
            m = m % d;
        }
    }
    if (m < d)
    {
        g = gcd(m, d);
        if (g != 1)
        {
            m = m / g;
            d = d / g;
        }
    }
    //输出
    if(flag){cout << "(-";}
    if(d==0) cout<<temp;
    else{
    if(temp!=0) cout<<temp<<" ";   
    cout<<m<<"/"<<d;
    }
    if(flag)cout << ")";
}

int main()
{
    int a, b, c, d;
    int molecu, denom;
    scanf("%d/%d %d/%d", &a, &b, &c, &d);
    //相加
    molecu = a * d + c * b;
    denom = b * d;
    fraction(a,b); cout<<" + ";fraction(c,d);cout<<" = ";fraction(molecu,denom);cout<<endl;
    //相减
    molecu = a * d - c * b;
    fraction(a,b); cout<<" - ";fraction(c,d);cout<<" = ";fraction(molecu,denom);cout<<endl;
    //相减
    molecu = a*c;
    fraction(a,b); cout<<" * ";fraction(c,d);cout<<" = ";fraction(molecu,denom);cout<<endl;
     //相除
    molecu =a*d;
    denom = b * c;
    fraction(a,b); cout<<" / ";fraction(c,d);cout<<" = ";fraction(molecu,denom);
    return 0;
}
posted @ 2021-12-09 15:22  Infinite_V胜  阅读(18)  评论(0)    收藏  举报