I'm doing KnR exercise 4-6:
Add access to library functions like sin, exp and pow to the polish calculator.
but I'm having a buffer problem. I'm trying to enter an exponent inside a function I called power (because I think people should choose the power they want).
But I'm getting this:
Code:
current value of the top of the stack: 4.000000000000000Enter a power: You Entered: 0 Inserted value: 1.000000000000000000
heh, that buffer problem.
I know I have to reset the buffer. I tried to reset the val[MAXVAL] unsuccessfully.
What do I have to reset ?
edit:
I tried to reset s (I thought I had to reset it because, after all, s is a char*)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#ifndef NUMBER
#define NUMBER '0'
#endif
#ifndef MAXOP
#define MAXOP 100
#endif
#ifndef PERIOD
#define PERIOD '.'
#endif
#ifndef MAXVAL
#define MAXVAL 100
#endif
#ifndef MAXBUF
#define MAXBUF 100
#endif
int bufp = 0;
char buf[MAXBUF];
int sp = 0;
double val[MAXVAL];
int getch(void);
void ungetch(int);
int getop(char*);
double pop(void);
void push(double);
void peek(void);
void duplicate(void);
void swap(void);
void clear(void);
double power(double);
int isanumber(char* s);
int main(void)
{
int type;
char s[MAXOP];
double op2;
while( (type = getop(s)) != EOF)
{
switch(type)
{
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '*':
push(pop() * pop());
break;
case '\n':
printf("Polish pop: \t%.8g\n", pop());
break;
case 'i':
push(sin(pop()));
break;
case 'e':
push(exp(pop()));
break;
case 'o':
push(power(pop()));
break;
case 'p':
peek();
break;
case 'd':
duplicate();
break;
case 'c':
clear();
break;
case 's':
swap();
break;
case '/':
op2 = pop();
if(op2 > 0.0)
push(pop() / op2);
else
printf("error: zero divisor\n");
break;
case '%':
op2 = pop();
if(op2 > 0.0)
push(fmod(pop(), op2));
else
printf("error: zero divisor\n");
break;
default:
printf("error: unknown command\n");
break;
}
}
return EXIT_SUCCESS;
}
int isanumber(char* s)
{
int i;
for(i = 0; s[i] != '\0'; i++)
if(isalpha(s[i]))
break;
if(s[i] != '\0')
return 0;
return 1;
}
void push(double f)
{
if(sp < MAXVAL)
{
val[sp++] = f;
printf("Inserted value: %f\n", val[sp - 1]);
}
else
printf("push: stack full, can't push %g\n", f);
}
double power(double f)
{
char strnum[MAXVAL];
int pw = 0;
do {
printf("Enter a power: ");
fgets(strnum, sizeof(strnum), stdin);
}while(!isanumber(strnum));
pw = atoi(strnum);
printf("You entered: %d", pw);
return pow(f,pw);
}
void duplicate(void)
{
if(sp < MAXVAL)
{
val[sp++] = pop();
}
else
printf("duplicate: stack full, can't push %f\n", pop());
}
void swap(void)
{
double temp;
if(sp > 0)
{
temp = val[sp];
val[sp] = val[sp - 1];
val[sp - 1] = temp;
}
else {
printf("swap: at least, two values have to be inserted for swapping.\n");
}
}
void clear(void)
{
int i;
for(i = 0; i < MAXVAL; i++)
val[i] = 0.0;
}
double pop(void)
{
if(sp > 0)
return val[--sp];
else
{
printf("pop: stack empty\n");
return 0.0;
}
}
void peek(void)
{
if(sp > 0)
printf("top element: %.2f\n", val[sp]);
else
printf("peek: stack is empty\n");
}
int getop(char* s)
{
int next;
int c, i;
while( (s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if(!isdigit(c) && c != PERIOD && c != '-')
return c;
if(c == '-')
{
next = getch();
if(!isdigit(next) && next != PERIOD)
return c;
c = next;
}
else {
c = getch();
}
i= 0;
while(isdigit(s[++i] = c)) /* collect integer part */
c = getch();
if(c == PERIOD) /* collect fraction part */
while(isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if(c != EOF)
ungetch(c);
return NUMBER;
}
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if(bufp < MAXBUF)
buf[bufp++] = c;
else
printf("ungetch: too many characters.");
}