I have a program that compiles with no errors or warnings, but the logic is flawed. It is a postfix calculator...I have a default in the switch statement that is supposed to match non-operator input and post it.
The switch is called as a result of a function to define isoperator conditions.
Unfortunately, the input is read, and each char input, space, and \n is printf'd back out as the invalid input default and a random math total is displayed.
Am I misinterpreting the logic contained within my switch statement?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define STACKSIZE 256
#define TRUE 1
#define FALSE 0
/* stack type definition */
typedef struct {
int top;
float items[STACKSIZE];
} stack_type;
/* function prototypes */
int is_empty(stack_type*);
void push(stack_type*, float);
float pop(stack_type*);
int main(void)
{
char buffer[256];
char *input[STACKSIZE];
stack_type stk;
int i=0;
stk.top = -1; /* Initialize the stack to empty */
int sum;
printf("This program will evaluate postfix expressions using a\n");
printf("good implementation of stacks.\n");
printf("You may enter up to a 50-char expression.\n");
printf("Please enter a postfix/reverse-polish expression for me.\n");
fgets(*input, sizeof(*input), stdin);
while(1) {
for (i=0; i<strlen(*input); i++) {
int operator = is_operator(input[i]);
if(operator)
{
switch(operator) {
case '+':
sum = pop(&stk) + pop(&stk);
break;
case '-':
sum = pop(&stk) - pop(&stk);
break;
case '*':
sum = pop(&stk) * pop(&stk);
break;
case '/':
sum = pop(&stk) / pop(&stk);
break;
case '%':
sum = pop(&stk) % pop(&stk);
break;
default:
printf("Epic Fail...invalid input.\n");
}
push(&stk, sum);
}
else if (isdigit(*input[i]))
{
push(&stk, (*input[i] - 48));
}
else if (strcmp(*input, "quit\n") == 0)
{
printf("Time to quit...goodbye!\n");
exit(0);
}
}
printf("The total is %f\n", stk);
printf("Please enter another expression: \n");
fgets(*input, sizeof(*input), stdin);
}
return 0;
}
/* IS Empty */
int is_empty(stack_type* stk)
{
if ( (*stk).top == -1 )
return TRUE;
else
return FALSE;
}
/* Push */
void push(stack_type* stk, float element)
{
if ( stk == NULL )
{ printf("Warning! Stack pointer is set to NULL.\n");
exit(1); /* exit the program */
}
/* check stack is not full */
if ( (*stk).top == (STACKSIZE - 1) )
{ printf("Stack Overflow!\n");
exit(1);
}
/* increment stack top */
(*stk).top++;
/* add the new element to the top */
(*stk).items[(*stk).top] = element;
return;
}
/* Pop */
float pop(stack_type* stk)
{ float value=0;
/* Check stack exists */
if ( stk == NULL )
{ printf("Warning! Stack pointer is set to NULL. Is it really there?\n");
exit(1);
}
/* check that the stack is not empty */
if (is_empty(stk))
{ printf("Stack underflow!\n");
exit(1);
}
/* get top item */
value = (*stk).items[(*stk).top];
/* decrement top */
(*stk).top--;
/* return popped item to calling f(x); */
return value;
}
int is_operator(int x)
{
if(x == '+' || x == '-' || x =='/' || x == '*' || x == '%')
return;
}