Skip to content

Commit 24e1874

Browse files
committed
Change directory structure.
1 parent d9bb9ae commit 24e1874

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+301
-0
lines changed

1-1.c renamed to chapter01/1-1.c

File renamed without changes.

1-10.c renamed to chapter01/1-10.c

File renamed without changes.

1-11.c renamed to chapter01/1-11.c

File renamed without changes.

1-12.c renamed to chapter01/1-12.c

File renamed without changes.

1-13.c renamed to chapter01/1-13.c

File renamed without changes.

1-14.c renamed to chapter01/1-14.c

File renamed without changes.

1-15.c renamed to chapter01/1-15.c

File renamed without changes.

1-16.c renamed to chapter01/1-16.c

File renamed without changes.
File renamed without changes.

1-17.c renamed to chapter01/1-17.c

File renamed without changes.

1-18.c renamed to chapter01/1-18.c

File renamed without changes.

1-19.c renamed to chapter01/1-19.c

File renamed without changes.

1-2.c renamed to chapter01/1-2.c

File renamed without changes.

1-20.c renamed to chapter01/1-20.c

File renamed without changes.

1-21.c renamed to chapter01/1-21.c

File renamed without changes.

1-22.c renamed to chapter01/1-22.c

File renamed without changes.

1-23.c renamed to chapter01/1-23.c

File renamed without changes.

1-24.c renamed to chapter01/1-24.c

File renamed without changes.

1-3.c renamed to chapter01/1-3.c

File renamed without changes.

1-4.c renamed to chapter01/1-4.c

File renamed without changes.

1-5.c renamed to chapter01/1-5.c

File renamed without changes.

1-6.c renamed to chapter01/1-6.c

File renamed without changes.

1-7.c renamed to chapter01/1-7.c

File renamed without changes.

1-8.c renamed to chapter01/1-8.c

File renamed without changes.

1-9.c renamed to chapter01/1-9.c

File renamed without changes.

2-1.c renamed to chapter02/2-1.c

File renamed without changes.

2-10.c renamed to chapter02/2-10.c

File renamed without changes.

2-2.c renamed to chapter02/2-2.c

File renamed without changes.

2-3.c renamed to chapter02/2-3.c

File renamed without changes.

2-4.c renamed to chapter02/2-4.c

File renamed without changes.

2-5.c renamed to chapter02/2-5.c

File renamed without changes.

2-6.c renamed to chapter02/2-6.c

File renamed without changes.

2-7.c renamed to chapter02/2-7.c

File renamed without changes.

2-8.c renamed to chapter02/2-8.c

File renamed without changes.

2-9.c renamed to chapter02/2-9.c

File renamed without changes.

3-1.c renamed to chapter03/3-1.c

File renamed without changes.

3-2.c renamed to chapter03/3-2.c

File renamed without changes.

3-3.c renamed to chapter03/3-3.c

File renamed without changes.

3-4.c renamed to chapter03/3-4.c

File renamed without changes.

3-5.c renamed to chapter03/3-5.c

File renamed without changes.

3-6.c renamed to chapter03/3-6.c

File renamed without changes.

4-1.c renamed to chapter04/4-1.c

File renamed without changes.

4-10.c renamed to chapter04/4-10.c

File renamed without changes.

4-11.c renamed to chapter04/4-11.c

File renamed without changes.

4-12.c renamed to chapter04/4-12.c

File renamed without changes.

4-13.c renamed to chapter04/4-13.c

File renamed without changes.

4-14.c renamed to chapter04/4-14.c

File renamed without changes.

4-2.c renamed to chapter04/4-2.c

File renamed without changes.

4-3.c renamed to chapter04/4-3.c

File renamed without changes.

4-4.c renamed to chapter04/4-4.c

File renamed without changes.

4-5.c renamed to chapter04/4-5.c

File renamed without changes.

4-6.c renamed to chapter04/4-6.c

File renamed without changes.

4-7.c renamed to chapter04/4-7.c

File renamed without changes.

4-8.c renamed to chapter04/4-8.c

File renamed without changes.

4-9.c renamed to chapter04/4-9.c

File renamed without changes.

5-1.c renamed to chapter05/5-1.c

File renamed without changes.

5-10.c renamed to chapter05/5-10.c

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

5-13.c renamed to chapter05/5-13.c

File renamed without changes.

5-14.c renamed to chapter05/5-14.c

File renamed without changes.

5-15.c renamed to chapter05/5-15.c

File renamed without changes.

5-16.c renamed to chapter05/5-16.c

File renamed without changes.

chapter05/5-17.c

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
/*
2+
* Exercise 5-17. Add a field-searching capability, so sorting may bee done on
3+
* fields within lines, each field sorted according to an independent set of
4+
* options. (The index for this book was sorted with -df for the index category
5+
* and -n for the page numbers.) 5.12 Complicated Declarations
6+
* By Faisal Saadatmand
7+
*/
8+
9+
#include <stdio.h>
10+
#include <string.h>
11+
#include <stdlib.h>
12+
#include <ctype.h>
13+
14+
#define MAXLINES 5000 /* max #lines to be sorted */
15+
#define MAXLEN 1000 /* max length of any input line */
16+
#define ALLOCSIZE 10000 /* storage for memory[] */
17+
18+
/* functions */
19+
int getLine(char *s, int lim);
20+
int readlines(char *lineptr[], int nlines);
21+
void witelines(char *lineptr[], int nlines);
22+
char *alloc(int);
23+
void qSort(void *lineptr[], int left, int right,
24+
int (*comp)(void *, void *));
25+
int numcmp(char *, char *);
26+
int strCmp(char *s, char *t);
27+
int rnumcmp(char *, char *);
28+
int rstrCmp(char *s, char *t);
29+
int fstrCmp(char *s, char *t);
30+
int frstrCmp(char *s, char *t);
31+
32+
/* Globals */
33+
char *lineptr[MAXLINES]; /* pointers to text lines */
34+
static char allocbuf[ALLOCSIZE]; /* storage for alloc */
35+
static char *allocp = allocbuf; /* next fre position */
36+
37+
/* getLine: get line into s, return length of s -- pointer version */
38+
int getLine(char *s, int lim)
39+
{
40+
int c, len;
41+
42+
len = 0;
43+
while (--lim > 0 && (c = getchar()) != EOF && c != '\n') {
44+
*s++ = c;
45+
++len;
46+
}
47+
if ( c == '\n') {
48+
*s++ = c;
49+
++len;
50+
}
51+
*s = '\0';
52+
return len;
53+
}
54+
55+
/* readlines: read input lines */
56+
int readlines(char *lineptr[], int maxlines)
57+
{
58+
int len, nlines;
59+
char *p, line[MAXLEN];
60+
61+
nlines = 0;
62+
while ((len = getLine(line, MAXLEN)) > 0)
63+
if (nlines >= maxlines || (p = alloc(len)) == NULL)
64+
return -1;
65+
else {
66+
line[len - 1] = '\0'; /* delete newline character */
67+
strcpy(p, line);
68+
lineptr[nlines++] = p;
69+
}
70+
return nlines;
71+
}
72+
73+
/* writelines: write output lines */
74+
void writelines(char *lineptr[], int nlines)
75+
{
76+
while (nlines-- > 0)
77+
printf("%s\n", *lineptr++);
78+
}
79+
80+
/* alloc: allocate memory */
81+
char *alloc(int n) /* return pointer to n characters */
82+
{
83+
if (allocbuf + ALLOCSIZE - allocp >=n) { /* it fits */
84+
allocp += n;
85+
return allocp - n; /* old p */
86+
} else /* not enough room */
87+
return 0;
88+
}
89+
90+
/* qsort: sort v[left]...V[right] into increasing order */
91+
void qSort(void *v[], int left, int right,
92+
int (*comp)(void *, void *))
93+
{
94+
int i, last;
95+
96+
void swap(void *v[], int, int);
97+
98+
if (left >= right) /* do nothing if array contains */
99+
return; /* fewer than two elements */
100+
101+
swap(v, left, (left + right) / 2);
102+
last = left;
103+
for (i = left + 1; i <= right; i++)
104+
if ((*comp)(v[i], v[left]) < 0)
105+
swap(v, ++last, i);
106+
swap(v, left, last);
107+
qSort(v, left, last - 1, comp);
108+
qSort(v, last + 1, right, comp);
109+
}
110+
111+
/* numcmp: compare s1 and s2 numerically */
112+
int numcmp(char *s1, char *s2)
113+
{
114+
double v1, v2;
115+
116+
v1 = atof(s1);
117+
v2 = atof(s2);
118+
119+
if (v1 < v2)
120+
return -1;
121+
else if (v1 > v2)
122+
return 1;
123+
else
124+
return 0;
125+
}
126+
127+
/* strCmp: return < 0 if s < t, 0 if s == t, > 0 if s > t */
128+
int strCmp(char *s, char *t)
129+
{
130+
for ( ; *s == *t; s++, t++)
131+
if (*s == '\0')
132+
return 0;
133+
return *s - *t;
134+
}
135+
136+
/* rnumcmp: compare s1 and s2 numerically */
137+
int rnumcmp(char *s1, char *s2)
138+
{
139+
double v1, v2;
140+
141+
v1 = atof(s1);
142+
v2 = atof(s2);
143+
144+
if (v1 > v2)
145+
return -1;
146+
else if (v1 < v2)
147+
return 1;
148+
else
149+
return 0;
150+
}
151+
152+
/* rstrCmp: same as strCmp but in reverse order */
153+
int rstrCmp(char *s, char *t)
154+
{
155+
for ( ; *s == *t; s++, t++)
156+
if (*s == '\0')
157+
return 0;
158+
return *t - *s;
159+
}
160+
161+
void swap(void *v[], int i, int j)
162+
{
163+
void *temp;
164+
165+
temp = v[i];
166+
v[i] = v[j];
167+
v[j] = temp;
168+
}
169+
170+
/* fstrCmp: same as strCmp but case insensitive */
171+
int fstrCmp(char *s, char *t)
172+
{
173+
for ( ; tolower(*s) == tolower(*t); s++, t++)
174+
if (*s == '\0')
175+
return 0;
176+
return tolower(*s) - tolower(*t);
177+
}
178+
179+
/* frstrCmp: same as rstrCmp but case insensitive */
180+
int frstrCmp(char *s, char *t)
181+
{
182+
for ( ; tolower(*s) == tolower(*t); s++, t++)
183+
if (*s == '\0')
184+
return 0;
185+
return tolower(*t) - tolower(*s);
186+
}
187+
188+
/* dstrCmp: directory order; compares only letters, numbers and blanks. */
189+
int dstrCmp(char *s, char *t)
190+
{
191+
for ( ; ; ++s, ++t) {
192+
while (*s != '\0' && (!isalnum(*s) && !isblank(*s)))
193+
++s;
194+
while (*t != '\0' && (!isalnum(*t) && !isblank(*t)))
195+
++t;
196+
197+
if (*s == *t) {
198+
if (*s == '\0')
199+
return 0;
200+
} else
201+
break;
202+
}
203+
return *s - *t;
204+
}
205+
206+
/* drstrCmp: same as strCmp but in reverse order - directory order version */
207+
int drstrCmp(char *s, char *t)
208+
{
209+
for ( ; ; ++s, ++t) {
210+
while (*s != '\0' && (!isalnum(*s) && !isblank(*s)))
211+
++s;
212+
while (*t != '\0' && (!isalnum(*t) && !isblank(*t)))
213+
++t;
214+
215+
if (*s == *t) {
216+
if (*s == '\0')
217+
return 0;
218+
} else
219+
break;
220+
}
221+
return *t - *s;
222+
}
223+
224+
/* dfstrCmp: same as strCmp but case insensitive - directory order version */
225+
int dfstrCmp(char *s, char *t)
226+
{
227+
for ( ; ; ++s, ++t) {
228+
while (*s != '\0' && (!isalnum(*s) && !isblank(*s)))
229+
++s;
230+
while (*t != '\0' && (!isalnum(*t) && !isblank(*t)))
231+
++t;
232+
233+
if (tolower(*s) == tolower(*t)) {
234+
if (*s == '\0')
235+
return 0;
236+
} else
237+
break;
238+
}
239+
return tolower(*s) - tolower(*t);
240+
}
241+
242+
/* dfrstrCmp: same as rstrCmp but case insensitive - directory order version */
243+
int dfrstrCmp(char *s, char *t)
244+
{
245+
for ( ; ; ++s, ++t) {
246+
while (*s != '\0' && (!isalnum(*s) && !isblank(*s)))
247+
++s;
248+
while (*t != '\0' && (!isalnum(*t) && !isblank(*t)))
249+
++t;
250+
251+
if (tolower(*s) == tolower(*t)) {
252+
if (*s == '\0')
253+
return 0;
254+
} else
255+
break;
256+
}
257+
return tolower(*t) - tolower(*s);
258+
}
259+
260+
/* sort input lines */
261+
int main(int argc, char *argv[])
262+
{
263+
int nlines; /* number of input lines read */
264+
int numeric = 0; /* 1 if numeric sort */
265+
int decreasing = 0; /* 1 if reverse order sort */
266+
int fold = 0; /* 1 if case insensitive sort */
267+
int dirOr = 0; /* 1 if directory order sort */
268+
269+
/* note: no input error checking */
270+
++argv;
271+
while (--argc > 0) {
272+
if (strCmp(argv[0], "-n") == 0)
273+
numeric = 1;
274+
if (strCmp(argv[0], "-r") == 0)
275+
decreasing = 1;
276+
if (strCmp(argv[0], "-f") == 0)
277+
fold = 1;
278+
if (strCmp(argv[0], "-d") == 0)
279+
dirOr = 1;
280+
++argv;
281+
}
282+
283+
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
284+
if (numeric)
285+
qSort((void**) lineptr, 0, nlines - 1,
286+
(int (*)(void*, void*))(decreasing ? rnumcmp : numcmp));
287+
else if (dirOr)
288+
qSort((void**) lineptr, 0, nlines - 1,
289+
(int (*)(void*, void*))(decreasing ? (fold) ? dfrstrCmp :
290+
drstrCmp : (fold) ? dfstrCmp : dstrCmp));
291+
else
292+
qSort((void**) lineptr, 0, nlines - 1,
293+
(int (*)(void*, void*))(decreasing ? (fold) ? frstrCmp :
294+
rstrCmp : (fold) ? fstrCmp : strCmp));
295+
writelines(lineptr, nlines);
296+
return 0;
297+
} else {
298+
printf("input too big to sort\n");
299+
return 1;
300+
}
301+
}

5-1a.c renamed to chapter05/5-1a.c

File renamed without changes.

5-2.c renamed to chapter05/5-2.c

File renamed without changes.

5-2a.c renamed to chapter05/5-2a.c

File renamed without changes.

5-3.c renamed to chapter05/5-3.c

File renamed without changes.

5-4.c renamed to chapter05/5-4.c

File renamed without changes.

5-5.c renamed to chapter05/5-5.c

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

5-7.c renamed to chapter05/5-7.c

File renamed without changes.

5-8.c renamed to chapter05/5-8.c

File renamed without changes.

5-9.c renamed to chapter05/5-9.c

File renamed without changes.

0 commit comments

Comments
 (0)