forked from Heatwave/The-C-Programming-Language-2nd-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40.sort-with-f.c
103 lines (87 loc) · 2 KB
/
40.sort-with-f.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
#include <stdio.h>
#include <string.h>
#define MAXLEN 10000
#define MAXLINES 5000
char *lineptr[MAXLINES];
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(void *lineptr[], int left, int right,
int reverse, int foldcase, int (*comp)(void *, void *));
int numcmp(const char *, const char *);
void strtolower(const char *src, char *dst);
// gcc 40.sort-with-f.c numcmp.c readlines-writelines.c 6.alloc.c case-convert.c
main(int argc, char *argv[])
{
char c;
int nlines;
int numeric = 0;
int reverse = 0;
int foldcase = 0;
while (--argc > 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch(c) {
case 'n':
numeric = 1;
break;
case 'r':
reverse = 1;
break;
case 'f':
foldcase = 1;
break;
default:
printf("find illegal option %c\n", c);
return 1;
}
if (argc != 0) {
printf("Usage: sort -n -r\n");
return 1;
}
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort((void **) lineptr, 0, nlines-1, reverse, foldcase,
(int (*)(void*, void*))(numeric ? numcmp : strcmp));
writelines(lineptr, nlines);
return 0;
} else {
printf("input too big to sort\n");
return 1;
}
}
void qsort(void *v[], int left, int right,
int reverse, int foldcase, int (*comp)(void *, void *))
{
int i, last;
void swap(void *v[], int, int);
char s1[MAXLEN], s2[MAXLEN];
if (left >= right)
return;
swap(v, left, (left + right)/2);
last = left;
for (i = left+1; i <= right; i++) {
char *vi;
char *vleft;
if (foldcase) {
strtolower(v[i], s1);
vi = s1;
strtolower(v[left], s2);
vleft = s2;
} else {
vi = v[i];
vleft = v[left];
}
if (reverse && ((*comp)(vi, vleft) > 0))
swap(v, ++last, i);
else if (!reverse && ((*comp)(vi, vleft) < 0))
swap(v, ++last, i);
}
swap(v, left, last);
qsort(v, left, last-1, reverse, foldcase, comp);
qsort(v, last+1, right, reverse, foldcase, comp);
}
void swap(void *v[], int i, int j)
{
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}