-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex5_14.c
113 lines (93 loc) · 2.08 KB
/
ex5_14.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
// lots of non-category 0 stuff here
// but they're essentially doing the same thing
// are safer and/or easier
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINES 1000
#define MAXLEN 100
int readlines(char *lineptr[], int maxlines)
{
int nlines = 0;
char buf[MAXLEN], *sp;
while (fgets(buf, MAXLEN, stdin)) {
if ((nlines >= maxlines) || ((sp = strdup(buf)) == NULL))
return -1;
else
lineptr[nlines++] = sp;
}
return nlines;
}
void writelines(char *lineptr[], int nlines)
{
int i;
for (i = 0; i < nlines; ++i) {
printf("%s", lineptr[i]);
free(lineptr[i]);
}
}
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_strcmp(char *s, char *t)
{
int i;
for (i = 0; s[i] == t[i]; i++) {
if (s[i] == '\0') {
return 0;
}
}
return s[i] - t[i];
}
void swap(void *v[], int p1, int p2)
{
void *temp;
temp = v[p1];
v[p1] = v[p2];
v[p2] = temp;
}
void my_qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *), int reverse)
{
int i, last;
if (left >= right)
return;
swap(lineptr, left, (left + right) / 2);
last = left;
for (i = left + 1; i <= right; i++) {
if ((reverse * (*comp)(lineptr[i], lineptr[left])) < 0) {
swap(lineptr, ++last, i);
}
}
swap(lineptr, last, left);
my_qsort(lineptr, left, last-1, comp, reverse);
my_qsort(lineptr, last+1, right, comp, reverse);
}
int main(int argc, char *argv[])
{
int nlines, numeric, reverse, i;
char *lineptr[MAXLINES];
numeric = reverse = 0;
if (argc > 1)
for (i = 1; i < argc; ++i)
if (my_strcmp(argv[i], "-nr") == 0)
numeric = reverse = 1;
else if (my_strcmp(argv[i], "-n") == 0)
numeric = 1;
else if (my_strcmp(argv[i], "-r") == 0)
reverse = 1;
nlines = readlines(lineptr, MAXLINES);
my_qsort((void **) lineptr, 0, nlines-1,
(int (*) (void *, void *)) (numeric ? numcmp : my_strcmp),
(reverse ? -1 : 1));
writelines(lineptr, nlines);
return 0;
}