PAT (B) rational number four operations (20)

Title Link: https://www.nowcoder.com/pat/6/problem/4060

Title Description

This question requires the compilation of a program to calculate the sum, difference, product and quotient of two rational numbers.

Enter a description:

Enter two rational numbers in the form of "a1/b1 a2/b2" in one line, in which the numerator and denominator are all integers within the range of integer, and the negative sign may only appear in front of the numerator

The mother is not 0.

Output Description:

Output the sum, difference, product and quotient of the two rational numbers in the order of "rational number 1 operator rational number 2 = result" in four lines. Note that each rational number of the output must be of the rational number

The simplest form "k a/b", where k is the integer part and a/b is the simplest fraction part; if it is a negative number, it must be bracketed; if the denominator of division is 0, it will output "Inf". Topic guarantee correct output

There are no integers beyond the range of integers.

Input example:

5/3 0/6

Output example:

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;
typedef long long ll;
int gcd(ll a,ll b){
	if(a%b==0){
		return b;
	}
	return gcd(b,a%b);
}
void output(ll a,ll b){
	//The integer part before the fraction is initialized to 0 
	ll c=0;
	//Positive number 
	if(a>0){
		//Such as 3/1 
		if(b==1){
			cout<<a; 
		}
		//Such as 5/3
		else if(a>b){
			c=a/b;
			a-=b*c;
			cout<<c<<" "<<a<<"/"<<b;
			
		} 
		//Such as 3/5
		else{
			cout<<a<<"/"<<b;
		} 
	}
	//a==0 such as 0/3
	else if(a==0){
		cout<<"0";
	} 
	//a<0
	else{
		//Such as -3/1 
		if(b==1){
			cout<<"("<<a<<")"; 
		}
		//Such as -5/3
		else if(abs(a)>b){
			c=a/b;
			a=-1*a%b;
			cout<<"("<<c<<" "<<a<<"/"<<b<<")";
		}
		//Such as -3/5
		else{
			cout<<"("<<a<<"/"<<b<<")";
		}
	}
}
void add(ll a1,ll b1,ll a2,ll b2){
	output(a1,b1);
	cout<<" + ";
	output(a2,b2);
	cout<<" = ";
	ll a3=a1*b2+a2*b1;
    ll b3=b1*b2;
    //Reduced to the simplest form, without fractional form
    ll gcd3=abs(gcd(a3, b3));
    a3/=gcd3;
    b3/=gcd3;
    output(a3,b3);
    cout<<endl; 
}

void sub(ll a1, ll b1, ll a2, ll b2){
    output(a1,b1);
    cout<<" - ";
    output(a2,b2);
    cout<<" = ";
    ll a3=a1*b2-a2*b1;
    ll b3=b1*b2;
    //Reduced to the simplest form, without fractional form
    ll gcd3=abs(gcd(a3, b3));
    a3/=gcd3;
    b3/=gcd3;
    output(a3,b3);
    cout<<endl; 
}

void mul(ll a1, ll b1, ll a2, ll b2){
    output(a1, b1);
    cout<<" * ";
    output(a2, b2);
    cout<<" = ";
    ll a3=a1*a2;
    ll b3=b1*b2;
    //Reduced to the simplest form, without fractional form
    ll gcd3=abs(gcd(a3, b3));
    a3/=gcd3;
    b3/=gcd3;
    output(a3,b3);
    cout<<endl; 
}

void div(ll a1, ll b1, ll a2, ll b2){
    output(a1,b1);
    cout<<" / ";
    output(a2,b2);
    cout<<" = ";
    if(a2==0){
        cout<<"Inf";
    }
    else if(a2 < 0){
        ll a3=-1*a1*b2;
        ll b3=-1*b1*a2;
        //Reduced to the simplest form, without fractional form
        ll gcd3=abs(gcd(a3, b3));
        a3/=gcd3;
        b3/=gcd3;
        output(a3,b3);
    }
    else{
        ll a3=a1*b2;
        ll b3=b1*a2;
        //Reduced to the simplest form, without fractional form
        ll gcd3=abs(gcd(a3, b3));
        a3/=gcd3;
        b3/=gcd3;
        output(a3,b3);
    }
    cout<<endl; 
}

int main(){
	ll a1,b1,a2,b2;
	ll c1=0;
	ll c2=0;
	scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2);
	//Reduce to the simplest form first 
	ll gcd1=abs(gcd(a1,b1));
	a1/=gcd1;
	b1/=gcd1;
	ll gcd2=abs(gcd(a2,b2));
	a2/=gcd2;
	b2/=gcd2;
	
	//Unified simplest form operation
	add(a1,b1,a2,b2);
	sub(a1,b1,a2,b2);
	mul(a1,b1,a2,b2); 
	div(a1,b1,a2,b2);
	return 0;
}

 

Posted by brotherhewd on Thu, 02 Jan 2020 02:13:26 -0800