Pat(A) 1088. Rational Arithmetic (20)

原题目:

原题链接:https://www.patest.cn/contests/pat-a-practise/1088

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

题目大意

给出两个有理数,分别计算其“ + ”,“ - ”,“ * ”,“ / ”的值。

解题报告

利用: $\frac ab + \frac cd = \frac {ad+bc}{bd}$ $\frac ab - \frac cd = \frac {ad-bc}{bd}$ $\frac ab * \frac cd = \frac {ac}{bd}$ $\frac ab / \frac cd = \frac {ad}{bc}$ 需要注意的就是符号问题还有0 的问题。

代码

#include "iostream"
#include "string"
using namespace std;

long long a,b,c,d;

long long int gcd(long long int m, long long int n) {
    return n == 0 ? m : gcd(n, m % n);
}

void num2string(long long m,long long n){
    bool sign = true;
    if(n == 0){
        cout<<"Inf";
        return;
    }
    if(m == 0){
        cout<<"0";
        return;
    }
    if(m<0){
        sign = !sign;
        m = -m;
    }
    if(n < 0){
        sign = !sign;
        n = -n;
    }
    long long g = gcd(m,n);
    m = m/g;
    n = n/g;
    if(!sign)
        cout<<"(-";
    long long x = m/n,y=m%n;
    if(x == 0)
        cout<<y<<"/"<<n;
    else if(y == 0)
        cout<<x;
    else
        cout<<x<<" "<<y<<"/"<<n;

    if(!sign)
        cout<<")";
}

void add(){
    long long e = a * d + b * c,f = b * d;
    num2string(a,b);
    cout<<" + ";
    num2string(c,d);
    cout<<" = ";
    num2string(e,f);
    cout<<endl;
}

void min(){
    long long e = a * d - b * c,f = b * d;
    num2string(a,b);
    cout<<" - ";
    num2string(c,d);
    cout<<" = ";
    num2string(e,f);
    cout<<endl;
}

void mul(){
    long long e =  a * c,f = b * d;
    num2string(a,b);
    cout<<" * ";
    num2string(c,d);
    cout<<" = ";
    num2string(e,f);
    cout<<endl;
}

void div(){
    long long e = a * d,f = b * c;
    num2string(a,b);
    cout<<" / ";
    num2string(c,d);
    cout<<" = ";
    num2string(e,f);
    cout<<endl;
}
int main(){
    char ch;
    cin>>a;
    scanf("%c",&ch);
    cin>>b>>c;
    scanf("%c",&ch);
    cin>>d;
    add();
    min();
    mul();
    div();
    system("pause");
}

发表评论

    

最新评论

    还没有人评论...

Powered by Django 2. Copyright © 2014.

huchengbei.com. All rights reserved.