题目描述
输入
输出
样例输入
样例输出
提示
#include<stdio.h>
#include<math.h>
int gys( int a, int b )
{
return b==0?a:gys( b,a%b );
}
int main()
{
int x1,y1, x2,y2;
char a;
int i, t;
while( ~scanf( "%d/%d%c%d/%d", &x1,&y1,&a, &x2,&y2 ) )
{
int num1=0, num2=0, k1=0;
if(( x1 == 0 && x2 == 0 ) || ( x1 == x2 && y1 == y2))
printf( "0\n");
else if( a == '+' )
{
if( y1 == y2 )
{
num2 = y1;
num1 = x1 + x2;
k1 = gys( num1, num2 );
num2 /= k1;
num1 /= k1;
}
else if( y1 != y2)
{
num2 = y1*y2 ;
num1 = x1*y2 + x2*y1;
k1 = gys( num1, num2 );
num2 /= k1;
num1 /= k1;
}
printf("%d/%d\n",num1,num2);
}
else if( a == '-' )
{
if( y1 == y2 )
{
num2 = y1;
num1 = x1 - x2;
k1 = gys( num1, num2 );
num2 /= k1;
num1 /= k1;
}
else
{
num2 = y1* y2;
num1 = x1*y2-x2*y1;
k1 = gys( num1, num2 );
num2 /= k1;
num1 /= k1;
if( num1 == 0 )
printf( "0\n" );
if( num2 < 0 )
{
num1 = -num1;
num2 = -num2;
}
}
printf( "%d/%d\n",num1,num2 );
}
}
}
#include<stdio.h>
#include<stdlib.h>
int GCD( int a,int b )
{
return b==0?a:GCD( b,a%b );
}
int main()
{
int a,b,c,d;
char f;
while( scanf( "%d/%d%c%d/%d",&a,&b,&f,&c,&d )!=EOF )
{
int t1=a*d,t2=b*c,t3=b*d,t;
if( f=='-' )
t=t1-t2;
else t=t1+t2;
if( t==0 )
printf( "0\n" );
else
{
int D=GCD( abs( t ),t3 );
printf( "%d/%d\n",t/D,t3/D );
}
}
}