Skip to content

Commit

Permalink
exercise 4.04-4.06
Browse files Browse the repository at this point in the history
  • Loading branch information
jlzhjp committed Dec 19, 2024
1 parent 2a9b1cf commit 8b841d3
Show file tree
Hide file tree
Showing 3 changed files with 395 additions and 0 deletions.
56 changes: 56 additions & 0 deletions chapter_4/exercise_4_04.c
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;
}
138 changes: 138 additions & 0 deletions chapter_4/exercise_4_05.c
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;
}
}
Loading

0 comments on commit 8b841d3

Please sign in to comment.