summaryrefslogtreecommitdiff
path: root/lib/fonctions.c
blob: 674b2e830eb563e32e5aefbbd068c37978e80d4a (plain)
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
/*
 *
 * fonctions avancees sur les polynomes
 *
 */

#include "fonctions.h"
#include "pile.h"
#include "hash.h"
#include "main.h"
#include "scalaires.h"
#include "terminal.h"
#include "exceptions.h"
#include "polynom.h"
#include "interface.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define _(x) x
#endif

/*
 * Nous allons utiliser des pointeurs sur des fonctions. Voici donc les typedefs pour notre
 * structure... 
 */
typedef void (*func_name) (polynome arg1, polynome arg2, polynome arg3);

typedef struct func_t {
    func_name func;
    char *nom;
    int arite;
} func_t;

/*
 * ... et la structure elle-meme 
 */

static func_t func_table[] = {
    {deriv, "deriv", 1,},
    {derivn, "derivn", 2},
    {integ, "int", 1},
    {printvars, "printvars", 0},
    {help, "help", 0},
    {setdisplay, "setdisplay", 1},
    {reinit, "reinit", 0},
    {exit_call, "exit", 0},
    {setsmartprint, "setsmartprint", 1},
    {NULL, NULL, -1}
};

/*
 * On cherche simplement la routine a appeler parmi la table des fonctions 
 */

void appel_fonction(char *nom, int arite, polynome p1, polynome p2, polynome p3)
{
    int i = 0;
    int trouve = 0;

    while ((func_table[i].nom) && (!trouve)) {
	if (!strcmp(func_table[i].nom, nom))
	    trouve = 1;
	i++;
    }
    if (trouve) {
	if (func_table[i - 1].arite == arite) {
	    (*func_table[i - 1].func) (p1, p2, p3);
	} else {
	    exception(1, _("appel_fonction: incorrect arg number"));
	}
    } else {
	exception(1, _("appel_fonction: non-existent function"));
    }
}

/*
 * Fonction de derivation - rajoute le resultat sur la pile. 
 */

void deriv(polynome p1, polynome p2, polynome p3)
{
    polynome resultat = NULL, temp = NULL, t;

    while (p1) {
	if (p1->degre) {
	    t =
		ply_constr(rat_constr((p1->coef.num) * (p1->degre), p1->coef.denom),
			   (p1->degre - 1));
	    if (t) {
		if (resultat) {
		    temp->suiv = t;
		    temp = t;
		} else {
		    resultat = t;
		    temp = t;
		}
	    }
	}
	p1 = p1->suiv;
    }
    push_pile_poly(resultat);
}

/*
 * Fonction paresseuse 
 */

void derivn(polynome p1, polynome p2, polynome p3)
{
    pile_elem temp;
    int i;

    if (p1) {
	if ((p1->degre == 0) && (p1->coef.num > 0) && (p1->coef.denom == 1)) {
	    push_pile_poly(NULL);
	    for (i = 0; i < p1->coef.num; i++) {
		temp = pop_pile(1);
		ply_destruct(temp.poly);
		deriv(temp.poly, NULL, NULL);
	    }
	} else {
	    exception(1, _("derivn: invalid 2nd arg"));
	}
    } else {
	exception(1, _("derivn: invalid 2nd arg"));
    }
}

/*
 * Integration d'un polynome 
 */

void integ(polynome p1, polynome p2, polynome p3)
{
    polynome resultat = NULL, temp = NULL, t;

    while (p1) {
	t =
	    ply_constr(rat_constr((p1->coef.num), (p1->coef.denom) * (p1->degre + 1)),
		       (p1->degre + 1));
	if (t) {
	    if (resultat) {
		temp->suiv = t;
		temp = t;
	    } else {
		resultat = t;
		temp = t;
	    }
	}
	p1 = p1->suiv;
    }
    push_pile_poly(resultat);
}

/*
 * Quelques fonctions explicites... 
 */

void printvars(polynome p1, polynome p2, polynome p3)
{
    AfficheTableau(variables);
}

void help(polynome p1, polynome p2, polynome p3)
{
    printf(_("Available functions:\n"
	     ". deriv(p); first derivative of p\n"
	     ". derivn(p, n); nth derivative of p\n"
	     ". integ(p); primitive of p\n"
	     ". printvars(); print all variables\n"
	     ". help(); this help message\n"
	     ". setdisplay(n); set integer display format\n"
	     "\tn=1: DECIMAL, n=2: HEXA\n"
	     "\tn=3: OCTAL, n=4: FLOAT\n"
	     ". smartprint(bool); toggle smart print of polynoms\n"
	     ". reinit(); clear all variables\n" ". exit(); end program\n"));
}

void setdisplay(polynome p1, polynome p2, polynome p3)
{
    if (p1) {
	if ((!p1->degre)) {
	    switch (p1->coef.num) {
	    case 1:
		display = DEC;
		break;
	    case 2:
		display = HEX;
		break;
	    case 3:
		display = OCT;
		break;
	    case 4:
		display = FLT;
		break;
	    default:
		exception(1, _("setdisplay: invalid arg"));
	    }
	} else {
	    exception(1, _("setdisplay: invalid arg"));
	}
    } else {
	exception(1, _("setdisplay: invalid arg"));
    }
}

void reinit(polynome p1, polynome p2, polynome p3)
{
    DetruitTab(&variables);
    Initialise(&variables);
}

void exit_call(polynome p1, polynome p2, polynome p3)
{
    quit = 1;
}

void setsmartprint(polynome p1, polynome p2, polynome p3)
{
    if (p1) {
	if ((!p1->degre)) {
	    smartprint = p1->coef.num;
	} else {
	    exception(1, _("setsmartprint: invalid arg"));
	}
    } else {
	smartprint = 0;
    }
}