View Code of Problem 3494

#include<stdio.h>
#include<string.h>
#include<math.h>
int gcd(int a,int b)
{
	if(b==0) return a;
	else return gcd(b,a%b);
}
int main()
{
	int a,b,c,d,fz,fm;
	char ch;
	while(scanf("%d/%d%c%d/%d",&a,&b,&ch,&c,&d)!=EOF)
	{
		if(ch=='+') 
		{
			fz=a*d+b*c;
			fm=b*d;
		}
		else if(ch=='-') 
		{
			fz=a*d-b*c;
			fm=b*d;
		}
		else if(ch=='*') 
		{
			fz=a*c;
			fm=b*d;
		}
		else if(ch=='/')
		{
			fz=a*d;
			fm=b*c;
		 } 
		int t=gcd(abs(fz),abs(fm));
		if(fz==0) printf("0\n");
		else if(fm==1) printf("%d\n",fz);
		else if(fz<0||fm<0)
		{
			printf("-%d/%d\n",abs(fz)/t,abs(fm)/t);
		}
		else 
		printf("%d/%d\n",fz/t,fm/t);
	}
	return 0;
}
/*
F:\temp\21619361.46348\Main.cc: In function 'int main()':
F:\temp\21619361.46348\Main.cc:36: error: 'abs' was not declared in this scope
*/

Double click to view unformatted code.


Back to problem 3494