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
|
/*
*
* Operations sur les scalaires ( rationnels )
*
*/
#include <math.h>
#include <string.h>
#include "scalaires.h"
#include "exceptions.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define _(x) x
#endif
#define PRECISION 1E6
typedisplay display=DEC;
static int pgcd(int a, int b)
{
if (a < b)
return pgcd(b, a);
if (!b)
return a;
return pgcd(b, a % b);
}
rationnel rat_constr_zero(void)
{ /* renvoie 0 */
rationnel temp;
temp.num = 0;
temp.denom = 1;
return temp;
}
rationnel rat_constr(int num, int denom)
{ /* cree une fraction */
rationnel temp;
int sgnnum = 1, sgndenom = 1;
if (num < 0) {
sgnnum = -1;
num = -num;
}
if (denom < 0) {
sgndenom = -1;
denom = -denom;
}
if (!num) {
temp.num = 0;
temp.denom = 1;
} else if (denom) {
temp.num = sgnnum * sgndenom * num / pgcd(num, denom);
temp.denom = denom / pgcd(num, denom);
} else {
exception(2, _("rat_constr: division by zero"));
}
return temp;
}
rationnel rat_constr_from_double(double flt)
{ /* cree une fraction a partir d un double */
return rat_constr(floor(flt * PRECISION), PRECISION);
}
void rat_destruct(rationnel rat)
{ /* destructeur */
}
double rat_to_double(rationnel rat)
{ /* obtention du double correspondant a un rationnel */
return ((double) rat.num / (double) rat.denom);
}
rationnel rat_addition(rationnel rat1, rationnel rat2)
{ /* addition */
return rat_constr(rat1.num * rat2.denom + rat2.num * rat1.denom, rat1.denom * rat2.denom);
}
rationnel rat_soustraction(rationnel rat1, rationnel rat2)
{ /* soustraction */
return rat_constr(rat1.num * rat2.denom - rat2.num * rat1.denom, rat1.denom * rat2.denom);
}
rationnel rat_moinsunaire(rationnel rat1)
{ /* moins unaire */
return rat_constr(-rat1.num, rat1.denom);
}
rationnel rat_multiplication(rationnel rat1, rationnel rat2)
{ /* multiplication */
return rat_constr(rat1.num * rat2.num, rat1.denom * rat2.denom);
}
rationnel rat_division(rationnel rat1, rationnel rat2)
{ /* division */
if (!rat2.num)
exception(2,_("division by zero"));
return rat_constr(rat1.num * rat2.denom, rat1.denom * rat2.num);
}
char *rat_to_string(rationnel rat, int first)
{
static char resultat[128];
char temp[64];
resultat[0] = '\0';
if (rat.num<0) {
rat.num=-rat.num;
if (first) {
strcat(resultat,"-");
} else {
strcat(resultat,"- ");
}
} else {
if (!first) {
strcat(resultat,"+ ");
}
}
switch(display) {
case DEC:
if (rat.denom==1)
sprintf(temp,"%d", rat.num);
else
sprintf(temp,"%d/%d", rat.num, rat.denom);
break;
case HEX:
if (rat.denom==1)
sprintf(temp,"0x%x", rat.num);
else
sprintf(temp,"0x%x/0x%x", rat.num, rat.denom);
break;
case OCT:
if (rat.denom==1)
sprintf(temp,"0%o", rat.num);
else
sprintf(temp,"0%o/0%o", rat.num, rat.denom);
break;
case FLT:
sprintf(temp,"%g", rat_to_double(rat));
break;
}
strcat(resultat, temp);
return resultat;
}
|