Rational Arithmetic (20)

题目描述

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, 

product and quotient.

输入描述:

Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". 

The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in 

front of the numerator. The denominators are guaranteed to be non-zero numbers.


输出描述:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each 

line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is 

the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the 

denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.

输入例子:

5/3 0/6

输出例子:

1 2/3 + 0 = 1 2/3

1 2/3 - 0 = 1 2/3

1 2/3 * 0 = 0

1 2/3 / 0 = Inf

我的代码:

(注:此代码只在牛客网上可以通过)

#include<iostream>
using namespace std;
int gys(int x,int y)
{
    if(y%x==0) return x;
    else return gys(y%x,x);
}
int main()
{
    int i,n,m,x,y,a,b,c,d,flag=1,flag1=1,flag2=1;
	int x1,y1,a1,b1,flag3=1,x2,y2,a2,b2,flag4=1;
	int x3,y3,a3,b3,flag5=1,x4,y4,a4,b4,flag6=1;
    scanf("%d/%d %d/%d",&n,&m,&x,&y);
    if(m==0 || y==0) return 0;
    if(x==0) flag=0;
    a=n/m,b=n%m,c=x/y,d=x%y;
    if(a<0)
    {
        a=-a; flag1=0;
    }
    if(b<0)
    {
        b=-b; flag1=0;
    }
    if(c<0)
    {
        c=-c; flag2=0;
    }
    if(d<0)
    {
        d=-d; flag2=0;
    }
    for(i=0;i<4;i++)
    {
        if(flag1==0) printf("(-");
        if(a!=0) cout<<a;
        if(b!=0)
        {
			int e=gys(b,m);
            if(a!=0) cout<<" "<<b/e<<'/'<<m/e;
            else cout<<b/e<<'/'<<m/e;
        }
        else
        {
            if(a==0) cout<<0;
        }
        if(flag1==0) cout<<')';
        if(i==0) cout<<" "<<'+'<<" ";
        else if(i==1) cout<<" "<<'-'<<" ";
        else if(i==2) cout<<" "<<'*'<<" ";
        else cout<<" "<<'/'<<" ";
        if(flag2==0) printf("(-");
        if(c!=0) cout<<c;
        if(d!=0)
        {
			int f=gys(d,y);
            if(c!=0) cout<<" "<<d/f<<'/'<<y/f;
            else cout<<d/f<<'/'<<y/f;
        }
        else
        {
            if(c==0) cout<<0;
        }
        if(flag2==0) cout<<')';
        cout<<" "<<'='<<" ";
        if(i==0)
        {
            x1=m*y,y1=n*y+m*x,a1=y1/x1,b1=y1%x1;
            if(x1<0)
            {
                flag3=0; x1=-x1;
            }
            if(a1<0)
            {
                flag3=0; a1=-a1;
            }
            if(b1<0)
            {
                flag3=0; b1=-b1;
            }
            if(flag3==0) printf("(-");
            if(a1!=0) cout<<a1;
            if(b1!=0)
            {
				int e1=gys(b1,x1);
                if(a1!=0) cout<<" "<<b1/e1<<'/'<<x1/e1;
                else cout<<b1/e1<<'/'<<x1/e1;
            }
            else
            {
                if(a1==0) cout<<0;
            }
            if(flag3==0) cout<<')';
        }
        else if(i==1)
        {
            x2=m*y,y2=n*y-m*x,a2=y2/x2,b2=y2%x2;
            if(x2<0)
            {
                flag4=0; x2=-x2;
            }
            if(a2<0)
            {
                flag4=0; a2=-a2;
            }
            if(b2<0)
            {
                flag4=0; b2=-b2;
            }
            if(flag4==0) printf("(-");
            if(a2!=0) cout<<a2;
            if(b2!=0)
            {
				int e2=gys(b2,x2);
                if(a2!=0) cout<<" "<<b2/e2<<'/'<<x2/e2;
                else cout<<b2/e2<<'/'<<x2/e2;
            }
            else
            {
                if(a2==0) cout<<0;
            }
            if(flag4==0) cout<<')';
        }
        else if(i==2)
        {
            x3=m*y,y3=n*x,a3=y3/x3,b3=y3%x3;
            if(x3<0)
            {
                x3=-x3; flag5=0;
            }
            if(a3<0)
            {
                a3=-a3; flag5=0;
            }
            if(b3<0)
            {
                b3=-b3; flag5=0;
            }
            if(flag5==0) printf("(-");
            if(a3!=0) cout<<a3;
            if(b3!=0)
            {
				int e3=gys(b3,x3);
                if(a3!=0) cout<<" "<<b3/e3<<'/'<<x3/e3;
                else cout<<b3/e3<<'/'<<x3/e3;
            }
            else
            {
                if(a3==0) cout<<0;
            }
            if(flag5==0) cout<<')';
        }
        else
        {
            if(flag==0) cout<<"Inf";
			else if(a==0 && b==0) cout<<0;
            else
            {
                x4=m*x,y4=n*y,a4=y4/x4,b4=y4%x4;
                if(x4<0)
                {
                    flag6=0; x4=-x4;
                }
                if(a4<0)
                {
                    flag6=0; a4=-a4;
                }
                if(b4<0)
                {
                    flag6=0; b4=-b4;
                }
                if(flag6==0) printf("(-");
                if(a4!=0) cout<<a4;
                if(b4!=0)
                {
					int e4=gys(b4,x4);
                    if(a4!=0) cout<<" "<<b4/e4<<'/'<<x4/e4;
                    else cout<<b4/e4<<'/'<<x4/e4;
                }
                else
                {
                    if(a4==0) cout<<0;
                }
                if(flag6==0) cout<<')';
            }
        }
        puts("");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值