美文网首页
1088 Rational Arithmetic

1088 Rational Arithmetic

作者: virgilshi | 来源:发表于2018-09-25 21:29 被阅读0次

    1088 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.

    Input Specification:
    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.

    Output Specification:

    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.

    Sample Input 1:

    2/3 -4/2
    

    Sample Output 1:

    2/3 + (-2) = (-1 1/3)
    2/3 - (-2) = 2 2/3
    2/3 * (-2) = (-1 1/3)
    2/3 / (-2) = (-1/3)
    

    Sample Input 2:

    5/3 0/6
    

    Sample Output 2:

    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>
    #include <cmath>
    using namespace std;
    long long gcd(long long a,long long b){
        return b==0 ? a:gcd(b,a%b) ;
    }
    void func(long long m,long long n){
        int flag1=0,flag2=0,flag=0;
        if(n==0){
            printf("Inf");
            return ;
        }
        if(m==0) {
            printf("0");
            return ;
        }
        if(m<0) flag1=1;
        if(n<0) flag2=1;
        m=abs(m),n=abs(n);
        if(flag1==1 && flag2==1) flag=0;
        else if(flag1==1 || flag2==1) flag=1;
        if(m==n){
            if(flag==1) printf("(-1)");
            else printf("1");
            return ;
        }
        long long x=m%n,y=m/n;
        if(x==0){
            if(flag==0) printf("%d",y);
            else printf("(-%d)",y);
            return ;
        }else{
            long long t1=m-y*n,t2=n,t=gcd(t1,t2);
            t1/=t,t2/=t;
            if(flag==1){
                printf("(-");
                if(y==0){
                    printf("%lld/%lld",t1,t2);
                }else{
                    printf("%lld %lld/%lld",y,t1,t2);
                }
                printf(")");
            }else{
                if(y==0){
                    printf("%lld/%lld",t1,t2);
                }else{
                    printf("%lld %lld/%lld",y,t1,t2);
                }
            }
        }
    }
    int main(){
        long long int a,b,c,d;
        scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
    //  printf("%lld %lld %lld %lld",a,b,c,d);
        func(a,b);printf(" + ");func(c,d);printf(" = ");func(a*d+c*b,b*d);printf("\n");
        func(a,b);printf(" - ");func(c,d);printf(" = ");func(a*d-c*b,b*d);printf("\n");
        func(a,b);printf(" * ");func(c,d);printf(" = ");func(a*c,b*d);printf("\n");
        func(a,b);printf(" / ");func(c,d);printf(" = ");func(a*d,b*c);printf("\n");
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:1088 Rational Arithmetic

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