Skip to content

Commit 3fa77b3

Browse files
committed
added Exercise 5-15
1 parent 29ee32b commit 3fa77b3

File tree

7 files changed

+211
-1
lines changed

7 files changed

+211
-1
lines changed

Chapter-5/Exercise 5-14/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ int main(int argc, char **argv) {
3535
}
3636
}
3737
if (argc != 0) {
38-
printf("Usage: %s -n -r\n", name);
38+
printf("Usage: %s -n -r\n"
39+
"n: numeric sort\n"
40+
"r: reverse sort\n", name);
3941
return 0;
4042
}
4143
//return 0;

Chapter-5/Exercise 5-15/alloc.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define ALLOCSIZE 10000 /* size of available space */
2+
3+
static char allocbuf[ALLOCSIZE]; /* storage for alloc */
4+
static char *allocp = allocbuf; /* next free position */
5+
6+
char *alloc(int n) /* return pointer to n characters */{
7+
/* end of the allocbuf - allocp should be >= n for it to fit */
8+
if (allocbuf + ALLOCSIZE - allocp >= n) { /* it fits */
9+
allocp += n;
10+
return allocp-n; /* old pointer */
11+
} else
12+
return 0;
13+
}
14+
15+
void afree(char *p) /* free storage pointed to by p */ {
16+
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
17+
allocp = p;
18+
}

Chapter-5/Exercise 5-15/alloc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef ALLOC_H
2+
#define ALLOC_H
3+
4+
char *alloc(int n);
5+
6+
void afree(char *p);
7+
8+
#endif

Chapter-5/Exercise 5-15/main.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include "my_io.h"
5+
#include "alloc.h"
6+
7+
#define MAXLINES 5000
8+
char *lineptr[MAXLINES];
9+
10+
void _qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *), int order);
11+
12+
int numcmp(char *, char *);
13+
int strcmpi(char *, char *);
14+
15+
int main(int argc, char **argv) {
16+
int numeric = 0;
17+
//int reverse = -1;
18+
int order = 1;
19+
int ignore_case = 0;
20+
char *name = *argv;
21+
char c;
22+
/*if (argc > 1 && strcmp(*++argv, "-n") == 0)
23+
numeric = 1;*/
24+
while (--argc > 0 && **++argv == '-') {
25+
while (c = *++(*argv))
26+
switch(c) {
27+
case 'n':
28+
numeric = 1;
29+
break;
30+
case 'r':
31+
order *= -1;
32+
break;
33+
case 'f':
34+
ignore_case = 1;
35+
break;
36+
default:
37+
printf("%s: illegal character %c\n", name, c);
38+
argc = -1;
39+
break;
40+
}
41+
}
42+
if (argc != 0) {
43+
printf("Usage: %s -n -r\n"
44+
"n: numeric sort\n"
45+
"r: reverse sort\n"
46+
"f : fold (ignore case)\n", name);
47+
return 0;
48+
}
49+
//return 0;
50+
int nlines;
51+
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
52+
_qsort((void **) lineptr, 0, nlines-1, (int (*)(void*, void*))(numeric ? numcmp : ((ignore_case) ? strcmpi : strcmp)), order);
53+
writelines(lineptr, nlines);
54+
return 0;
55+
} else {
56+
printf("input too big to sort\n");
57+
return 1;
58+
}
59+
return 0;
60+
}
61+
62+
/* qsort: sort v[left]...v[right] into increasing order */
63+
void _qsort(void *v[], int left, int right, int (*comp)(void *, void *), int order) {
64+
int i, last;
65+
void swap(void *v[], int, int);
66+
67+
if (left >= right) return;
68+
swap(v, left, (left + right) / 2);
69+
last = left;
70+
for (i = left + 1 ; i <= right ; i++)
71+
if ((order)*(*comp)(v[i], v[left]) < 0)
72+
swap(v, ++last, i);
73+
swap(v, left, last);
74+
_qsort(v, left, last - 1, comp, order);
75+
_qsort(v, last + 1, right, comp, order);
76+
}
77+
78+
/* numcmp: compare s1 and s2 numerically */
79+
int numcmp(char *s1, char *s2) {
80+
double v1, v2;
81+
82+
v1 = atof(s1);
83+
v2 = atof(s2);
84+
if (v1 < v2)
85+
return -1;
86+
else if (v1 > v2)
87+
return 1;
88+
else
89+
return 0;
90+
}
91+
92+
int strcmpi(char *s1, char *s2) {
93+
void strupr(char *);
94+
95+
char v1[1024], v2[1024];
96+
strcpy(v1, s1);
97+
strupr(v1);
98+
strcpy(v2, s2);
99+
strupr(v2);
100+
101+
return strcmp(v1, v2);
102+
}
103+
104+
void strupr(char *s) {
105+
while(*s != '\0') {
106+
if (*s >= 'a' && *s <= 'z')
107+
*s = 'A' + (*s-'a');
108+
s++;
109+
}
110+
}
111+
112+
void swap(void *v[], int i, int j) {
113+
void *temp;
114+
temp = v[i], v[i] = v[j], v[j] = temp;
115+
}

Chapter-5/Exercise 5-15/my_io.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include "alloc.h"
4+
#define MAXLEN 1000 /* max length of any input line */
5+
6+
int _getline(char str[], int lim) {
7+
int c, i = 0;
8+
while (--lim && (c = getchar()) != EOF && c != '\n')
9+
str[i++] = c;
10+
if (c == '\n') str[i++] = c;
11+
str[i] = '\0';
12+
13+
return i;
14+
}
15+
16+
/* readlines: read input lines */
17+
int readlines(char *lineptr[], int maxlines) {
18+
int len, nlines;
19+
char *p, line[MAXLEN];
20+
21+
nlines = 0;
22+
while ((len = _getline(line, MAXLEN)) > 0)
23+
if (nlines >= maxlines || (p = alloc(len)) == NULL)
24+
return -1;
25+
else {
26+
line[len-1] = '\0'; /* delete newline */
27+
strcpy(p, line);
28+
lineptr[nlines++] = p;
29+
}
30+
return nlines;
31+
}
32+
33+
/* _readlines: doesn't use the alloc for memory allocation */
34+
int _readlines(char *lineptr[], int maxlines, char *linestore) {
35+
int len, nlines;
36+
char line[MAXLEN];
37+
while ((len = _getline(line, MAXLEN)) > 0) {
38+
if (nlines >= maxlines)
39+
return -1;
40+
else {
41+
line[len-1] = '\0'; /* delete the newline */
42+
strcpy(linestore, line);
43+
lineptr[nlines++] = linestore;
44+
}
45+
linestore += len;
46+
}
47+
return nlines;
48+
}
49+
50+
/* writelines: write output lines */
51+
void writelines(char *lineptr[], int nlines) {
52+
int i;
53+
54+
for (i = 0 ; i < nlines ; i++)
55+
printf("%s\n", lineptr[i]);
56+
}

Chapter-5/Exercise 5-15/my_io.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef MY_IO_H
2+
#define MY_IO_H
3+
4+
int _getline(char str[], int limit);
5+
6+
int _readlines(char *lineptr[], int maxlines, char *linestore);
7+
int readlines(char *lineptr[], int maxlines);
8+
9+
void writelines(char *lineptr[], int nlines);
10+
11+
#endif

Chapter-5/Exercise 5-15/sort

13.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)