-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tony Soul
authored and
Tony Soul
committed
Sep 1, 2016
1 parent
a0d4315
commit b809aa6
Showing
15 changed files
with
375 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"configurations": [{ | ||
"name": "Mac", | ||
"includePath": ["/usr/include"], | ||
"browse": { | ||
"limitSymbolsToIncludedHeaders": true, | ||
"databaseFilename": "" | ||
} | ||
}, { | ||
"name": "Linux", | ||
"includePath": ["/usr/include"], | ||
"browse": { | ||
"limitSymbolsToIncludedHeaders": true, | ||
"databaseFilename": "" | ||
} | ||
}, { | ||
"name": "Win32", | ||
"includePath": ["c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include", "C:/MinGW/include"], | ||
"browse": { | ||
"limitSymbolsToIncludedHeaders": true, | ||
"databaseFilename": "" | ||
} | ||
}], | ||
"clang_format": { | ||
"style": "file", | ||
"fallback-style": "LLVM", | ||
"sort-includes": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> /* for atf() */ | ||
|
||
#define MAXOP 100 /* max size of oparand or operator */ | ||
#define NUMBER '0' /* signal that a number was found */ | ||
|
||
int getop(char[]); | ||
void push(double); | ||
double pop(void); | ||
|
||
/* reverse Polish calulator */ | ||
int main() { | ||
int type; | ||
double op2; | ||
char s[MAXOP]; | ||
|
||
while ((type = getop(s)) != EOF) { | ||
switch (type) { | ||
case NUMBER: | ||
push(atof(s)); | ||
break; | ||
case '+': | ||
push(pop() + pop()); | ||
break; | ||
case '*': | ||
push(pop() * pop()); | ||
break; | ||
case '-': | ||
op2 = pop(); | ||
push(pop() - op2); | ||
break; | ||
case '/': | ||
op2 = pop(); | ||
if (op2 != 0.0) | ||
push(pop() / op2); | ||
else { | ||
printf("error: zero divisor\n"); | ||
} | ||
break; | ||
case '\n': | ||
printf("\t%.8g\n", pop()); | ||
break; | ||
default: | ||
printf("error: unknow command %s\n", s); | ||
break; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
#define MAXVAL 100 /* maximum depth of val stack */ | ||
|
||
int sp=0; /* next free stack position */ | ||
double val[MAXVAL]; /* value stack */ | ||
|
||
/* puch: push f onto value stack */ | ||
void push(double f) | ||
{ | ||
if(sp<MAXVAL) | ||
val[sp++]=f; | ||
else | ||
printf("error: stack full, can't push %g\n",f); | ||
} | ||
|
||
/* pop: pop and return top value from stack */ | ||
double pop(void) | ||
{ | ||
if(sp>0) | ||
return val[--sp]; | ||
else{ | ||
printf("error: stack empty\n"); | ||
return 0.0; | ||
} | ||
} | ||
|
||
#include<ctype.h> | ||
|
||
int getch(void); | ||
void ungetch(int); | ||
|
||
/* getop: get next charactor or numeric operand */ | ||
int getop(char s[]) | ||
{ | ||
int i,c; | ||
while((s[0]=ge=getch())==' '||c=='\t') | ||
; | ||
s[1]='\0'; | ||
if(!isdigit(c)&&c!='.') | ||
return c; /* not a number */ | ||
i=0; | ||
if(isdigit(c)) /* collect interger part */ | ||
while(isdigit(s[i++] = c=getch())) | ||
; | ||
if(c=='.') /* collect fraction part */ | ||
while(isdigit(s[i++]=c=getch())) | ||
; | ||
s[i]='\n'; | ||
if(c!=EOF) | ||
ungetch(c); | ||
return NUMBER; | ||
} | ||
|
||
#define BUFSIZE 100 | ||
|
||
char buf[BUFSIZE]; /* buffer for ungetch */ | ||
int bufp=0; /* next free position in buf */ | ||
int getch(void) /* get a (possibly pushed-back) character */ | ||
{ | ||
return (bufp>0)?buf[--bufp]:getchar(); | ||
} | ||
|
||
void ungetch(int c) /* push charactor back on input */ | ||
{ | ||
if(bufp>BUFSIZE) | ||
printf("ungetch: too many characters\n"); | ||
else | ||
buf[bufp]=c; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> /* for atf() */ | ||
|
||
#define MAXOP 100 /* max size of oparand or operator */ | ||
#define NUMBER '0' /* signal that a number was found */ | ||
|
||
int getop(char[]); | ||
void push(double); | ||
double pop(void); | ||
|
||
/* reverse Polish calulator */ | ||
int main() { | ||
int type; | ||
double op2; | ||
char s[MAXOP]; | ||
|
||
while ((type = getop(s)) != EOF) { | ||
switch (type) { | ||
case NUMBER: | ||
push(atof(s)); | ||
break; | ||
case '+': | ||
push(pop() + pop()); | ||
break; | ||
case '*': | ||
push(pop() * pop()); | ||
break; | ||
case '-': | ||
op2 = pop(); | ||
push(pop() - op2); | ||
break; | ||
case '/': | ||
op2 = pop(); | ||
if (op2 != 0.0) | ||
push(pop() / op2); | ||
else { | ||
printf("error: zero divisor\n"); | ||
} | ||
break; | ||
case '\n': | ||
printf("\t%.8g\n", pop()); | ||
break; | ||
default: | ||
printf("error: unknow command %s\n", s); | ||
break; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
#define MAXVAL 100 /* maximum depth of val stack */ | ||
|
||
int sp=0; /* next free stack position */ | ||
double val[MAXVAL]; /* value stack */ | ||
|
||
/* puch: push f onto value stack */ | ||
void push(double f) | ||
{ | ||
if(sp<MAXVAL) | ||
val[sp++]=f; | ||
else | ||
printf("error: stack full, can't push %g\n",f); | ||
} | ||
|
||
/* pop: pop and return top value from stack */ | ||
double pop(void) | ||
{ | ||
if(sp>0) | ||
return val[--sp]; | ||
else{ | ||
printf("error: stack empty\n"); | ||
return 0.0; | ||
} | ||
} | ||
|
||
#include<ctype.h> | ||
|
||
int getch(void); | ||
void ungetch(int); | ||
|
||
/* getop: get next charactor or numeric operand */ | ||
int getop(char s[]) | ||
{ | ||
int i,c; | ||
while((s[0]=ge=getch())==' '||c=='\t') | ||
; | ||
s[1]='\0'; | ||
if(!isdigit(c)&&c!='.') | ||
return c; /* not a number */ | ||
i=0; | ||
if(isdigit(c)) /* collect interger part */ | ||
while(isdigit(s[i++] = c=getch())) | ||
; | ||
if(c=='.') /* collect fraction part */ | ||
while(isdigit(s[i++]=c=getch())) | ||
; | ||
s[i]='\n'; | ||
if(c!=EOF) | ||
ungetch(c); | ||
return NUMBER; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include<stdio.h> | ||
#include <ctype.h> | ||
|
||
int getch(void); | ||
void ungetch(int); | ||
|
||
/* getint: get next integer form input into *pn */ | ||
int getint(int *pn) { | ||
int c, sign; | ||
while (isspace(c = getch())) /* skip white space */ | ||
; | ||
if (isdigit(c) && c != EOF && c != '+' && c != '-') { | ||
ungetch(c); /* it is not a number */ | ||
return 0; | ||
} | ||
|
||
sign = (c == '-') ? -1 : 1; | ||
if (c == '+' || c == '-') | ||
c = getch(); | ||
for (*pn = 0; isdigit(c), c = getch()) | ||
*pn = 10 * *pn + (c - '0'); | ||
*pn *= sign; | ||
if (c != EOF) | ||
ungetch(c); | ||
return c; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#define ALLOCSIZE 10000 /* size of available space */ | ||
|
||
static char allocbuf[ALLOCSIZE]; /* storage for alloc */ | ||
static char *allocp = allocbuf; /* next free position */ | ||
|
||
char *alloc(int n) /* return pointer to n characters */ | ||
{ | ||
if (allocbuf + ALLOCSIZE - allocp >= n) /* it fits */ | ||
{ | ||
allocp += n; | ||
return allocp - n; /* old p */ | ||
} else { /* not enough room */ | ||
return 0; | ||
} | ||
} | ||
|
||
void afree(char *p) { | ||
if (p >= allocbuf && p < allocbuf + ALLOCSIZE) | ||
allocp = p; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* functions */ | ||
/* atof: convert string s to double */ | ||
double atof(char s[]) { | ||
double val, power; | ||
int i, sign; | ||
|
||
for (i = 0; isspace(s[i]); i++) /* skip white space */ | ||
; | ||
sign = (s[i] == '-') ? -1 : 1; | ||
if (s[i] == '+' || s[i] == '-') | ||
i++; | ||
for (val = 0.0; isdigit(s[i]); i++) | ||
val = 10.0 * val + (s[i] - '0'); | ||
if (s[i] == '.') | ||
i++; | ||
for (power = 1.0; isdigit(s[i]); i++) { | ||
val = 10.0 * val + (s[i] - '0'); | ||
power *= 10; | ||
} | ||
return sign * val / power; | ||
} | ||
|
||
/* strlin: return length of string s */ | ||
int strlen(char *s) { | ||
int n; | ||
for (n = 0; *s != '\0'; s++) | ||
n++; | ||
return n; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <ctype.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define MAXWORD 100 | ||
|
||
int getword(char *, int); | ||
int binsearch(char *, struct key *, int); | ||
|
||
/* count C keywords */ | ||
int main() { | ||
int n; | ||
char word[MAXWORD]; | ||
|
||
while (getword(word, MAXWORD) != EOF) | ||
if (isalpha(word[0])) | ||
if ((n = binsearch(word, keytab, NKEYS)) >= 0) | ||
keytab[n].count++; | ||
for (n = 0; n < NKEYS; n++) | ||
if (keytab[n].count > 0) | ||
ptintf("%4d %s\n", keytab[n].count, keytab[n].word); | ||
return 0; | ||
} | ||
|
||
int binsearch(char *word, struct key tab[], int n) { | ||
int cond; | ||
int low, high, mid; | ||
|
||
low = 0; | ||
high = n - 1; | ||
while (low <= high) { | ||
mid = (low + high) / 2; | ||
if ((cond = strcmp(word, tab[mid].word)) < 0) | ||
high = mid - 1; | ||
else if (cond > 0) | ||
low = mid + 1; | ||
else | ||
return mid; | ||
} | ||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include <ctype.h> | ||
#include <stdio.h> | ||
|
||
|
||
int main() /* lower: convert input to lower case */ | ||
{ | ||
int c; | ||
while ((c = getchar()) != EOF) | ||
putchar(tolower(c)); | ||
return 0; | ||
} |
Binary file not shown.