-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex5_15.c
242 lines (202 loc) · 4.32 KB
/
ex5_15.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXLINES 1000
#define LINELEN 100
int readlines(char *lineptr[], int maxlines)
{
char buf[LINELEN], *p;
int nlines = 0;
while (fgets(buf, maxlines, stdin)) {
if ((nlines >= maxlines) || (buf == NULL) || ((p = strdup(buf)) == NULL))
return -1;
else {
lineptr[nlines++] = p;
}
}
return nlines;
}
void writelines(char *lineptr[], int nlines)
{
int i;
for (i = 0; i < nlines; ++i) {
printf("%s", lineptr[i]);
free(lineptr[i]);
}
}
void swap(void *v[], int p1, int p2)
{
int *temp;
temp = v[p1];
v[p1] = v[p2];
v[p2] = temp;
}
void my_qsort(void *v[], int left, int right,
int (*comp)(void *, void*))
{
int last, i;
if (left >= right)
return;
swap(v, left, (left+right)/2);
last = left;
for (i = left+1; i <= right; ++i) {
if ((*comp)(v[i], v[left]) < 0) {
swap(v, ++last, i);
}
}
swap(v, last, left);
my_qsort(v, left, last-1, comp);
my_qsort(v, last+1, right, comp);
}
int my_strcmp(char *s1, char *s2)
{
int i;
while (*s1 == *s2) {
if (*s1 == '\0') {
return 0;
}
s1++;
s2++;
}
return (*s1 - *s2);
}
int numcmp(char *s1, char *s2)
{
double d1, d2;
d1 = strtod(s1, NULL);
d2 = strtod(s2, NULL);
if (d1 < d2)
return -1;
else if (d1 > d2)
return 1;
return 0;
}
int my_strcmpr(char *s1, char *s2)
{
while (*s1 == *s2) {
if (*s1 == '\0') {
return 0;
}
s1++;
s2++;
}
return (*s2 - *s1);
}
int numcmpr(char *s1, char *s2)
{
double d1, d2;
d1 = strtod(s1, NULL);
d2 = strtod(s2, NULL);
if (d1 < d2)
return 1;
else if (d1 > d2)
return -1;
return 0;
}
void fold_case(char *s, char *res)
{
char *c = s;
int i = 0;
while (*c != '\0') {
if (isalpha(*c)) {
res[i++] = toupper(*c);
}
else {
res[i++] = *c;
}
++c;
}
res[i] = '\0';
}
int strcmpf(char *s1, char *s2)
{
char fs1[LINELEN];
char fs2[LINELEN];
fold_case(s1, fs1);
fold_case(s2, fs2);
return my_strcmp(fs1, fs2);
}
int strcmpfr(char *s1, char *s2)
{
char fs1[LINELEN];
char fs2[LINELEN];
fold_case(s1, fs1);
fold_case(s2, fs2);
return my_strcmpr(fs1, fs2);
}
int numcmpf(char *s1, char *s2)
{
char fs1[LINELEN];
char fs2[LINELEN];
fold_case(s1, fs1);
fold_case(s2, fs2);
return numcmp(fs1, fs2);
}
int numcmpfr(char *s1, char *s2)
{
char fs1[LINELEN];
char fs2[LINELEN];
fold_case(s1, fs1);
fold_case(s2, fs2);
return numcmpr(fs1, fs2);
}
int main(int argc, char *argv[])
{
int numeric, reverse, fold, i, nlines;
char *lineptr[MAXLINES];
numeric = reverse = fold = 0;
if (argc > 1) {
for (i = 1; i < argc; ++i) {
if (my_strcmp(argv[i], "-n") == 0) {
numeric = 1;
}
if (my_strcmp(argv[i], "-r") == 0) {
reverse = 1;
}
if (my_strcmp(argv[i], "-nr") == 0) {
numeric = reverse = 1;
}
if (my_strcmp(argv[i], "-f") == 0) {
fold = 1;
}
}
}
nlines = readlines(lineptr, MAXLINES);
if (nlines == -1)
exit(EXIT_FAILURE);
if ((numeric == 1) && (reverse == 0) && (fold == 0)) {
my_qsort((void **) lineptr, 0, nlines-1,
(int (*)(void *, void*)) numcmp);
}
else if ((numeric == 0) && (reverse == 0) && (fold == 0)) {
my_qsort((void **) lineptr, 0, nlines-1,
(int (*)(void *, void *)) my_strcmp);
}
else if ((numeric == 0) && (reverse == 1) && (fold == 0)) {
my_qsort((void **) lineptr, 0, nlines-1,
(int (*)(void *, void *)) my_strcmpr);
}
else if ((numeric == 1) && (reverse == 1) && (fold == 0)) {
my_qsort((void **) lineptr, 0, nlines-1,
(int (*)(void *, void *)) numcmpr);
}
else if ((numeric == 0) && (fold == 1) && (reverse == 0)) {
my_qsort((void **) lineptr, 0, nlines-1,
(int (*)(void *, void *)) strcmpf);
}
else if ((numeric == 0) && (fold == 1) && (reverse == 1)) {
my_qsort((void **) lineptr, 0, nlines-1,
(int (*)(void *, void *)) strcmpfr);
}
else if ((numeric == 1) && (fold == 1) && (reverse == 0)) {
my_qsort((void **) lineptr, 0, nlines-1,
(int (*)(void *, void *)) numcmpf);
}
else if ((numeric == 1) && (fold == 1) && (reverse == 1)) {
my_qsort((void **) lineptr, 0, nlines-1,
(int (*)(void *, void *)) numcmpfr);
}
writelines(lineptr, nlines);
return 0;
}