码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-03 21:47:34      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

简单题。

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

#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==0) return a;
    return gcd(b,a%b);
}

FS change(FS res)
{
    if(res.fz!=0)
    {
        long long GCD=gcd(abs(res.fz),abs(res.fm));
        res.fz=res.fz/GCD;
        res.fm=res.fm/GCD;
    }

    else
    {
        res.fz=0;
        res.fm=1;
    }

    return res;
}

FS ADD(FS a,FS b)
{
    FS res(0,1);

    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(0,1);

    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(0,1);

    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(0,1);

    if(b.fz==0)
    {
        res.fz=0;
        res.fm=0;
        return res;
    }

    if(a.fz==0) return res;

    res.fz=a.fz*b.fm;
    res.fm=a.fm*b.fz;

    if(res.fm<0)
    {
        res.fm=-res.fm;
        res.fz=-res.fz;
    }

    res=change(res);
    return res;
}

void output(FS a)
{
    if(a.fm==0)
    {
        printf("Inf");
        return;
    }

    if(a.fz==0)
    {
        printf("0");
        return;
    }

    a=change(a);
    if(abs(a.fz)<a.fm)
    {
        if(a.fz<0) printf("(");

        printf("%lld/%lld",a.fz,a.fm);
        if(a.fz<0) printf(")");
        return;
    }

    if(a.fz>0)
    {
        if(a.fz%a.fm==0)
        {
            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==0)
        {
            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 0;
}

 

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

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/5638731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!