-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrcmp.c
190 lines (165 loc) · 4.95 KB
/
strcmp.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
/* using numeric value based string comparison to sort input lines
*
* Exercise 5-14. Modify the sort program to handle a -r flag, which indicates
* sorting in reverse (decreasing) order. Be sure that -r works with -n.
*
* Exercise 5-15. Add the option -f to fold upper and lower case together, so
* that case distinctions are not made during sorting; for example, a and A
* compare equal.
*
* Exercise 5-16. Add the -d (‘‘directory order’’) option, which makes
* comparisons only on letters, numbers and blanks. Make sure it works in
* conjunction with -f.
*
* Exercise 5-17. Add a field-searching capability, so sorting may bee done on
* fields within lines, each field sorted according to an independent set of
* options. (The index for this book was sorted with -df for the index category
* and -n for the page numbers.) */
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "ctype.h"
#define MAXLINES 10000
int getline_(char **line);
void *alloc(int n, int size);
void qsort_(void **v, int left, int right,
int (*cmp)(void *, void *, char, char), char fold, char dir);
void swap(void **v, int p0, int p1);
int numcmp(const char *s, const char *t, char, char);
int strcmp_(const char *s, const char *t, char fold, char dir);
void showlines(void **v, int nlines, char reverse);
int main(int argc, char *argv[]) {
char *lines[MAXLINES]; // storing input
char numeric = 0; // numeric sort switch
char reverse = 0; // print in reverse
char fold = 0; // fold lowercase and uppercase
char dir = 0; // directory order
int start = 0, stop = 0; // only sort lines between start and stop
const char *usage = "Sort input.\n"
"Usage: strcmp [OPTIONS] [-START] [+STOP]\n"
"OPTIONS:\n"
"-h\n\thelp.\n"
"-n\n\tnumeric sort.\n"
"-r\n\treverse sort.\n"
"-f\n\tfold lowercase and uppercase together.\n"
"-d\n\tdirectory sort (only consider alphabet, digit and blank).\n"
"\n"
"START & STOP:\n"
"\tonly sort lines between start and stop line.\n";
int nlines; // #lines
while (--argc) {
if (**++argv == '-') {
if (isdigit(*++*argv)) {
start = atoi(*argv);
continue;
}
while (**argv)
switch (*(*argv)++) {
case 'n':
numeric = 1;
break;
case 'r':
reverse = 1;
break;
case 'f':
fold = 1;
break;
case 'd':
dir = 1;
break;
case 'h':
printf("%s", usage);
return 0;
default:
printf("error: invalid argument.\n%s", usage);
return 0;
}
} else if (**argv == '+')
stop = atoi(*argv);
}
nlines = 0;
while (getline_(lines + nlines) > 0 && nlines < MAXLINES)
nlines++;
if (nlines == MAXLINES) {
printf("warning: too many input lines.\n");
return 0;
}
qsort_((void **)lines, start, stop ? stop : nlines-1,
(int (*)(void *, void *, char, char))(numeric ? numcmp : strcmp_),
fold, dir);
showlines((void **)lines, nlines, reverse);
return 0;
}
#define MAXLEN 10000
int getline_(char **line) {
static char linebuf[MAXLEN];
char *lineptr;
int i, c;
i = 0;
while (i < MAXLEN && (linebuf[i]=c=getchar()) != '\n' && c != EOF)
i++;
if (i == MAXLEN) {
printf("warning: too long line length.\n");
linebuf[--i] = '\0';
} else
linebuf[i] = '\0';
if ((lineptr = (char *)alloc(i+1, sizeof(char)))) {
strncpy(lineptr, linebuf, i+1);
*line = lineptr;
return i;
}
printf("error: stack used up.\n");
return 0;
}
#define STKSIZE 10000
void *alloc(int n, int size) {
static char stack[STKSIZE];
static char *p = stack;
if (n > 0 && p + n*size - stack < STKSIZE) {
p += n * size;
return p - n * size;
}
printf("error: allocate memory failed.\n");
return NULL;
}
void qsort_(void **v, int left, int right,
int (*cmp)(void *, void *, char, char), char fold, char dir){
if (left >= right)
return;
swap(v, left, (left+right) / 2);
int last = left;
for (int i = left+1; i <= right; i++) {
if ((*cmp)(v[left], v[i], fold, dir) > 0)
swap(v, ++last, i);
}
swap(v, left, last);
qsort_(v, left, last, cmp, fold, dir);
qsort_(v, last+1, right, cmp, fold, dir);
}
void swap(void **v, int p0, int p1) {
void *temp;
temp = v[p0];
v[p0] = v[p1];
v[p1] = temp;
}
int numcmp(const char *s, const char *t, char fold, char dir) {
return atoi(s) - atoi(t);
}
#define isdirchar(c) (isblank(c) && isdigit(c) && isalpha(c))
int strcmp_(const char *s, const char *t, char fold, char dir) {
int i;
i = 0;
while (s[i] != '\0' && t[i] != '\0' &&
(dir ? isdirchar(t[i]) && isdirchar(s[i]) : 1) &&
(fold ? tolower(s[i]) == tolower(t[i]) : s[i] == t[i]))
i++;
return fold ? tolower(s[i]) - tolower(t[i]) : s[i] - t[i];
}
void showlines(void **v, int nlines, char reverse) {
if (reverse)
for (int i = nlines-1; i >= 0; i--)
printf("%s\n", (char *)v[i]);
else
for (int i = 0; i < nlines; i++)
printf("%s\n", (char *)v[i]);
}