This commit is contained in:
asterisk 2022-07-14 20:26:23 +02:00
parent 32237d4c72
commit 99595eca13
11 changed files with 168 additions and 0 deletions

BIN
K&R/05/dcl/a.out Executable file

Binary file not shown.

99
K&R/05/dcl/dcl.c Normal file
View File

@ -0,0 +1,99 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXTOKEN 100
enum { NAME, PARENS, BRACKETS };
void dcl(void);
void dirdcl(void);
int gettoken(void);
int tokentype; /* type of last token */
char token[MAXTOKEN]; /* last token string */
char name[MAXTOKEN]; /* identifier name */
char datatype[MAXTOKEN]; /* data type = char, int, etc. */
char out[1000];
/* dcl: parse a declarator */
void dcl(void)
{
int ns;
for (ns = 0; gettoken() == '*'; ) /* count *s */
ns++;
dirdcl();
while (ns-- > 0)
strcat(out, " pointer to");
}
/* dir-dcl: pasrse a direct declarator */
void dirdcl(void)
{
int type;
if (tokentype == '(') {
dcl();
if (tokentype != ')') {
printf("error: missing )\n");
}
} else if (tokentype == NAME )
strcpy(name, token);
else
printf("error: expected name or dcl\n ");
while ((type=gettoken()) == PARENS || type == BRACKETS)
if (type == PARENS)
strcat(out, "function returning");
else {
strcat(out, " array");
strcat(out, token);
strcat(out, " of");
}
}
int main()
{
while (gettoken() != EOF){
strcpy(datatype, token);
out[0] = '\0';
dcl();
if (tokentype != '\n')
printf("syntax error\n");
printf("%s: %s %s\n", name, out, datatype);
}
return 0;
}
int gettoken(void)
{
int c, getch(void);
void ungetch(int);
char *p = token;
while ((c = getch()) == ' ' || c == '\t')
;
if (c == '(') {
if ((c = getch()) == ')') {
strcpy(token, "()");
return tokentype = PARENS;
} else {
ungetch(c);
return tokentype = '(';
}
} else if (c == '[') {
for (*p++ = c; (*p++ = getch()) != ']'; )
;
*p = '\0';
return tokentype = BRACKETS;
} else if (isalpha(c)) {
for (*p++ = c; isalnum(c = getch()); )
*p++ = c;
*p = '\0';
ungetch(c);
return tokentype = NAME;
} else
return tokentype = c;
}

20
K&R/05/dcl/getch.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0; /* pointer to next empty position */
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if(bufp >= BUFSIZE)
printf("ungetch: too many charachters\n");
else
buf[bufp++] = c;
}

49
K&R/05/misc/sort.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <string.h>
#define MAXLINES 5000; /* max lines to be sorted */
char *lineptr[MAXLINES]; /* pointers to text lines */
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *));
int numcmp(char *, char *);
/* sort input lines */
int main(int argc, char *argv[])
{
int nlines;
int numeric = 0;
if (argc > 0 && strcmp(argv[i], "-n") == 0)
numeric = 1;
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort((void **) lineptr, 0, nlines-1, (int (*)(void*, void*))(numeric ? numcmp : strcmp));
writelines(lineptr, nlines);
return 0;
} else {
printf("input too big to sort\n");
return 1;
}
return 0;
}
void qsort(char *v[], int left, int right)
{
int i, last;
void swap(char *v[], int i, int j);
if (left >= right)
return;
swap(v, left, (left + right)/2);
for (i = left+1; i <= right; i++)
if (strcmp(v[i], v[left]) < 0)
swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}