I'm currently working on this for my own practice. I get 4 digits and an operation.
The input is x1 y1 op x2 y2
and the fraction is x1/y1
and x2/y2
. If I get the input: 1 3 + 1 2
then it's 1/3 + 1/2
and the answer should be given the minimal fractional so it's 5/6
. I pass the testcases I get and I can't figure out what I'm doing wrong.
To summarize what I do:
- Read input and check the operation if it's
+
,-
,/
or*
. I generate a prime array to find the biggest common divisor. - Send the input to a function depending on which operation it is.
- I count the given input with simple math.
- Then I find the biggest common divisor and divide both numerator and denominator with this.
- After that I print out the result.
Here is the main function and how I handle if the operation is *
. I handle the other operation the same but with other math.
#include <iomanip>
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <iterator>
#include <array>
#include <stdio.h>
#include <string.h>
#include <cstddef>
#include <string>
#include <sstream>
#include <math.h>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;
long long nwd(long long a, long long b){
long long c;
while(b != 0){
c = a % b;
a = b;
b = c;
}
return a;
}
void add(long long x1, long long y1, long long x2, long long y2){
long long bottom = (y1) * (y2);
long long top = ((x1) * (y2)) + ((x2) * (y1));
//cout << bottom << " " << top << endl;
long long frac;
if(bottom != 0||top != 0){
frac = nwd(top,bottom);
}else{
frac = 1;
}
string sign = "";
if(top * bottom < 0){
sign = "-";
}else{
sign = "";
}
printf("%s%lld / %lld\n",sign.c_str(),abs(top/frac),abs(bottom/frac) );
}
void sub(long long x1, long long y1,long long x2, long long y2){
long long bottom = (y1) * (y2);
long long top = ((x1) * (y2)) - ((x2) * (y1));
long long frac;
if(bottom != 0||top != 0){
frac = nwd(top,bottom);
}else{
frac = 1;
}
string sign = "";
if(top * bottom < 0){
sign = "-";
}else{
sign = "";
}
printf("%s%lld / %lld\n",sign.c_str(),abs(top/frac),abs(bottom/frac) );
}
void divi(long long x1, long long y1, long long x2, long long y2){
long long top = (x1) * (y2);
long long bottom = (x2) * (y1);
long long frac;
if(bottom != 0||top != 0){
frac = nwd(top,bottom);
}else{
frac = 1;
}
string sign = "";
if(top * bottom < 0){
sign = "-";
}else{
sign = "";
}
printf("%s%lld / %lld\n",sign.c_str(),abs(top/frac),abs(bottom/frac) );
}
void mult(long long x1, long long y1, long long x2, long long y2){
long long top = (x1) * (x2);
long long bottom = (y2) * (y1);
long long frac;
if(bottom != 0||top != 0){
frac = nwd(top,bottom);
}else{
frac = 1;
}
string sign = "";
if(top * bottom < 0){
sign = "-";
}else{
sign = "";
}
printf("%s%lld / %lld\n",sign.c_str(),abs(top/frac),abs(bottom/frac) );
}
int main()
{
int numOp;
scanf("%d", &numOp);
while(numOp != 0){
long long x1,x2,y1,y2;
char op[2];
scanf("%lld %lld %s %lld %lld", &x1, &y1, op, &x2, &y2);
if( op[0] == '+'){
add(x1, y1, x2,y2);
}
else if(op[0] == '-'){
sub(x1,y1,x2,y2);
}
else if(op[0] == '/'){
divi(x1,y1,x2,y2);
}
else{
mult(x1,y1,x2,y2);
}
numOp--;
}
return 0;
}
Here is my code with the given testcase and I get the correct result. I need some tips with either different testcases or any suggestions.
nwd
for what is called in English GCD (Greatest Common Divisor) suggests you might be from Poland. ;) It is better to use English names for readability. \$\endgroup\$