Jump to content

Problem with Stacks in C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
22 replies to this topic

#1
TheUmer

TheUmer

    Newbie

  • Members
  • PipPip
  • 27 posts
I have to implement an RPN calculator in C language using stacks and pointers, while keeping it simple. I have a few problems in the code which I've tried to explain under the posted fragment of the code. Kindly take a look:



int main(int argc, char *argv[])

{

    int i, rc;

    char elements[5];

    char *ptr;

    int array[2];

    int array2[2];

    int temp, temp2;


    stack_elem_t elem;

    stackADT mystack = NULL;


    printf ("Enter five elements, operants and operators included.\n");

    for (i=0; i<5; i++)

    {

        scanf ("%s", &elements[i]);

        if (isdigit(elements[i]))

        {

         // array[i] = strtol (elements, &ptr , 0);

            array[i] = elements[i] - '0';

            push(mystack, array[i]);

        }

        if (!isdigit(elements[i]))

        {

            switch (elements[i])

            {

                case '+':

                    if (i < 2)

                          printf ("Not enough operators to perform addition operation!\n");

                    array2[0] = pop(mystack, &array[0]); // Output: 1

                    array2[1] = pop(mystack, &array[1]); // Output: 1

                    printf ("%d\n", array[0]); // Output: 3 (as I entered)

                    printf ("%d\n", array[1]); // Output: 4 (as I entered) 

                    temp = (array[0]+array[1]);

                    push (mystack, temp);

                    printf ("%d\n", temp); // Output: 1

                    temp2 = pop (mystack, &temp);

                    printf ("%d\n",temp2); // Output: 1

                    break;

}



And MAIN problem is:

Now the problem is that this does not add the two integers. Why is this happening? Besides, it does not take anything in array2[0] and array2[1]. When I pop the elements array[0] and array[1] into array2[0] and array2[1] and print them out, only 1 is printed out. What could be the problem?

Further, when I add array[0] and array[1] into temp and print temp out, again, 1 is printed and nothing else. However, if I print out array[0] and array[1] separately, they are displayed as I enter them, i.e 3 and 4.

What's going on?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You've got something very weird going on: why does pop take two parameters? You need to show us the pop function before we can help.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
TheUmer

TheUmer

    Newbie

  • Members
  • PipPip
  • 27 posts
I'll post my entire code. See what and where the problem exists:

(I'm posting it all other than the MAIN programme which I've already posted)


#include <stdio.h>

#include <stdlib.h>


#define  MAX_STACK_DEPTH  10


enum stackADT_enums {

    ADT_NOERROR,

    ADT_INVALID_STACK,

    ADT_NOMEM,

    ADT_STACK_FULL,

    ADT_STACK_EMPTY,

};



typedef int stack_elem_t;

typedef struct stackCDT *stackADT;


struct stackCDT {

    stack_elem_t stk[MAX_STACK_DEPTH];

    int stack_ptr;

};


int create_stack (stackADT *pstack);

int free_stack (stackADT stack);

int push (stackADT stack, stack_elem_t elem);

int pop (stackADT stack, stack_elem_t *pelem);

int depth (stackADT stack);

int print_stack (stackADT stack);



int create_stack (stackADT *pstack)

{

    *pstack = malloc(sizeof(struct stackCDT));

    if (!*pstack) {

        return (ADT_NOMEM);

    }

    (*pstack)->stack_ptr = -1;

    return (ADT_NOERROR);

}


int free_stack (stackADT stack)

{

    if (!stack) {

        return (ADT_INVALID_STACK);

    }

    free(stack);

    return (ADT_NOERROR);

}


int push (stackADT stack, stack_elem_t elem)

{

    if (!stack) {

        return (ADT_INVALID_STACK);

    }

    if (stack->stack_ptr == (MAX_STACK_DEPTH - 1)) {

        return (ADT_STACK_FULL);

    }

    ++stack->stack_ptr;

    stack->stk[stack->stack_ptr] = elem;

    return (ADT_NOERROR);

}


int pop (stackADT stack, stack_elem_t *pelem)

{

    if (!stack) {

        return (ADT_INVALID_STACK);

    }

    if (stack->stack_ptr == -1) {

        return (ADT_STACK_EMPTY);

    }

    *pelem = stack->stk[stack->stack_ptr];

    --stack->stack_ptr;

    return (ADT_NOERROR);

}


int print_stack (stackADT stack)

{

    int i;


    if (!stack) {

        return (ADT_INVALID_STACK);

    }

    printf("Top ");

    for (i = stack->stack_ptr ; i >= 0 ; --i) {

        printf("%d, ", stack->stk[i]);

    }

    printf("Bottom\n");

    return (ADT_NOERROR);

}


void print_menu (void)

{

    printf("\nPress:\n");

    printf("1 to create stack.\n");

    printf("2 to free stack.\n");

    printf("3 to push element.\n");

    printf("4 to pop element.\n");

    printf("5 to print stack contents.\n");


    return;

}



What now?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
array in your first if is being used as if it has 5 elements, but you declared it with 2. array and array2 could easily be stomping on each other.

You still haven't posted all of main.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
TheUmer

TheUmer

    Newbie

  • Members
  • PipPip
  • 27 posts
Here's the entire main:


int main(int argc, char *argv[])

{

    int i, rc;

    char elements[5];

    char *ptr;

    int array[2];

    int array2[2];

    int temp, temp2;


    stack_elem_t elem;

    stackADT mystack = NULL;


    rc = create_stack(&mystack);


    printf ("Enter five elements, operants and operators included.\n");

    for (i=0; i<5; i++)

    {

        scanf ("%s", &elements[i]);

        if (isdigit(elements[i]))

        {

         // array[i] = strtol (elements, &ptr , 0);

            array[i] = elements[i] - '0';

            push(mystack, array[i]);

        }

        if (!isdigit(elements[i]))

        {

            switch (elements[i])

            {

                case '+':

                    if (i < 2)

                        printf ("Not enough operators to perform addition operation!\n");

                    array2[0] = pop(mystack, &array[0]);

                  //  array[0] = strtol (array[0], NULL , 10);

                    array2[1] = pop(mystack, &array[1]);

                    printf ("%d\n", array[0]);

                    printf ("%d\n", array[1]);

                    temp = (array[0]+array[1]);

                    push (mystack, temp);

                    temp2 = pop (mystack, &temp);

                    printf ("%d\n",temp2);

                    rc = print_stack(mystack);

          //        printf ("%d\n", rc);

                    break;

                case '-':

                    if (i < 2)

                        printf ("Not enough operators to perform addition operation!\n");

                   /* array[0] = pop(mystack, &elements[0]);

                    array[1] = pop(mystack, &elements[1]);

                    temp = (array[0]-array[1]);

                    push (mystack, temp);

                    rc = print_stack(mystack);

                    printf ("%d\n", rc);*/

                    array2[0] = pop(mystack, &array[0]);

                  //  array[0] = strtol (array[0], NULL , 10);

                    array2[1] = pop(mystack, &array[1]);

                    printf ("%d\n", array[0]);

                    printf ("%d\n", array[1]);

                    temp = (array[0]-array[1]);

                    push (mystack, temp);

                    printf ("%d\n", temp);

                    temp2 = pop (mystack, &temp);

                    printf ("%d\n",temp2);

                    rc = print_stack(mystack);

                    break;

                case '*':

                    if (i < 2)

                        printf ("Not enough operators to perform addition operation!\n");

                  /*  array[0] = pop(mystack, &elements[0]);

                    array[1] = pop(mystack, &elements[1]);

                    temp = (array[0]*array[1]);

                    push (mystack, temp);

                    rc = print_stack(mystack);

                    printf ("%d\n", rc);*/

                    array2[0] = pop(mystack, &array[0]);

                  //  array[0] = strtol (array[0], NULL , 10);

                    array2[1] = pop(mystack, &array[1]);

                    printf ("%d\n", array[0]);

                    printf ("%d\n", array[1]);

                    temp = (array[0]*array[1]);

                    push (mystack, temp);

                    printf ("%d\n", temp);

                    temp2 = pop (mystack, &temp);

                    printf ("%d\n",temp2);

                    rc = print_stack(mystack);

                    break;

                case '/':

                    if (i < 2)

                        printf ("Not enough operators to perform addition operation!\n");

                  /*  array[0] = pop(mystack, &elements[0]);

                    array[1] = pop(mystack, &elements[1]);

                    temp = (array[0]/array[1]);

                    push (mystack, temp);

                    rc = print_stack(mystack);

                    printf ("%d\n", rc);*/

                    array2[0] = pop(mystack, &array[0]);

                  //  array[0] = strtol (array[0], NULL , 10);

                    array2[1] = pop(mystack, &array[1]);

                    printf ("%d\n", array[0]);

                    printf ("%d\n", array[1]);

                    temp = (array[0]/array[1]);

                    push (mystack, temp);

                    printf ("%d\n", temp);

                    temp2 = pop (mystack, &temp);

                    printf ("%d\n",temp2);

                    rc = print_stack(mystack);

                    break;

            }

        }

    }


Now that I've ncluded creat_stack in my code which, supid enough, I didn't do earlier, the problem seems to have reduced. I'll try to improve the code and post it here, in case of further problems. Thanks.

#6
TheUmer

TheUmer

    Newbie

  • Members
  • PipPip
  • 27 posts
We won't enter more than two simultaneous operands, for the time being at least, so I don't think so we need array1 greater than 2. Wouldn't it work?

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
array[i] = elements[i] - '0';
This is BAD!!!
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
TheUmer

TheUmer

    Newbie

  • Members
  • PipPip
  • 27 posts
What's wrong with that? I think it's working pretty fine.

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
array is limited to indexes of 0 or 1, i goes up to 4.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts

Quote

array[i] = elements[i] - '0';
This is BAD!!!

That and it's not portable. I'd get dinged for it if I were to write that for a class assignment.

Quote

#include <ctype.h>
...
array[i] = atoi(elements[i]);

sudo rm -rf /

#11
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

dargueta said:

That and it's not portable. I'd get dinged for it if I were to write that for a class assignment.
Hm? What's not portable about it?

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Depends on the locality/charset. Unicode would break it, I think, unless you use a typecast.

Strange Way to Convert Char to Int? (Yes, I'm aware this is Java.)
sudo rm -rf /