c语言简单加减法代码
时间: 2023-11-13 20:05:44 浏览: 104
C语言简单加减法代码如下:
#include <stdio.h>
int main() {
int a, b, c, d;
char op;
while (scanf("%d/%d%c%d/%d", &a, &b, &op, &c, &d) != EOF) {
int den = b * d;
int num;
if (op == '+') {
num = a * d + c * b;
} else {
num = a * d - c * b;
}
int gcd = num < 0 ? -num : num;
int tmp = den < 0 ? -den : den;
while (tmp != 0) {
int r = gcd % tmp;
gcd = tmp;
tmp = r;
}
num /= gcd;
den /= gcd;
if (num == 0) {
printf("0\n");
} else if (den == 1) {
printf("%d\n", num);
} else {
printf("%d/%d\n", num, den);
}
}
return 0;
}
这段代码实现了分数的加减法,输入格式为"a/b+/-c/d",输出格式为最简分数形式。
阅读全文