I've been working on this assignment for the past week or so and have been stuck trying to work out what is wrong with my pop() function.
The push() function works fine and is able to store input tokens into the stack; however, as soon as I try to pop them, as per the shunting yard algorithm, the code is unable to compile.
Hopefully someone can help me out here!
I apologize if I've used the wrong terminology / caused confusion in my question it is my first programming subject!
Here is the incomplete code so far including the pop function at the bottom:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAX_LENGTH 400
#define MAX_STACK_SIZE 40
void NoWhitespace(char *string);
int IsNumber(char token);
int IsOperator(char token);
int Push(char *stack, int *top, char token);
int Pop(char *stack, int *top, char token);
int Top(char *stack);
int OrderOfOps(char token);
int main()
{
int top = -1;
char input[MAX_LENGTH];
char token;
char output[MAX_LENGTH];
char stack[MAX_STACK_SIZE];
char popped;
//* Greeting Message *//
printf("Simple Calculator\n");
//* While this statement is true, continue with program *//
while(1)
{
//* Display Prompt *//
printf(">>> ");
fflush(stdout);
//* Get the user input *//
gets(input);
//* Check for help *//
if (input[0] == ('h') && input[1] == '\0')
{
printf("Simple Calculator understands the following arithmetic operations:\n^ exponentiation\n+ addition\n- subtraction\n* multiplication\n/ division\n");
}
//* Check for quit *//
else if (input[0] == ('q') && input[1] == '\0')
{
printf("Goodbye!");
return 0;
}
//* Get rid of all the whitespace in the input before looping through it *//
NoWhitespace(input);
//* Initialize value j before the for loop begins so that after breaking, it will reset to zero *//
int j = 0;
//* Go through every component of the string *//
for (int i = 0; input[i] != '\0' ; i++)
{
//* Let variable 'token' be equal to the component that the for loop is up to in the input string *//
token = input[i];
//* Check if the token is a number *//
if (IsNumber(token))
{
//* Add it to the output array and increment j by one *//
output[j++] = token;
}
//* Check if the token is an operator *//
else if (IsOperator(token))
{
if (stack[0] == '\0')
{
Push(stack, &top, token);
}
while (stack[0] != '\0')
{
if ((OrderOfOps(token) <= OrderOfOps(Top(stack))))
{
popped = Pop(stack, &top, Top(stack));
output[j++] = popped;
}
else
{
Push(stack, &top, token);
}
}
}
else if (token == '(')
{
Push(stack, &top, token);
}
}
printf(output);
printf("\n");
printf(stack);
printf('\n');
}
return 0;
}
int Pop(char *stack, int *top, char token)
{
if( *top == -1 )
{
return(-1);
}
else
{
stack[*top] = token;
*top = *top - 1;
return(1);
}
}