PAT (Advanced Level) Practice A1088 Rational Arithmetic (20 分)(C++)(甲级)(分数四则运算)

版权声明:假装有个原创声明……虽然少许博文不属于完全原创,但也是自己辛辛苦苦总结的,转载请注明出处,感谢! https://blog.csdn.net/m0_37454852/article/details/86593099

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

//B1034 有理数四则运算 英文版
//https://blog.csdn.net/m0_37454852/article/details/86593071
using namespace std;
#include<algorithm>
#include<iostream>
#include<cstdio>

typedef struct Fraction//分数结构体
{
    long long int up, down;//分子分母
}Fraction;
Fraction A, B;

long long int gcd(long long int a, long long int b)//求最大公约数
{
    if(!b) return a;
    return gcd(b, a%b);
}

Fraction reduction(Fraction X)//化简
{
    if(X.down < 0) X.up = -X.up, X.down = -X.down;
    if(!X.up) X.down = 1;
    else
    {
        long long int d = gcd(abs(X.up), abs(X.down));
        X.up /= d, X.down /= d;
    }
    return X;
}

void print_X(Fraction X)//打印某有理数
{
    X = reduction(X);
    if(!X.up) printf("0");
    else if(X.down == 1)
    {
        if(X.up < 0) printf("(%lld)", X.up);
        else printf("%lld", X.up);
    }
    else if(abs(X.up) > X.down)//假分数
    {
        if(X.up < 0)
        {
            printf("(%lld %lld/%lld)", X.up/X.down, abs(X.up%X.down), X.down);
        }
        else
        {
            printf("%lld %lld/%lld", X.up/X.down, X.up%X.down, X.down);
        }
    }
    else
    {
        if(X.up < 0) printf("(%lld/%lld)", X.up, X.down);
        else printf("%lld/%lld", X.up, X.down);
    }
}

void print(Fraction A, char ch, Fraction B)//打印有理数1 运算符 有理数2 = 
{
    print_X(A);
    printf(" %c ", ch);
    print_X(B);
    printf(" = ");
}

void print_result(Fraction R)//打印运算结果和回车
{
    print_X(R);
    printf("\n");
}

Fraction ADD(Fraction A, Fraction B)//加减乘除
{
    Fraction result;
    result.down = A.down*B.down;
    result.up = A.up*B.down + A.down*B.up;
    return result;
}
Fraction MINUS(Fraction A, Fraction B)
{
    Fraction result;
    result.down = A.down*B.down;
    result.up = A.up*B.down - A.down*B.up;
    return result;
}
Fraction MULTIPLY(Fraction A, Fraction B)
{
    Fraction result;
    result.down = A.down*B.down;
    result.up = A.up*B.up;
    return result;
}
Fraction DIVIDE(Fraction A, Fraction B)
{
    Fraction result;
    result.down = A.down*B.up;
    result.up = A.up*B.down;
    return result;
}


int main()
{
    scanf("%lld/%lld %lld/%lld", &A.up, &A.down, &B.up, &B.down);
    print(A, '+', B);
    print_result(ADD(A, B));
    print(A, '-', B);
    print_result(MINUS(A, B));
    print(A, '*', B);
    print_result(MULTIPLY(A, B));
    print(A, '/', B);
    if(!reduction(B).up) printf("Inf\n");//除数为0单独考虑
    else print_result(DIVIDE(A, B));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/86593099
今日推荐