-
Notifications
You must be signed in to change notification settings - Fork 3
/
book_5-16.c
196 lines (154 loc) · 4.61 KB
/
book_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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_STRING_LENGTH 100
#define ALLOC_SIZE 10000
#define MAXLINES 5000
#define MAXLEN 1000
void create_dir(char str[], char temp[]) {
int temp_index = 0;
for (int index = 0; str[index] != '\0'; index++)
if (isspace(str[index]) || isdigit(str[index]) || isalpha(str[index]))
temp[temp_index++] = str[index];
temp[temp_index] = '\0';
}
int getnumber(char str[], int index) {
for ( ; str[index] != '\0' && !isdigit(str[index]); index++);
int number = 0;
while (isdigit(str[index]))
number = 10 * number + (str[index++] - '0');
return number;
}
int getline(char s[], int lim) {
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
char *alloc(int n) {
static char allocbuf[ALLOC_SIZE];
static char *allocp = allocbuf;
if (allocbuf + ALLOC_SIZE - allocp >= n) {
allocp += n;
return allocp - n;
} else
return 0;
}
int readlines(char *lineptr[], int maxlines) {
int len, nlines;
char *p, line[MAXLEN];
nlines = 0;
while ((len = getline(line, MAXLEN)) > 0)
if (nlines >= maxlines || (p = alloc(len)) == NULL)
return -1;
else {
line[len - 1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
return nlines;
}
void writelines(char *lineptr[], int nlines) {
printf("\n");
for (int index = 0; index < nlines; index++)
printf("%s\n", lineptr[index]);
}
int numcmp(char *str_1, char *str_2) {
if (atof(str_1) < atof(str_2))
return -1;
else if (atof(str_1) > atof(str_2))
return 1;
else
return 0;
}
void swap(void *v[], int i, int j) {
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
void create_lower(char *str, char *lower) {
int index;
for (index = 0; index < strlen(str); index++) {
if (isupper(str[index]))
lower[index] = str[index] + 32;
else
lower[index] = str[index];
}
lower[++index] = '\0';
}
int foldcmp(char *str_1, char *str_2) {
char fold_1[MAX_STRING_LENGTH];
char fold_2[MAX_STRING_LENGTH];
create_lower(str_1, fold_1);
create_lower(str_2, fold_2);
if (strcmp(fold_1, fold_2) < 0)
return -1;
else if (strcmp(fold_1, fold_2) > 0)
return 1;
else
return 0;
}
int dircmp(char *str_1, char *str_2) {
char dir_1[MAX_STRING_LENGTH];
char dir_2[MAX_STRING_LENGTH];
create_dir(str_1, dir_1);
create_dir(str_2, dir_2);
if (strcmp(dir_1, dir_2) < 0)
return -1;
else if (strcmp(dir_1, dir_2) > 0)
return 1;
else
return 0;
}
void qqsort(void *v[], int left, int right, int (*comp)(void *, void *)) {
int last;
if (left >= right)
return;
swap(v, left, (left + right) / 2);
last = left;
for (int index = left + 1; index <= right; index++)
if ((*comp)(v[index], v[left]) < 0)
swap(v, ++last, index);
swap(v, left, last);
qqsort(v, left, last - 1, comp);
qqsort(v, last + 1, right, comp);
}
int main(int argc, char *argv[]) {
char *lineptr[MAXLINES];
int nlines = readlines(lineptr, MAXLINES);
if (argc == 1)
qqsort((void **) lineptr, 0, nlines - 1, (int (*)(void *, void *))(strcmp));
else {
int reverse = 0;
for (int arg_index = 1; arg_index < argc; arg_index++) {
if (*argv[arg_index] == '-') {
switch(*(argv[arg_index] + 1)) {
case 'n':
qqsort((void **) lineptr, 0, nlines - 1, (int (*)(void *, void *))(numcmp));
break;
case 'f':
qqsort((void **) lineptr, 0, nlines - 1, (int (*)(void *, void *))(foldcmp));
break;
case 'd':
qqsort((void **) lineptr, 0, nlines - 1, (int (*)(void *, void *))(dircmp));
break;
case 'r':
reverse = 1;
break;
default:
printf("INVALID ARGUMENT : %s", argv[arg_index]);
}
}
}
if (reverse)
for (int reverse_index = 0; reverse_index < nlines / 2; reverse_index++)
swap((void **) lineptr, reverse_index, nlines - reverse_index - 1);
}
writelines(lineptr, nlines);
return 0;
}