Algorithm
TDA Rational
By Neilor Tonin, URI Brazil
You were invited to do a little job for your Mathematic teacher. The job is to read a Mathematic expression in format of two rational numbers (numerator / denominator) and present the result of the operation. Each operand or operator is separated by a blank space. The input sequence (each line) must respect the following format: number, (‘/’ char), number, operation char (‘/’, ‘*’, ‘+’, ‘-‘), number, (‘/’ char), number. The answer must be presented followed by ‘=’ operator and the simplified answer. If the answer can’t be simplified, it must be repeated after a ‘=’ operator.
Considering N1 and D1 as numerator and denominator of the first fraction, follow the orientation about how to do each one of these 4 operations:
Sum: (N1*D2 + N2*D1) / (D1*D2)
Subtraction: (N1*D2 - N2*D1) / (D1*D2)
Multiplication: (N1*N2) / (D1*D2)
Division: (N1/D1) / (N2/D2), that means (N1*D2)/(N2*D1)
Input
The input contains several cases of test. The first value is an integer N (1 ≤ N ≤ 1*104), indicating the amount of cases of test that must be read. Each case of test contains a rational value X (1 ≤ X ≤ 1000), an operation (-, +, * or /) and another rational value Y (1 ≤ Y ≤ 1000).
Output
The output must be a rational number, followed by a “=“ sign and another rational number, that is the simplification of the first value. In case of the first value can’t be simplified, the same value must be repeated after the ‘=’ sign.
Input Sample | Output Sample |
4 |
10/8 = 5/4 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int euclides(int a, int b)
{
int divisor, dividendo, c;
if(a == 0)
return 1;
if(b > a)
{
dividendo = b;
divisor = a;
}
else
{
dividendo = a;
divisor = b;
}
while(dividendo % divisor != 0)
{
c = dividendo % divisor;
dividendo = divisor;
divisor = c;
}
return divisor;
}
int main()
{
char c1, c2, c3;
int n, N1, N2, D1, D2, num, den, numS, denS, e;
scanf("%i", &n);
int i;
for (i = 0; i < n; ++i)
{
scanf("%i %c %i %c %i %c %i", &N1, &c1, &D1, &c2, &N2, &c3, &D2);
if(c2 == '+')
{
num = ((N1 * D2) + (N2 * D1));
den = (D1 * D2);
}
else if(c2 == '-')
{
num = ((N1 * D2) - (N2 * D1));
den = (D1 * D2);
}
else if(c2 == '*')
{
num = (N1 * N2);
den = (D1 * D2);
}
else
{
num = (N1 * D2);
den = (N2 * D1);
}
e = euclides(num, den>;
numS = num / e;
denS = den / e;
if(numS > 0 && denS > 0)
{
printf("%i/%i = %i/%in", num, den, numS, denS);
}
else
{
if(denS < 0)
{
denS = -denS;
numS = -numS;
}
printf("%i/%i = %i/%in", num, den, numS, denS>;
}
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
using namespace std;
int euclides(int a, int b)
{
int divisor, dividendo, c;
if(a == 0)
return 1;
if(b > a){
dividendo = b;
divisor = a;
}else{
dividendo = a;
divisor = b;
}
while(dividendo % divisor != 0)
{
c = dividendo % divisor;
dividendo = divisor;
divisor = c;
}
return divisor;
}
int main()
{
char c1, c2, c3;
int n, N1, N2, D1, D2, num, den, numS, denS, e;
scanf("%i", &n);
for (int i = 0; i < n; ++i)
{
scanf("%i %c %i %c %i %c %i", &N1, &c1, &D1, &c2, &N2, &c3, &D2);
if(c2 == '+'){
num = ((N1 * D2) + (N2 * D1));
den = (D1 * D2);
}else if(c2 == '-'){
num = ((N1 * D2) - (N2 * D1));
den = (D1 * D2);
}else if(c2 == '*'){
num = (N1 * N2);
den = (D1 * D2);
}else{
num = (N1 * D2);
den = (N2 * D1);
}
e = euclides(num, den>;
numS = num / e;
denS = den / e;
if(numS > 0 && denS > 0){
printf("%i/%i = %i/%in", num, den, numS, denS);
}else{
if(denS < 0){
denS = -denS;
numS = -numS;
}
printf("%i/%i = %i/%in", num, den, numS, denS>;
}
}
return 0;
}
Copy The Code &
Try With Live Editor