-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_normalizer.c
More file actions
292 lines (244 loc) · 8.42 KB
/
function_normalizer.c
File metadata and controls
292 lines (244 loc) · 8.42 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include "function_normalizer.h"
#include "defs.h"
#include "parser_utils.h"
const char VALID_OPERATORS[] = VALID_DEFINED_OPERATORS;
// Valid functions are ordered by length (descending) to prevent functions from the conflict
// For example: sin(x) and sinh(x)
const char* VALID_FUNCTIONS[] = {
FUNC_ASIN, FUNC_ACOS, FUNC_ATAN, FUNC_SINH, FUNC_COSH, FUNC_TANH,
FUNC_ABS, FUNC_EXP, FUNC_LOG, FUNC_SIN, FUNC_COS, FUNC_TAN, FUNC_LN
};
const size_t NUM_VALID_FUNCTIONS = sizeof(VALID_FUNCTIONS) / sizeof(VALID_FUNCTIONS[0]);
char* remove_spaces(const char* function) {
if (function == NULL) {
return NULL;
}
size_t length = strlen(function);
char* cleaned_str = (char*)malloc((length + 1) * sizeof(char));
if (cleaned_str == NULL) {
return NULL;
}
char* dest = cleaned_str;
for (size_t i = 0; i < length; i++) {
if (!isspace((unsigned char)function[i])) {
*dest++ = function[i];
}
}
*dest = END_STRING_CHAR;
return cleaned_str;
}
bool is_valid_expression(char* function) {
if (function == NULL) {
return false;
}
size_t len = strlen(function);
size_t i = 0;
// First, we check the balance of the brackets
if (!check_parentheses_balance(function)) {
return false;
}
while (i < len) {
len = strlen(function);
// Checking functions
if (check_valid_functions(function, &i)) {
continue;
}
// Checking numbers
if (isdigit(function[i]) || function[i] == DECIMAL_POINT || function[i] == DECIMAL_E_CHAR) {
if (!check_number(function, &i)) {
return false;
}
continue;
}
// Checking a variable
if (function[i] == VALID_VARIABLE) {
i++;
continue;
}
// Checking operators
if (strchr(VALID_OPERATORS, function[i]) != NULL) {
i++;
continue;
}
// Checking brackets
if (function[i] == OPERATOR_LEFT_PAREN || function[i] == OPERATOR_RIGHT_PAREN) {
i++;
continue;
}
return false;
}
return true;
}
bool check_parentheses_balance(const char* expression) {
if (expression == NULL) {
return false;
}
int balance = 0;
size_t i = 0;
while (expression[i] != END_STRING_CHAR) {
if (expression[i] == OPERATOR_LEFT_PAREN) {
balance++;
} else if (expression[i] == OPERATOR_RIGHT_PAREN) {
balance--;
if (balance < 0) {
// A closing parenthesis without a corresponding opening parenthesis
return false;
}
}
i++;
}
// If the balance is zero, the brackets are balanced.
return balance == 0;
}
bool check_valid_functions(const char* expression, size_t *index) {
if (expression == NULL || index == NULL) {
return false;
}
for (size_t j = 0; j < NUM_VALID_FUNCTIONS; j++) {
size_t func_len = strlen(VALID_FUNCTIONS[j]);
// Valid functions are ordered by length (descending) to prevent functions from the conflict
// For example: sin(x) and sinh(x)
if (strncmp(&expression[*index], VALID_FUNCTIONS[j], func_len) == 0) {
*index += func_len;
return true;
}
}
return false;
}
// Helper Function to Convert Scientific Notation to Standard Decimal
bool convert_scientific_to_decimal(char* expression, size_t* index, char* buffer, size_t buffer_size) {
size_t start = *index;
size_t len = 0;
// Temporary buffer to hold the number substring
char number_substr[BUFFER_SIZE] = {0};
// Copy the number substring
// The condition needs to handle e/E and exponent parts
while (isdigit(expression[*index]) || expression[*index] == DECIMAL_POINT ||
expression[*index] == DECIMAL_E_CHAR ||
(*index > NUMBER_ZERO && expression[*index - 1] == DECIMAL_E_CHAR &&
(expression[*index] == OPERATOR_PLUS || expression[*index] == OPERATOR_MINUS)
)
) {
if (len >= BUFFER_SIZE - 1) {
// Number too long
return false;
}
number_substr[len++] = expression[*index];
(*index)++;
}
number_substr[len] = END_STRING_CHAR;
// Parse the number using strtod for better error handling
double value;
char* endptr;
value = strtod(number_substr, &endptr);
if (endptr == number_substr) {
// Conversion failed
return false;
}
// Convert the double value back to string in standard decimal format with sufficient precision
// %.10f will give 10 decimal places; adjust as needed
int written = snprintf(buffer, buffer_size, "%.10f", value);
if (written < 0 || (size_t)written >= buffer_size) {
return false;
}
// Remove trailing zeros
remove_trailing_zeros(buffer);
// Replace the original number substring with the standard decimal string
size_t number_len = len;
size_t decimal_len = strlen(buffer);
if (decimal_len > number_len) {
// Shift the remaining part of the expression to the right
size_t shift = decimal_len - number_len;
size_t expr_len = strlen(expression);
if (expr_len + shift >= BUFFER_SIZE) {
// Not enough space to shift
return false;
}
memmove(&expression[start + decimal_len], &expression[start + number_len],
expr_len - start - number_len + 1); // +1 to include null terminator
} else if (decimal_len < number_len) {
// Shift the remaining part of the expression to the left
memmove(&expression[start + decimal_len], &expression[start + number_len],
strlen(&expression[start + number_len]) + 1); // +1 to include null terminator
}
// Copy the standard decimal string into the expression
memcpy(&expression[start], buffer, decimal_len);
// Update the index to point after the new number
*index = start + decimal_len;
return true;
}
bool check_number(char* expression, size_t* index) {
if (expression == NULL || index == NULL) {
return false;
}
size_t original_index = *index;
bool has_digits = false;
bool has_decimal_point = false;
// Buffer to hold the converted number
char converted_number[BUFFER_SIZE] = {0};
// Check for optional minus sign
if (expression[*index] == MINUS_SIGN) {
(*index)++;
}
// Check for digits and decimal point
while (isdigit(expression[*index]) || expression[*index] == DECIMAL_POINT) {
if (expression[*index] == DECIMAL_POINT) {
if (has_decimal_point) {
// Two decimal points are invalid
*index = original_index;
return false;
}
has_decimal_point = true;
} else {
has_digits = true;
}
(*index)++;
}
// Check for scientific notation
if (expression[*index] == DECIMAL_E_CHAR) {
(*index)++;
// Check for optional '+' or '-' after 'E'
if (expression[*index] == OPERATOR_PLUS || expression[*index] == OPERATOR_MINUS) {
(*index)++;
}
// There must be at least one digit after 'E'
if (!isdigit(expression[*index])) {
// Invalid exponent
*index = original_index;
return false;
}
while (isdigit(expression[*index])) {
(*index)++;
}
}
// Now, determine if the number is in scientific notation
bool is_scientific = false;
for (size_t i = original_index; i < *index; i++) {
if (expression[i] == DECIMAL_E_CHAR) {
is_scientific = true;
break;
}
}
if (is_scientific) {
// Convert scientific notation to standard decimal
*index = original_index;
if (!convert_scientific_to_decimal(expression, index, converted_number, BUFFER_SIZE)) {
// Conversion failed
*index = original_index;
return false;
}
printf("[DEBUG]: Converted scientific notation to standard decimal: %s\n", converted_number);
} else {
// No conversion needed; ensure that there are digits
if (!has_digits) {
*index = original_index;
return false;
}
}
return true;
}