Skip to content

Commit bb46084

Browse files
committed
5.11
1 parent 6b8c8b5 commit bb46084

File tree

7 files changed

+477
-0
lines changed

7 files changed

+477
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define MAXLINES 5000
5+
char *lineptr[MAXLINES];
6+
7+
int readlines(char *lineptr[], int nlines);
8+
void writelines(char *lineptr[], int nlines);
9+
10+
void qsort(void *lineptr[], int left, int right,
11+
int (*comp)(void *, void *));
12+
13+
int numcmp(const char *, const char *);
14+
15+
// gcc 38.pointers-to-functions.c numcmp.c readlines-writelines.c 6.alloc.c
16+
main(int argc, char *argv[])
17+
{
18+
int nlines;
19+
int numeric = 0;
20+
21+
if (argc > 1 && strcmp(argv[1], "-n") == 0)
22+
numeric = 1;
23+
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
24+
qsort((void **) lineptr, 0, nlines-1,
25+
(int (*)(void*, void*))(numeric ? numcmp : strcmp));
26+
writelines(lineptr, nlines);
27+
return 0;
28+
} else {
29+
printf("input too big to sort\n");
30+
return 1;
31+
}
32+
}
33+
34+
void qsort(void *v[], int left, int right,
35+
int (*comp)(void *, void *))
36+
{
37+
int i, last;
38+
void swap(void *v[], int, int);
39+
40+
if (left >= right)
41+
return;
42+
swap(v, left, (left + right)/2);
43+
last = left;
44+
for (i = left+1; i <= right; i++)
45+
if ((*comp)(v[i], v[left]) < 0)
46+
swap(v, ++last, i);
47+
swap(v, left, last);
48+
qsort(v, left, last-1, comp);
49+
qsort(v, last+1, right, comp);
50+
}
51+
52+
void swap(void *v[], int i, int j)
53+
{
54+
void *temp;
55+
temp = v[i];
56+
v[i] = v[j];
57+
v[j] = temp;
58+
}
59+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define MAXLINES 5000
5+
char *lineptr[MAXLINES];
6+
7+
int readlines(char *lineptr[], int nlines);
8+
void writelines(char *lineptr[], int nlines);
9+
10+
void qsort(void *lineptr[], int left, int right,
11+
int reverse, int (*comp)(void *, void *));
12+
13+
int numcmp(const char *, const char *);
14+
15+
// gcc 39.sort-with-r.c numcmp.c readlines-writelines.c 6.alloc.c
16+
main(int argc, char *argv[])
17+
{
18+
char c;
19+
int nlines;
20+
int numeric = 0;
21+
int reverse = 0;
22+
23+
while (--argc > 0 && (*++argv)[0] == '-')
24+
while (c = *++argv[0])
25+
switch(c) {
26+
case 'n':
27+
numeric = 1;
28+
break;
29+
case 'r':
30+
reverse = 1;
31+
break;
32+
default:
33+
printf("find illegal option %c\n", c);
34+
return 1;
35+
}
36+
37+
if (argc != 0) {
38+
printf("Usage: sort -n -r\n");
39+
return 1;
40+
}
41+
42+
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
43+
qsort((void **) lineptr, 0, nlines-1, reverse,
44+
(int (*)(void*, void*))(numeric ? numcmp : strcmp));
45+
writelines(lineptr, nlines);
46+
return 0;
47+
} else {
48+
printf("input too big to sort\n");
49+
return 1;
50+
}
51+
}
52+
53+
void qsort(void *v[], int left, int right,
54+
int reverse, int (*comp)(void *, void *))
55+
{
56+
int i, last;
57+
void swap(void *v[], int, int);
58+
59+
if (left >= right)
60+
return;
61+
swap(v, left, (left + right)/2);
62+
last = left;
63+
for (i = left+1; i <= right; i++) {
64+
if (reverse && ((*comp)(v[i], v[left]) > 0))
65+
swap(v, ++last, i);
66+
else if (!reverse && ((*comp)(v[i], v[left]) < 0))
67+
swap(v, ++last, i);
68+
}
69+
swap(v, left, last);
70+
qsort(v, left, last-1, reverse, comp);
71+
qsort(v, last+1, right, reverse, comp);
72+
}
73+
74+
void swap(void *v[], int i, int j)
75+
{
76+
void *temp;
77+
temp = v[i];
78+
v[i] = v[j];
79+
v[j] = temp;
80+
}
81+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define MAXLEN 10000
5+
6+
#define MAXLINES 5000
7+
char *lineptr[MAXLINES];
8+
9+
int readlines(char *lineptr[], int nlines);
10+
void writelines(char *lineptr[], int nlines);
11+
12+
void qsort(void *lineptr[], int left, int right,
13+
int reverse, int foldcase, int (*comp)(void *, void *));
14+
15+
int numcmp(const char *, const char *);
16+
17+
void strtolower(const char *src, char *dst);
18+
19+
// gcc 40.sort-with-f.c numcmp.c readlines-writelines.c 6.alloc.c case-convert.c
20+
main(int argc, char *argv[])
21+
{
22+
char c;
23+
int nlines;
24+
int numeric = 0;
25+
int reverse = 0;
26+
int foldcase = 0;
27+
28+
while (--argc > 0 && (*++argv)[0] == '-')
29+
while (c = *++argv[0])
30+
switch(c) {
31+
case 'n':
32+
numeric = 1;
33+
break;
34+
case 'r':
35+
reverse = 1;
36+
break;
37+
case 'f':
38+
foldcase = 1;
39+
break;
40+
default:
41+
printf("find illegal option %c\n", c);
42+
return 1;
43+
}
44+
45+
if (argc != 0) {
46+
printf("Usage: sort -n -r\n");
47+
return 1;
48+
}
49+
50+
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
51+
qsort((void **) lineptr, 0, nlines-1, reverse, foldcase,
52+
(int (*)(void*, void*))(numeric ? numcmp : strcmp));
53+
writelines(lineptr, nlines);
54+
return 0;
55+
} else {
56+
printf("input too big to sort\n");
57+
return 1;
58+
}
59+
}
60+
61+
void qsort(void *v[], int left, int right,
62+
int reverse, int foldcase, int (*comp)(void *, void *))
63+
{
64+
int i, last;
65+
void swap(void *v[], int, int);
66+
char s1[MAXLEN], s2[MAXLEN];
67+
68+
if (left >= right)
69+
return;
70+
swap(v, left, (left + right)/2);
71+
last = left;
72+
for (i = left+1; i <= right; i++) {
73+
char *vi;
74+
char *vleft;
75+
76+
if (foldcase) {
77+
strtolower(v[i], s1);
78+
vi = s1;
79+
strtolower(v[left], s2);
80+
vleft = s2;
81+
} else {
82+
vi = v[i];
83+
vleft = v[left];
84+
}
85+
86+
if (reverse && ((*comp)(vi, vleft) > 0))
87+
swap(v, ++last, i);
88+
else if (!reverse && ((*comp)(vi, vleft) < 0))
89+
swap(v, ++last, i);
90+
}
91+
swap(v, left, last);
92+
qsort(v, left, last-1, reverse, foldcase, comp);
93+
qsort(v, last+1, right, reverse, foldcase, comp);
94+
}
95+
96+
void swap(void *v[], int i, int j)
97+
{
98+
void *temp;
99+
temp = v[i];
100+
v[i] = v[j];
101+
v[j] = temp;
102+
}
103+

0 commit comments

Comments
 (0)