1034 four operations of rational number (20) (20 points)
This question requires the compilation of a program to calculate the sum, difference, product and quotient of two rational numbers.
Input format:
Enter two rational numbers in the form of "a1/b1 a2/b2" in a row, in which the numerator and denominator are all integers within the range of integer, the negative sign may only appear in front of the numerator, and the denominator is not 0.
Output format:
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 output must be the simplest form "k a/b" of the rational number, 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 must output "Inf". Ensure that the correct output does not exceed the integer range.
Enter example 1:
2/3 -4/2
Output example 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
Enter example 2:
5/3 0/6
Output example 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;
int gcd(long long a, long long b)
{
return b == 0 ? a : gcd(b, a % b);
}
void print(long long a, long long b){
long long c = 0; //Integer part before fraction, default is 0
if(a > 0){ //Positive number
if(b == 1){ //Shaped like 3/1
printf("%lld", a);
}
else if(a > b){ //Shaped like 5/3
c = a / b;
a -= b * c;
printf("%lld %lld/%lld", c, a, b);
}
else{ //True fraction is like 3 / 5
printf("%lld/%lld", a, b);
}
}
else if(a == 0){ //Shaped like 0/3
printf("%c", '0');
}
else{ //negative
if(b == 1){ //Shaped like -3/1
printf("(%lld)", a);
}
else if(-1 * a > b){ //Shaped like -5/3
c = a / b;
a = (-1 * a) % b;
printf("(%lld %lld/%lld)", c, a, b);
}
else{ //True fraction
printf("(%lld/%lld)", a, b);
}
}
}
void add(long long a1, long long b1, long long a2, long long b2){
print(a1, b1);
printf(" + ");
print(a2, b2);
printf(" = ");
long long a3 = a1 * b2 + a2 * b1;
long long b3 = b1 * b2;
//Reduced to the simplest form, without fractional form
long long gcd3 = abs(gcd(a3, b3));
a3 /= gcd3;
b3 /= gcd3;
print(a3, b3);
printf("\n");
}
void subtract(long long a1, long long b1, long long a2, long long b2){
print(a1, b1);
printf(" - ");
print(a2, b2);
printf(" = ");
long long a3 = a1 * b2 - a2 * b1;
long long b3 = b1 * b2;
//Reduced to the simplest form, without fractional form
long long gcd3 = abs(gcd(a3, b3));
a3 /= gcd3;
b3 /= gcd3;
print(a3, b3);
printf("\n");
}
void multiply(long long a1, long long b1, long long a2, long long b2){
print(a1, b1);
printf(" * ");
print(a2, b2);
printf(" = ");
long long a3 = a1 * a2;
long long b3 = b1 * b2;
//Reduced to the simplest form, without fractional form
long long gcd3 = abs(gcd(a3, b3));
a3 /= gcd3;
b3 /= gcd3;
print(a3, b3);
printf("\n");
}
void divide(long long a1, long long b1, long long a2, long long b2){
print(a1, b1);
printf(" / ");
print(a2, b2);
printf(" = ");
if(a2 == 0){
printf("Inf");
}
else if(a2 < 0){
long long a3 = -1 * a1 * b2;
long long b3 = -1 * b1 * a2;
//Reduced to the simplest form, without fractional form
long long gcd3 = abs(gcd(a3, b3));
a3 /= gcd3;
b3 /= gcd3;
print(a3, b3);
}
else{
long long a3 = a1 * b2;
long long b3 = b1 * a2;
//Reduced to the simplest form, without fractional form
long long gcd3 = abs(gcd(a3, b3));
a3 /= gcd3;
b3 /= gcd3;
print(a3, b3);
}
printf("\n");
}
int main(){
long long a1, b1, a2, b2;
long long c1 = 0, c2 = 0;
scanf("%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2);
//Reduce to the simplest form first, without fractional form
long long gcd1 = abs(gcd(a1, b1));
a1 /= gcd1;
b1 /= gcd1;
long long gcd2 = abs(gcd(a2, b2));
a2 /= gcd2;
b2 /= gcd2;
//Unified participation in operation in the simplest form
add(a1, b1, a2, b2);
subtract(a1, b1, a2, b2);
multiply(a1, b1, a2, b2);
divide(a1, b1, a2, b2);
return 0;
}