#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int input(int zero)
{
int i;
while (1) {
scanf("%d", &i);
if (i == 0 && zero) {
continue;
}
if (i < -32768 || 32767 < i) {
continue;
}
break;
}
return i;
}
int main()
{
int bi1, bo1, op, bi2, bo2;
int bi, bo;
int g1, g2, g3;
printf("1番目分数の分子=\n");
bi1 = input(0);
printf("1番目分数の分母=\n");
bo1 = input(1);
printf("演算子(1:足し算 2:引き算 3:掛け算 4:割り算)=\n");
do {
scanf("%d", &op);
} while (op < 1 || 4 < op);
printf("2番目分数の分子=\n");
bi2 = input(op == 4);
printf("2番目分数の分母=\n");
bo2 = input(1);
switch (op) {
case 1: // 足し算
bi = bi1 * bo2 + bi2 * bo1;
bo = bo1 * bo2;
break;
case 2: // 引き算
bi = bi1 * bo2 - bi2 * bo1;
bo = bo1 * bo2;
break;
case 3: // 掛け算
bi = bi1 * bi2;
bo = bo1 * bo2;
break;
case 4: // 割り算
bi = bi1 * bo2;
bo = bo1 * bi2;
break;
}
// 約分 ユークリッドの互除法
g1 = bi;
g2 = bo;
while (g3 = g1 % g2) {
g1 = g2;
g2 = g3;
}
bi /= g2;
bo /= g2;
if (bo == 1) {
printf("%d\n", bi);
} else if (bi == 0) {
printf("%d\n", bi);
} else {
printf("%c%d/%d\n", (bi * bo < 0) ? '-' : ' ', abs(bi), abs(bo));
}
return 0;
}
I2RlZmluZSBfQ1JUX1NFQ1VSRV9OT19XQVJOSU5HUwojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoKaW50IGlucHV0KGludCB6ZXJvKQp7CglpbnQgaTsKCgl3aGlsZSAoMSkgewoJCXNjYW5mKCIlZCIsICZpKTsKCQlpZiAoaSA9PSAwICYmIHplcm8pIHsKCQkJY29udGludWU7CgkJfQoJCWlmIChpIDwgLTMyNzY4IHx8IDMyNzY3IDwgaSkgewoJCQljb250aW51ZTsKCQl9CgkJYnJlYWs7Cgl9CglyZX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