forked from jhpy1024/CProgrammingLanguageExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_5-16.c
220 lines (187 loc) · 4.15 KB
/
exercise_5-16.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
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
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SORT_NUMERICALLY "-n"
#define SORT_REVERSE "-r"
#define FOLD_CASE "-f"
#define DIRECTORY_ORDER "-d"
enum
{
MAX_NUM_LINES = 5000,
BUFFER_SIZE = 10000,
MAX_LINE_LENGTH = 1000
};
static char alloc_buffer[BUFFER_SIZE];
static char* next_free = alloc_buffer;
bool sort_reverse = false;
bool fold_case = false;
bool directory_order = false;
char* alloc(int n)
{
if ((alloc_buffer + BUFFER_SIZE) - next_free >= n)
{
next_free += n;
return next_free - n;
}
else
{
return NULL;
}
}
int get_line(char line[])
{
int current_char;
int index = 0;
while (index < MAX_LINE_LENGTH && (current_char = getchar()) != EOF && current_char != '\n')
{
line[index++] = current_char;
}
if (current_char == '\n')
{
line[index++] = current_char;
}
line[index] = '\0';
return index;
}
int read_lines(char* lines[])
{
char line[MAX_LINE_LENGTH] = { 0 };
char* line_ptr;
int line_length = 0;
int num_lines = 0;
while ((line_length = get_line(line)) > 0)
{
if (num_lines >= MAX_NUM_LINES || (line_ptr = alloc(line_length)) == NULL)
{
return -1;
}
else
{
line[line_length - 1] = '\0';
strcpy(line_ptr, line);
lines[num_lines++] = line_ptr;
}
}
return num_lines;
}
void write_lines(char* lines[], int num_lines)
{
for (int i = 0; i < num_lines; ++i)
{
puts(lines[i]);
}
}
int numeric_cmp(char* a, char* b)
{
double a_val = atof(a);
double b_val = atof(b);
if (a_val < b_val)
{
return (sort_reverse ? 1 : -1);
}
else if (a_val > b_val)
{
return (sort_reverse ? -1 : 1);
}
else
{
return 0;
}
}
bool lexicographically_equal(char a, char b)
{
if (directory_order)
{
/*
* If the directory order flag is set, we only compare numbers, letters, and whitespace so
* consider consider characters that do not meet this criteria to be equal.
*/
if (!isdigit(a) && !isdigit(b) && !isspace(a) && !isspace(b) && !isalpha(a) && !isalpha(b))
{
return true;
}
}
if (fold_case)
{
a = tolower(a);
b = tolower(b);
}
return a == b;
}
int lexicographic_cmp(char* a, char* b)
{
int index;
for (index = 0; lexicographically_equal(a[index], b[index]); ++index)
{
if (a[index] == '\0')
{
return 0;
}
}
return (sort_reverse ? b[index] - a[index] : a[index] - b[index]);
}
void swap(void* v[], int i, int j)
{
void* tmp = v[i];
v[i] = v[j];
v[j] = tmp;
}
void quick_sort(void* v[], int left, int right, int (*cmp)(void*, void*))
{
int index;
int last;
if (left >= right)
{
return;
}
swap(v, left, (left + right) / 2);
last = left;
for (index = left + 1; index <= right; ++index)
{
if ((*cmp)(v[index], v[left]) < 0)
{
swap(v, ++last, index);
}
}
swap(v, left, last);
quick_sort(v, left, last - 1, cmp);
quick_sort(v, last + 1, right, cmp);
}
int main(int argc, char* argv[])
{
int num_lines;
char* lines[MAX_NUM_LINES];
bool sort_numerically = false;
if (argc > 1)
{
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], SORT_NUMERICALLY) == 0)
{
sort_numerically = true;
}
else if (strcmp(argv[i], SORT_REVERSE) == 0)
{
sort_reverse = true;
}
else if (strcmp(argv[i], FOLD_CASE) == 0)
{
fold_case = true;
}
}
}
if ((num_lines = read_lines(lines)) >= 0)
{
quick_sort((void**) lines, 0, num_lines - 1,
(int(*)(void*, void*))(sort_numerically ? numeric_cmp : lexicographic_cmp));
write_lines(lines, num_lines);
return 0;
}
else
{
printf("error: input too large to sort\n");
return 1;
}
}