This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frac.h
252 lines (212 loc) · 5.07 KB
/
frac.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* frac.h
* 2016/05/26(Thu)
* walkingmask
* discripthon
* Calculating fractional api
*
* 2016/05/28 Up to date
* - added some functions
* - It outputs the integer value when the denominator is 1
* - Complex calculation of an integer value
* - add an structure of fraction, but it is not in use now
*
* 2016/05/29 Up to date
* - exception handling of the case expression is too long in frac()
*
* ToDo
* - add the process for the operation of very large value
* that Within the range of the fomula's limitation
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* the structure of the fraction
* caution: it is not in use yet
*/
typedef struct {
int top; // top part
int bot; // bottom part
int in; // integer part
} frac_t;
/*
* the number is a fraction or an integer?
* caution: another possibility is not taken (like a float...)
*/
int isfrac(char *num) {
int i;
for (i=0; i<strlen(num); i++)
if (num[i] == '/') return 0;
return 1;
}
/*
* do convert: an integer -> a fraction
*/
void int2frac(char *i, char *f) {
strcpy(f, i);
f[strlen(i)] = '/';
f[strlen(i)+1] = '1';
f[strlen(i)+2] = '\0';
}
/*
* can convert the fraction to an integer?
*/
int canconv2int(char *f) {
int top, bot;
sscanf(f, "%d/%d", &top, &bot);
if (top%bot == 0) return 0;
return 1;
}
/*
* do convert: an integer -> a fraction
*/
void frac2int(char *f) {
int top, bot;
sscanf(f, "%d/%d", &top, &bot);
sprintf(f, "%d", top/bot);
}
/*
* get the greatest common divisor of x and y
*/
int gcd(int x, int y) {
if (y == 0) return x;
return gcd(y, x%y);
}
/*
* get the least common multiple of x and y
*/
int lcm(int x, int y) {
if ( (0 == x) || (0 == y) ) return 0;
return ((x/ gcd(x, y)) * y);
}
/*
* do reduction
*/
void reduc(int *top, int *bot) {
int comd;
if ( (comd = abs( gcd(*top, *bot) )) != 1) {
*top = *top / comd;
*bot = *bot / comd;
}
}
/*
* a simple addition of fraction
* f1 : fraction1
* f2 : fraction2
* res : result
* return : there is no reason now
*/
int fracadd(const char *f1, const char *f2, char *res) {
int top1, top2, bot1, bot2, comm, rest, resb;
sscanf(f1, "%d/%d", &top1, &bot1);
sscanf(f2, "%d/%d", &top2, &bot2);
// reducing
if (bot1 != bot2) {
comm = abs( lcm(bot1, bot2) );
top1 = top1 * (comm / bot1);
top2 = top2 * (comm / bot2);
bot1 = bot2 = comm;
}
rest = top1 + top2;
resb = bot1;
reduc(&rest, &resb);
sprintf(res, "%d/%d", rest, resb);
return 0;
}
/*
* a simple subtraction of fraction
*/
int fracsub(const char *f1, const char *f2, char *res) {
int top1, top2, bot1, bot2, comm, rest, resb;
sscanf(f1, "%d/%d", &top1, &bot1);
sscanf(f2, "%d/%d", &top2, &bot2);
// pre reducing
if (bot1 != bot2) {
comm = abs( lcm(bot1, bot2) );
top1 = top1 * (comm / bot1);
top2 = top2 * (comm / bot2);
bot1 = bot2 = comm;
}
rest = top1 - top2;
resb = bot1;
reduc(&rest, &resb);
sprintf(res, "%d/%d", rest, resb);
return 0;
}
/*
* a simple multiplication of fraction
*/
int fracmult(const char *f1, const char *f2, char *res) {
int top1, top2, bot1, bot2, rest, resb;
sscanf(f1, "%d/%d", &top1, &bot1);
sscanf(f2, "%d/%d", &top2, &bot2);
// pre reduction
reduc(&top1, &bot2);
reduc(&top2, &bot1);
rest = top1 * top2;
resb = bot1 * bot2;
reduc(&rest, &resb);
sprintf(res, "%d/%d", rest, resb);
return 0;
}
/*
* a simple division of fraction
*/
int fracdiv(const char *f1, const char *f2, char *res) {
int top, bot;
char tempf[16];
// reverse the fraction 2
sscanf(f2, "%d/%d", &top, &bot);
sprintf(tempf, "%d/%d", bot, top);
// multiplication
fracmult(f1, tempf, res);
return 0;
}
/*
* fraction calculator
* f : fomula of fraction
* fomula must be like this "1/2 * 3/4"
*/
int frac(const char *f, char *res) {
// exception handling of the case expression is too long
if (strlen(f) > 32) {
fprintf(stderr, "the fomula is too long.\n");
exit(EXIT_FAILURE);
}
char tempf[32], *f1, *op, *f2, ft1[16], ft2[16];
// convert const char* to char*
strcpy(tempf, f);
// get the fractions and operator from fomula
f1 = strtok(tempf, " ");
op = strtok(NULL, " ");
f2 = strtok(NULL, " ");
// for convert from an integer to a fraction
if (isfrac(f1) != 0) {
int2frac(f1, ft1);
f1 = ft1;
}
if (isfrac(f2) != 0) {
int2frac(f2, ft2);
f2 = ft2;
}
// branch by the operator
switch (*op) {
case '+':
fracadd(f1, f2, res);
break;
case '-':
fracsub(f1, f2, res);
break;
case '*':
fracmult(f1, f2, res);
break;
case '/':
fracdiv(f1, f2, res);
break;
default:
break;
}
// for convert from a fraction to an integer
if (canconv2int(res) == 0) frac2int(res);
return 0;
}