Skip to content

Commit 91e6173

Browse files
authored
Add Fraction Math in C (#4303)
1 parent 182d337 commit 91e6173

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

archive/c/c/fraction-math.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
typedef struct {
6+
int numerator;
7+
int denominator;
8+
} Fraction;
9+
10+
int gcd(int a, int b) {
11+
if (b == 0) return a;
12+
return gcd(b, a % b);
13+
}
14+
15+
Fraction simplify(Fraction f) {
16+
int g = gcd(abs(f.numerator), abs(f.denominator));
17+
f.numerator /= g;
18+
f.denominator /= g;
19+
if (f.denominator < 0) {
20+
f.numerator = -f.numerator;
21+
f.denominator = -f.denominator;
22+
}
23+
return f;
24+
}
25+
26+
Fraction parse_fraction(const char* str) {
27+
Fraction f;
28+
sscanf(str, "%d/%d", &f.numerator, &f.denominator);
29+
return simplify(f);
30+
}
31+
32+
Fraction add(Fraction a, Fraction b) {
33+
Fraction result = {
34+
a.numerator * b.denominator + b.numerator * a.denominator,
35+
a.denominator * b.denominator
36+
};
37+
return simplify(result);
38+
}
39+
40+
Fraction subtract(Fraction a, Fraction b) {
41+
Fraction result = {
42+
a.numerator * b.denominator - b.numerator * a.denominator,
43+
a.denominator * b.denominator
44+
};
45+
return simplify(result);
46+
}
47+
48+
Fraction multiply(Fraction a, Fraction b) {
49+
Fraction result = {
50+
a.numerator * b.numerator,
51+
a.denominator * b.denominator
52+
};
53+
return simplify(result);
54+
}
55+
56+
Fraction divide(Fraction a, Fraction b) {
57+
Fraction result = {
58+
a.numerator * b.denominator,
59+
a.denominator * b.numerator
60+
};
61+
return simplify(result);
62+
}
63+
64+
int compare(Fraction a, Fraction b) {
65+
int lhs = a.numerator * b.denominator;
66+
int rhs = b.numerator * a.denominator;
67+
if (lhs < rhs) return -1;
68+
if (lhs > rhs) return 1;
69+
return 0;
70+
}
71+
72+
void print_fraction(Fraction f) {
73+
printf("%d/%d\n", f.numerator, f.denominator);
74+
}
75+
76+
int main(int argc, char* argv[]) {
77+
if (argc != 4) {
78+
printf("Usage: ./fraction-math operand1 operator operand2\n");
79+
return 1;
80+
}
81+
82+
Fraction a = parse_fraction(argv[1]);
83+
Fraction b = parse_fraction(argv[3]);
84+
char* op = argv[2];
85+
86+
Fraction result;
87+
int cmp;
88+
89+
if (strcmp(op, "+") == 0) {
90+
result = add(a, b);
91+
print_fraction(result);
92+
} else if (strcmp(op, "-") == 0) {
93+
result = subtract(a, b);
94+
print_fraction(result);
95+
} else if (strcmp(op, "*") == 0) {
96+
result = multiply(a, b);
97+
print_fraction(result);
98+
} else if (strcmp(op, "/") == 0) {
99+
result = divide(a, b);
100+
print_fraction(result);
101+
} else if (strcmp(op, "==") == 0) {
102+
cmp = compare(a, b);
103+
printf("%d\n", cmp == 0);
104+
} else if (strcmp(op, ">") == 0) {
105+
cmp = compare(a, b);
106+
printf("%d\n", cmp > 0);
107+
} else if (strcmp(op, "<") == 0) {
108+
cmp = compare(a, b);
109+
printf("%d\n", cmp < 0);
110+
} else if (strcmp(op, ">=") == 0) {
111+
cmp = compare(a, b);
112+
printf("%d\n", cmp >= 0);
113+
} else if (strcmp(op, "<=") == 0) {
114+
cmp = compare(a, b);
115+
printf("%d\n", cmp <= 0);
116+
} else if (strcmp(op, "!=") == 0) {
117+
cmp = compare(a, b);
118+
printf("%d\n", cmp != 0);
119+
} else {
120+
printf("Invalid operator\n");
121+
return 1;
122+
}
123+
124+
return 0;
125+
}

0 commit comments

Comments
 (0)