Compilation with no errors...yay! Thanks for the wiki again.
I'm declaring an array of items from the user-input to push on to the stack to calculate. Inside of a for-loop, I believe I have the correct logic:
Code:
for (i=0; i<strlen(input); i++) {
switch(input[i]) {
case '+':
push(&stk, (input[i] - 48));
push((&stk), pop(&stk) + pop(&stk));
break;
case '-':
push(&stk, (input[i] - 48));
push((&stk), pop(&stk) - pop(&stk));
break;
case '*':
push(&stk, (input[i] - 48));
push((&stk), pop(&stk) * pop(&stk));
break;
case '/':
push(&stk, (input[i] - 48));
push((&stk), pop(&stk) / pop(&stk));
break;
case '1':
push(&stk, (input[i] - 48));
break;
case '2':
push(&stk, (input[i] - 48));
break;
case '3':
push(&stk, (input[i] - 48));
break;
case '4':
push(&stk, (input[i] - 48));
break;
case '5':
push(&stk, (input[i] - 48));
break;
case '6':
push(&stk, (input[i] - 48));
break;
case '7':
push(&stk, (input[i] - 48));
break;
case '8':
push(&stk, (input[i] - 48));
break;
case '9':
push(&stk, (input[i] - 48));
break;
case 48:
push(&stk, (input[i] - 48));
break;
default:
printf("Invalid Input\n"); }
}
return 0;
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);
}
When I execute it, none of the input characters are matching, and they all cascade to the default statement of invalid input. Am I incorrect in my implementation of:
char input[STACKSIZE];
...
fgets(input, sizeof(input), stdin);
...
push(&stk, (input[i] - 48)) and so on?