Thread: stack (array impl) problem: postfix notation

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    stack (array impl) problem: postfix notation

    Hi, guys, I've been trying to get this code to work. First, get the postfix notation string from user (e.g. 3 4 +). The digits are single digits, there is one space between them. Nothing fancy. After I get the string, I clean it up from spaces, and then I try to perform calculations. Only + - * /, nothing else. However, the program does not work. It compiles, but I have 4 warnings:
    integral size mismatch in argument : conversion supplied in the following line (all 4 of the same lines in the switch statement:
    Code:
    case '+': result = left + right;
    	          i++; push(&myStack, result); break;
    Here is the rest of the code. I do not paste here the functions as they have been working properly in other programs.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXSIZE 50
    
    typedef struct {
        char elements[MAXSIZE];
        int count;
    } StackType;
    
    StackType myStack;
    
       
    void initialize(StackType *stackPtr);
    int isEmpty(StackType *stackPtr);
    int isFull(StackType *stackPtr);
    void push(StackType *stackPtr, char c);
    char pop(StackType *stackPtr);
    
    
    int main()
    {
       int left,right;
       int result = 0;
       int i = 0;
       char c;
       char expression[MAXSIZE];
       
       initialize(&myStack);
       
       printf("Enter the expression in the RPN form: ");
       gets(expression);
       
       while(expression[i] != '\0')
       {
            if(expression[i] != ' ')
            {
              i++;
              continue;
            }
            else
            {
               expression[i] = expression[i+1];
               i++;
            }
       }
              
    
       i = 0;
       while(expression[i] != '\0')
       {
          if(isdigit(expression[i]))
    	  {
    	     push(&myStack, expression[i]);
    	     i++;
    	     continue;
    	  }
    	  else
    	  {
                 c = pop(&myStack);
    	     right = atoi(&c);
    
                      if(isEmpty(&myStack))
    	  {
                        printf("The result is %d\n", right); 
                        return 0;
                      }
    
                 c = pop(&myStack);
                 left = atoi(&c);
                      }
    						
    	switch(expression[++i]) 
    	{
    	case '+': result = left + right;
    	          i++; push(&myStack, result); break;
    	case '-': result = left - right;
    	          i++; push(&myStack, result); break;
    	case '*': result = left * right;
    	          i++; push(&myStack, result); break;
    	case '/': result = left / right;
    	          i++; push(&myStack, result); break;
    	default: printf("Incorrect function. Aborting... \n"); return 0;
          }
    			
      }
    	  
        
       
       return 0;
    }
    I will appreciate if someone could take a look and tell me what I am doing wrong. I've been having problems with stacks (and queues as well)... and also this is the first time for me to use atoi and isdigit, so maybe that's where the problem is. Or the increment of i???
    Thanks.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It looks like that warning might be because you have push() defined to accept a char as the 2nd argument, but you're passing it an int.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks... I fixed this one, however I still have a warning
    for this line:
    if(isdigit(expression[i]))
    saying: subscript has type `char'. What does it mean? It cannot mean "i" is of type char as I declared it an int?

    The program does not work, basically does not display anything, is there anything else I am doing wrong? Sorry to bother...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. stack problem: print after pop function
    By ivan12yu2000 in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2009, 02:18 AM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. Array Stack Problem
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2001, 06:58 PM