-
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
Showing
3 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
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,56 @@ | ||
#include <stdio.h> | ||
|
||
void push(double); | ||
double pop(void); | ||
double peek(void); | ||
void clear(); | ||
|
||
int main() | ||
{ | ||
push(1.0); | ||
push(2.0); | ||
push(3.0); | ||
printf("peek: %f\n", peek()); | ||
clear(); | ||
pop(); | ||
} | ||
|
||
#define MAXVAL 100 | ||
int sp = 0; | ||
double val[MAXVAL]; | ||
|
||
/* push: 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; | ||
} | ||
} | ||
|
||
double peek(void) | ||
{ | ||
if (sp > 0) { | ||
return val[sp - 1]; | ||
} else { | ||
printf("error: stack empty\n"); | ||
return 0.0; | ||
} | ||
} | ||
|
||
void clear(void) | ||
{ | ||
sp = 0; | ||
} |
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,138 @@ | ||
#include <ctype.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <math.h> | ||
|
||
#define MAXOP 100 /* max size of operand or operator */ | ||
#define NUMBER '0' /* signal that a number was found */ | ||
|
||
int getop(char[]); | ||
void push(double); | ||
double pop(void); | ||
|
||
int getch(void); | ||
void ungetch(int); | ||
|
||
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()); | ||
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 's': | ||
push(sin(pop())); | ||
break; | ||
case 'e': | ||
push(exp(pop())); | ||
break; | ||
case 'p': | ||
op2 = pop(); | ||
push(pow(pop(), op2)); | ||
break; | ||
case '\n': | ||
printf("\t%.8g\n", pop()); | ||
break; | ||
default: | ||
printf("error: unkown command %s\n", s); | ||
break; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
#define MAXVAL 100 | ||
int sp = 0; | ||
double val[MAXVAL]; | ||
|
||
/* push: 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; | ||
} | ||
} | ||
|
||
int getop(char s[]) | ||
{ | ||
int i, c; | ||
while ((s[0] = c = getch()) == ' ' || c == '\t') { | ||
} | ||
|
||
s[1] = '\0'; | ||
if (!isdigit(c) && c != '.') | ||
return c; | ||
|
||
i = 0; | ||
|
||
if (isdigit(c)) { | ||
while (isdigit(s[++i] = c = getch())) { | ||
} | ||
} | ||
if (c == '.') { | ||
while (isdigit(s[++i] = c = getch())) { | ||
} | ||
} | ||
|
||
s[i] = '\0'; | ||
|
||
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) | ||
{ | ||
return (bufp > 0) ? buf[--bufp] : getchar(); | ||
} | ||
|
||
void ungetch(int c) /* push character back on input */ | ||
{ | ||
if (bufp >= BUFSIZE) { | ||
printf("ungetch: too many characters\n"); | ||
} else { | ||
buf[bufp++] = c; | ||
} | ||
} |
Oops, something went wrong.