2

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);
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
C. A
  • 21
  • 1
  • What is the actual error? – kaylum Sep 11 '15 at 05:26
  • After 'char stack[MAX_STACK_SIZE];' was uninitialized i get the error Description Resource Path Location Type 'stack' may be used uninitialized in this function [-Wmaybe-uninitialized] calculator.c /Calculator line 81 C/C++ Problem Besides that, there are no warnings or errors. – C. A Sep 11 '15 at 05:41
  • If I run the code and input numbers, calculator.exe stops responding. The same goes with operator inputs. – C. A Sep 11 '15 at 05:43
  • 3
    Please don't `printf` variable strings as format string as you do in `printf(stack)`. What if your stack cntains formatting data? Prefer `puts(stack)` od `printf("%s\n", stack)`. – M Oehm Sep 11 '15 at 05:43
  • 3
    Please put all relevant information in the question description. Not in the comments where they can be easily missed. In any case, the warning (not error) message seems pretty clear. You are reading `stack` before it is ever written to. In several places. For example: `if (stack[0] == '\0')`. `stack` was never initialised prior to that line. – kaylum Sep 11 '15 at 05:44
  • Sorry about that, this is my first question so i didn't really know how to ask! Thank you for your help, but i'm still not sure why the pop() function isn't working, unless its all to do with me not initializing the stack? – C. A Sep 11 '15 at 05:55
  • 2
    Fix all known problems and re-test. There's no point talking about code that has known bugs in it. And yes, absolutely, accessing unintialised variables leads to [Undefined behaviour](https://en.wikipedia.org/wiki/Undefined_behavior). – kaylum Sep 11 '15 at 06:09
  • Your Pop function is writing to the stack, not reading from it. – stark Sep 11 '15 at 14:31

0 Answers0