#include
#include
int GCD(int m,int n) //求最大公约数
{
int tmp;
m=abs(m);
n=abs(n);
if(m==n)
return m;
if(m{
tmp=m;
m=n;
n=tmp;
}
while(m%n!=0)
{
tmp=m;
m=n;
n=tmp%n;
}
return n;
}
int main()
{
int a,b,c,d; //两个分数的分子和分母
int gcd_mn;
int res_den,res_num; //分母和分子
char op;
scanf("%d/%d%c%d/%d",&a,&b,&op,&c,&d);
gcd_mn=GCD(b,d); //求最大公约数
res_den=b*d/gcd_mn;
if(op=='+')
res_num=a*(res_den/b)+c*(res_den/d);
else
res_num=a*(res_den/b)-c*(res_den/d);
if(res_num!=0)
{
gcd_mn=GCD(res_num,res_den);
res_num=res_num/gcd_mn;
res_den=res_den/gcd_mn;
printf("%d/%d\n",res_num,res_den);
}
else
printf("0\n");
return 0;
}
代码可能有点乱,运行无误