2021/11/27
#include<stdio.h>
main()
{
int a, b, c, d;
int fm, fz;
char sign, temp;
printf("please input a decimal expression,system will return its result:\n");
scanf_s("%d/%d%c%d/%d", &a, &b, &sign, 2, &c, &d);
if (sign == '+')
fz = a * getLcm(b, d) / b + c * getLcm(b, d) / d;
else if (sign == '-')
fz = a * getLcm(b, d) / b - c * getLcm(b, d) / d;
if (fz == 0)
{
printf("the result is:0\n");
return 0;
}
fm = getLcm(b, d);
//得到分子和分母
if (fz < 0)
{
fz = -fz;
temp = '-';
printf("%c", temp);
}
//判断结果的符号
if (fz % fm == 0)
printf("the result is:%d\n", fz / fm);
else
printf("the result is:%d/%d\n", fz / getGcd(fz, fm), fm / getGcd(fz, fm));
return 0;
//输出结果
}
int getGcd(int a, int b)
{
while (b)
{
int reminder = a % b;
a = b;
b = reminder;
}
return a;
}
int getLcm(int a, int b)
{
return a * b / getGcd(a, b);
}