View Code of Problem 3494

#include <stdio.h>
#include <math.h>
 
int gcd(int a,int b){
	if(b==0)
		return a;
	return gcd(b,a%b);
}
int main() {
	int a,b,c,d;
	char o;
	while(scanf("%d/%d%c%d/%d",&a,&b,&o,&c,&d)!=EOF){
		a*=d;
		c*=b;
		int zi,mu;
		mu=b*d;
		if(o=='+')
			zi=a+c;
		else zi=a-c; 
		int temp=gcd(abs(zi),mu);
		if(zi==0)
			printf("0\n");
		else if(zi%mu==0)
			printf("%d\n",zi/mu); 
		else	printf("%d/%d\n",zi/temp,mu/temp);
	} 
        return 0;
}

Double click to view unformatted code.


Back to problem 3494