-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplify.c
191 lines (173 loc) · 3.68 KB
/
simplify.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
# include "simplify.h"
void simplify_plus(struct s_tree **node, struct s_list *list, float *coef, int krisbool)
{
if(!*node)
return;
if((*node)->type == OPERAND && *((enum e_operator*) (*node)->data) == PLUS)
{
if(krisbool == 0)
{
list = init_list();
*coef = 0;
}
simplify_plus(&(*node)->left, list, coef, 1);
simplify_plus(&(*node)->right, list, coef, 1);
(*node)->left = NULL;
(*node)->right = NULL;
free_tree(*node);
if(krisbool == 0)
{
*node = rebuild_tree(list, *coef, 0);
free_list(list);
list = NULL;
}
else
*node = NULL;
}
else
{
if(krisbool == 1)
{
if ((*node)->type == VALUE)
{
*coef += *((float*)(*node)->data);
free_tree(*node);
*node = NULL;
}
else
push_list(list, *node);
}
else
{
simplify_plus(&(*node)->left, list, coef, 0);
simplify_plus(&(*node)->right, list, coef, 0);
}
}
}
void simplify_minus(struct s_tree **node, int coef)
{
if(!*node)
return;
if ((*node)->type == OPERAND)
{
if (*((enum e_operator*) (*node)->data) == MINUS)
{
simplify_minus(&(*node)->right, coef * (-1));
if ((*node)->left == NULL)
{
struct s_tree *tmp = (*node)->right;
(*node)->right = NULL;
free_tree(*node);
*node = tmp;
}
else
{
*((enum e_operator*) (*node)->data) = PLUS;
simplify_minus(&(*node)->left, coef);
}
}
else if (*((enum e_operator*) (*node)->data) == TIME)
{
simplify_minus(&(*node)->right, coef);
}
else
{
simplify_minus(&(*node)->right, coef);
simplify_minus(&(*node)->left, coef);
}
}
else if ((*node)->type == VALUE)
*((float*) (*node)->data) *= coef;
else if ((*node)->type == VARIABLE)
((struct s_variable*) (*node)->data)->mult *= coef;
else
{
((struct s_function*) (*node)->data)->mult *= coef;
simplify_minus(&(*node)->left, 1);
}
}
struct s_tree *rebuild_tree(struct s_list *list, float coef, int mult)
{
struct s_tree *cur, *tree;
float *tmp, neutre;
int size = size_list(list);
if (size)
{
cur = pop_list(list);
size--;
if (mult)
{
neutre = 1;
simplify_mult(&cur, NULL, &neutre, 0);
multiplie_tree(cur, coef);
if(size)
{
tree = build_operator('*');
tree->left = cur;
if(size > 1)
tree->right = rebuild_tree(list, 1, mult);
else
tree->right = pop_list(list);
}
else
tree = cur;
}
else
{
neutre = 0;
simplify_plus(&cur, NULL, &neutre, 0);
tree = build_operator('+');
tree->left = cur;
tree->right = rebuild_tree(list, coef, mult);
}
return tree;
}
tmp = malloc(sizeof (float));
*tmp = coef;
return build_float(tmp);
}
void simplify_mult(struct s_tree **node, struct s_list *list, float *coef, int krisbool)
{
if(!*node)
return;
if((*node)->type == OPERAND && *((enum e_operator*)(*node)->data) == TIME)
{
if(krisbool == 0)
{
list = init_list();
*coef = 1;
}
simplify_mult(&(*node)->left, list, coef, 1);
simplify_mult(&(*node)->right, list, coef, 1);
(*node)->left = NULL;
(*node)->right = NULL;
free_tree(*node);
if(krisbool == 0)
{
*node = rebuild_tree(list, *coef, 1);
free_list(list);
list = NULL;
}
else
*node = NULL;
}
else
{
if(krisbool == 1)
{
if ((*node)->type == VALUE)
{
*coef *= *((float*)(*node)->data);
free_tree(*node);
*node = NULL;
}
else
push_list(list, *node);
}
else
{
simplify_mult(&(*node)->left, list, coef, 0);
simplify_mult(&(*node)->right, list, coef, 0);
}
}
}