-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrational.c
223 lines (176 loc) · 3.72 KB
/
rational.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
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
/**
* @brief Source file for rational.
*
* This file implements the rational functions.
*
* @file rational.c
* @author Thomas Irgang
* @date 17 Feb 2015
*/
#include <stdlib.h>
#include <stdio.h>
#include "rational.h"
/**
* @brief Largest common divisor.
*
* This function calculates the largest common divisor of the given
* two integer values.
*
* @param a
* integer a
* @param b
* integer b
* @return largest common divisor of a and b
*/
static int r_largest_common_divisor(int a, int b);
struct Rational *rational_create()
{
return rational_get(0, 1);
}
struct Rational *rational_get(int nominator, int denominator)
{
struct Rational *r = NULL;
r = (struct Rational *)malloc(sizeof(struct Rational));
if(r == NULL)
{
fprintf(stderr, ERROR_MALLOC_FAILED);
exit(EXIT_FAILURE);
}
r->n = nominator;
r->d = denominator;
return r;
}
void rational_normalize(struct Rational *r)
{
int div = r_largest_common_divisor(r->n , r->d);
r->n = (r->n)/div;
r->d = (r->d)/div;
if(r->d < 0)
{
r->n = -(r->n);
r->d = -(r->d);
}
}
struct Rational *rational_multiply(struct Rational *a, struct Rational *b)
{
int n, d;
struct Rational *r;
n = (a->n) * (b->n);
d = (a->d) * (b->d);
r = rational_get(n, d);
rational_normalize(r);
return r;
}
struct Rational *rational_divide(struct Rational *a, struct Rational *b)
{
int n, d;
struct Rational *r = NULL;
n = (a->n) * (b->d);
d = (a->d) * (b->n);
r = rational_get(n, d);
rational_normalize(r);
return r;
}
struct Rational *rational_add(struct Rational *a, struct Rational *b)
{
int n, d;
struct Rational *r;
n = (a->n) * (b->d) + (b->n) * (a->d);
d = (a->d) * (b->d);
r = rational_get(n, d);
rational_normalize(r);
return r;
}
struct Rational *rational_subtract(struct Rational *a, struct Rational *b)
{
int n, d;
struct Rational *r;
n = (a->n) * (b->d) - (b->n) * (a->d);
d = (a->d) * (b->d);
r = rational_get(n, d);
rational_normalize(r);
return r;
}
void rational_print(struct Rational *a)
{
if(a->d == 1)
{
printf("%d", a->n);
}
else
{
printf("%d/%d", a->n, a->d);
}
}
char **rational_to_string(struct Rational *a)
{
char **string = NULL;
int n;
string = (char **)malloc(sizeof(char *));
if(string == NULL)
{
fprintf(stderr, ERROR_MALLOC_FAILED);
exit(EXIT_FAILURE);
}
*string = (char *)malloc(BUFFER * sizeof(char));
if(*string == NULL)
{
fprintf(stderr, ERROR_MALLOC_FAILED);
exit(EXIT_FAILURE);
}
if(a->d == 1)
{
n = sprintf(*string, "%d", a->n);
}
else
{
n = sprintf(*string, "%d/%d", a->n, a->d);
}
if(n>(BUFFER - 1))
{
fprintf(stderr, "Memory error in toString() method of rational. Emergeny stop program!\n");
exit(EXIT_FAILURE);
}
*string = (char *)realloc(*string, (n+1) * sizeof(char));
return string;
}
static int r_largest_common_divisor(int a, int b)
{
int tmp;
a = (a<0)?-a:a;
b = (b<0)?-b:b;
do
{
if(b > a)
{
tmp = a;
a = b;
b = tmp;
}
a -= b;
}
while(b > 0);
return a;
}
int rational_is_a_smaller_than_b(struct Rational *a, struct Rational *b)
{
int va, vb;
va = (a->n) * (b->d);
vb = (b->n) * (a->d);
if(va < vb)
{
return 1;
}
else
{
return 0;
}
}
struct Rational *rational_invert_sign(struct Rational *a)
{
return rational_get(-a->n, a->d);
}
struct Rational *rational_clone(struct Rational *a)
{
return rational_get(a->n, a->d);
}