-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequation.c
90 lines (86 loc) · 1.85 KB
/
equation.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
#include "equation.h"
double* developper(struct tree *t){
double* tab = NULL;
if(t->left){
double* tabg = developper(t->left);
double* tabd = developper(t->right);
double g = tabg[0];
double d = tabd[0];
//gestion *
if(get_char(t) == '*'){
tab = malloc(sizeof(double)*(g+d-2));
tab[0]= g+d-2;
for(int i = 1; i<tab[0];i++){
tab[i] = 0;
}
for(int i = 1; i<g;i++){
for(int j = 1; j<d;j++){
tab[j+i-1] += tabg[i]*tabd[j];
}
}
}
else{
//gestion + et -
if(g>d){
tab = malloc(sizeof(double)*g);
tab[0] = g;
for(int i = 1; i<g;i++){
tab[i] = 0;
}
if (get_char(t) == '+'){
for(int i = 1; i<d;i++){
tab[i] = tabg[i]+tabd[i];
}
for(int i = d; i<g;i++){
tab[i] = tabg[i];
}
}
else{
for(int i = 1; i<d;i++){
tab[i] = tabg[i]-tabd[i];
}
for(int i = d; i<g;i++){
tab[i] = tabg[i];
}
}
}
else{
tab = malloc(sizeof(double)*d);
tab[0]=d;
for(int i = 1; i<d-1;i++){
tab[i] = 0;
}
if (get_char(t) == '+'){
for(int i = 1; i<=g;i++){
tab[i] = tabg[i]+tabd[i];
}
for(int i = g; i<d;i++){
tab[i] = tabd[i];
}
}
else{
for(int i = 1; i<d;i++){
if(i>=g)
tab[i]=-tabd[i];
else
tab[i]=tabg[i]-tabd[i];
}
}
}
}
free(tabg);
free(tabd);
}
else if (get_char(t) == 'x'){
tab = malloc(sizeof(double)*3);
tab[0]=3;
tab[1]=0;
tab[2]=1;
}
else{
tab = malloc(sizeof(double)*2);
tab[0] = 2;
tab[1] = get_number(t);
}
return tab;
}