-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.c
More file actions
183 lines (161 loc) · 4.56 KB
/
1.c
File metadata and controls
183 lines (161 loc) · 4.56 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
#include <stdio.h>
#include <stdlib.h>
#include "display.h"
#include <time.h>
#include "Parser.h"
#include "Graph.h"
#include "string.h"
#include <sys/time.h>
#include <ctype.h>
int NUM_CELLS;
extern int hasCycle;
extern int invalidRange;
Formula *formulaArray;
int is_number(const char *str)
{
if (*str == '\0')
return 0;
while (*str)
{
if (!isdigit((unsigned char)*str))
return 0;
str++;
}
return 1;
}
int main(int argc, char *argv[])
{
struct timeval start, end;
gettimeofday(&start, NULL);
if (argc != 3)
{
printf("Usage: %s <rows> <columns>\n", argv[0]);
return 1;
}
if (!is_number(argv[1]) || !is_number(argv[2]))
{
printf("Usage: %s <rows> <columns>\n", argv[0]);
return 1;
}
int R = atoi(argv[1]);
int C = atoi(argv[2]);
NUM_CELLS = R * C;
Graph *graph = CreateGraph();
formulaArray = malloc(NUM_CELLS * sizeof(Formula));
int *arr = (int *)calloc(R * C, sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1;
}
int currx = 0;
int curry = 0;
int output_disabled = 0; // Flag to track output state
// Initial display only if output is enabled
if (!output_disabled)
{
printer(currx, curry, arr, C, R);
}
// Print timing and status always
gettimeofday(&end, NULL);
double time_taken = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
printf("[%.6f] (ok) ", time_taken);
char *a = (char *)malloc(200 * sizeof(char));
if (a == NULL)
{
printf("Memory allocation failed\n");
free(arr);
return 1;
}
while (1)
{
// Always show prompt regardless of output state
printf("> ");
if (fgets(a, 100, stdin) == NULL)
{
// Handle the error, e.g., clear the buffer or print an error message
fprintf(stderr, "Error reading input\n");
return 1; // Or any appropriate error handling
}
a[strcspn(a, "\n")] = '\0';
if (a[0] == 'q' && a[1] == '\0')
{
break;
}
gettimeofday(&start, NULL);
int status = 1;
// Handle output control commands
if (strcmp(a, "disable_output") == 0)
{
output_disabled = 1;
gettimeofday(&end, NULL);
double time_taken = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
printf("[%.6f] (ok) ", time_taken);
continue;
}
else if (strcmp(a, "enable_output") == 0)
{
output_disabled = 0;
gettimeofday(&end, NULL);
double time_taken = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
printer(currx, curry, arr, C, R);
printf("[%.6f] (ok) ", time_taken);
continue;
}
// Process commands regardless of output state
if (a[0] == 'w' || a[0] == 'a' || (a[0] == 's' && a[1] == '\0') || (a[0] == 'd' && a[1] == '\0'))
{
int checker = scroller(a, arr, &currx, &curry, C, R, graph);
if (checker == -1)
{
status = -1;
}
}
else if (strncmp(a, "scroll_to ", 10) == 0)
{
int checker = scroller(a, arr, &currx, &curry, C, R, graph);
if (checker == -1)
{
status = -1;
}
}
else
{
status = parser(a, C, R, arr, graph, formulaArray);
}
gettimeofday(&end, NULL);
double time_taken = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
// Only display spreadsheet if output is not disabled
if (!output_disabled)
{
printer(currx, curry, arr, C, R);
}
// Always print time & status regardless of output setting
if (status > 0)
{
printf("[%.6f] (ok) ", time_taken);
}
else
{
if (hasCycle)
{
printf("[%.6f] (Circular dependency detected) ", time_taken);
hasCycle = 0;
}
else if (invalidRange)
{
printf("[%.6f] (Invalid range) ", time_taken);
invalidRange = 0;
}
else
{
printf("[%.6f] (unrecognized command) ", time_taken);
}
}
}
free(a);
free(arr);
free(formulaArray);
FreeGraph(graph);
return 0;
}