-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1034.c
74 lines (62 loc) · 1.33 KB
/
1034.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
long mgcd(long a, long b);
void printnum(long a, long b);
int main()
{
long a1, b1, a2, b2;
scanf("%ld/%ld %ld/%ld", &a1, &b1, &a2, &b2);
char opstr[4] = {'+', '-', '*', '/'};
for(int i = 0; i < 4; i++){
printnum(a1, b1);
printf(" %c ", opstr[i]);
printnum(a2, b2);
printf(" = ");
switch(i){
case 0: printnum(a1 * b2 + a2 * b1, b1 * b2); break;
case 1: printnum(a1 * b2 - a2 * b1, b1 * b2); break;
case 2: printnum(a1 * a2, b1 * b2); break;
case 3: printnum(a1 * b2, a2 * b1); break;
}
printf("\n");
}
return 0;
}
long mgcd(long a, long b){
long r;
while((r = a % b)){
a = b;
b = r;
}
return b;
}
void printnum(long a, long b){
if(b == 0){
printf("Inf");
return;
}
int flag = 1;
if(a < 0){
a = -a;
flag *= -1;
}
if(b < 0){
b = -b;
flag *= -1;
}
long gcd = mgcd(a, b);
a /= gcd;
b /= gcd;
if(flag == -1){
printf("(-");
}
if(a / b && a % b){
printf("%ld %ld/%ld", a / b, a % b, b);
}else if(a % b){
printf("%ld/%ld", a % b, b);
}else{
printf("%ld", a / b);
}
if(flag == -1){
printf(")");
}
}