-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
141 lines (118 loc) · 4.14 KB
/
Copy pathmain.c
File metadata and controls
141 lines (118 loc) · 4.14 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
#include <stdio.h>
#include <stdlib.h>
#include "parse_input.h"
#include "defs.h"
#include "shuntingyard.h"
#include "postscriptexport.h"
#include "parser_utils.h"
int main(int argc, char* argv[]) {
if (argc < 2 || argc > 4 || argv == NULL) {
return ERROR_ARG_COUNT;
}
// Check argument count
if (!check_arg_count(argc)) {
printf("Usage: %s <function> <output_file> [<limits>]\n", argv[0]);
return ERROR_ARG_COUNT;
}
// Allocate params
input_params_t* params = allocate_params();
if (!params) {
return ERROR_MEMORY_ALLOCATION;
}
// Extract function
if (!extract_function_param(params, argv[1])) {
free_input_params(params);
return ERROR_INVALID_FUNCTION;
}
// Extract output file
if (!extract_output_file_param(params, argv[2])) {
free_input_params(params);
return ERROR_OUTPUT_FILE;
}
// Parse limits if provided
if (argc == 4) {
if (!parse_limits_param(params, argv[3])) {
free_input_params(params);
return ERROR_INVALID_LIMITS;
}
} else {
set_default_limits(params);
}
// Normalize function
if (!normalize_function_param(params)) {
free_input_params(params);
return ERROR_INVALID_FUNCTION;
}
// Validate expression
if (!validate_expression_param(params)) {
free_input_params(params);
return ERROR_INVALID_FUNCTION;
}
// Check limits
if (!check_limits_valid(params)) {
free_input_params(params);
return ERROR_INVALID_LIMITS;
}
// Proceed with the rest of the program
printf("[DEBUG]: All input parameters are valid.\n");
printf("---------------------------------\n");
printf("Parsed input:\n");
printf("Function: %s\n", params->function_str);
printf("Output file: %s\n", params->output_file_str);
printf("X limits: [%lf, %lf]\n", params->x_min, params->x_max);
printf("Y limits: [%lf, %lf]\n", params->y_min, params->y_max);
printf("---------------------------------\n");
TokenQueue token_queue;
init_token_queue(&token_queue);
bool success = parse_expression(params->function_str, &token_queue);
if (!success) {
printf("Unsuccessful expression parsing!\n");
clear_token_queue(&token_queue);
free_input_params(params);
return ERROR_INVALID_FUNCTION;
}
// Calculate the number of points based on x limits and X_STEP_VALUE
int num_points = (int)round((params->x_max - params->x_min) / X_STEP_VALUE);
int real_num_points = 0;
// Dynamically allocate memory for x and y arrays
double* x = (double*)malloc(num_points * sizeof(double));
double* y = (double*)malloc(num_points * sizeof(double));
if (x == NULL || y == NULL) {
perror("Failed to allocate memory");
if (x != NULL) {
free(x);
}
if (y != NULL) {
free(y);
}
free_input_params(params);
clear_token_queue(&token_queue);
return ERROR_MEMORY_ALLOCATION;
}
// Fill x and y arrays
double current_x = params->x_min;
for (int i = 0; i < num_points; i++) {
double result = evaluate_expression(&token_queue, current_x);
if (result >= params->y_min && result <= params->y_max) {
x[real_num_points] = current_x;
y[real_num_points] = result;
++real_num_points;
}
current_x += X_STEP_VALUE;
}
char interval_label[100]; // Buffer for the interval string
// Generate the interval label using params min and max values
snprintf(interval_label, sizeof(interval_label), INTERVAL_STRING_FORMAT,
params->x_min, params->x_max, params->y_min, params->y_max);
// Export data to PostScript file
export_to_postscript(params->output_file_str, x, y, real_num_points,
params->x_min, params->x_max, params->y_min, params->y_max,
params->function_str, interval_label);
// Free allocated memory
free(x);
free(y);
// Cleanup
clear_token_queue(&token_queue);
free_input_params(params);
return SUCCESS;
}