Thread: stack project

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    52

    stack project

    I need to create a program that uses a stack to evaluate postfix arithmetic expressions (use only 4 arithmetical operators: +, -, *, /).
    The numbers can only be single-digit intergers. The numbers and operators are separated by single spaces. I have to use the function isdigit() to check whether a character is a digit, and the function atof() to convert a string to a float number. I was wondering if anyone can give me any tips or something. Stacks are new to me so it's hard for me to understand how these work. Also, I have no idea why she wants the atof() function. It doesn't seem to serve any purpose to me in the program, but that may be because I'm not seeing the full purpose of the program and that I've never used that function. Please help.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Basically it seems she wants the user to be able to enter a string. You then take that string and test each element with isDigit, which should return true on the case of the numbers and false on the case of the operators or whitespace. If it returns true, you convert the element to a float with atof(). As for the stack part, I don't see what this has to do with what I know to be a stack... but then I don't know much about stacks, anyway.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    so an example of something the user would enter would be something like "a b +" or something and then the program would convert it to some random float number? I don't understand if the user is supposed to type numbers or just random variables or words or what?

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I don't quite remember all that well what postfix means, but I think you would use the stack so that you could handle statements of the type "a b c +*" and such. You'd put everything in a stack, then pop an element from the element stack, then pop an operator, then pop another element, do the arithmetic, pop another op, then another element, and so forth.

    As for atof(), it's probably so that you can execute the proper arithmetic. Adding two strings together generally doesn't give the desired result...

    Furthermore, I think the user is either supposed to enter integers or you're supposed to implement some sort of variable storage in your program.
    Last edited by Happy_Reaper; 11-09-2005 at 07:27 PM.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    haha, sorry but I am still lost. thanks for trying to help though. I read over and over what you guys said but I just can't make any sense out of it. will someone please put this in the simplest terms possible or maybe just give examples of what input and output should look like.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Alright. You create a string, in other words a character array. Then you put a scanf loop that reads in the string from the user input. Let's say after that the string now looks like:

    Code:
    char equation[100] = {'1', ' ', '+', ' ', '5', ' ', '=')
    /* or if you wish... "1 + 5 =" */
    Now with that array, you do another loop. This loop now checks each element of the array with isdigit().

    Code:
    isdigit(equation[x]);
    If that returns true, you use atof() to convert the value to a float, which is stored in an array of floats.

    Code:
    float numbers[100];
    numbers[y] = atof(equation[x]);
    y++;
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    the_winky_files:

    If you dont know what a stack or a queue is, how do you expect yourself to be able to implement postfix evaluation and modify an already written program which manipulates a queue?

    These programs are not as trivial as they seem, please read a good book on Data Structures in C, and then try writing your code.
    Thanks,
    Sahil

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    ok, here's is what I've come up with so far. it works but it doesn't implement the atof() since I really don't understand where I'm supposed to put it or what purpose it serves within the program. please help me figure out where this function is needed within the program. I used an example from the book and modified it. she said this was legal in the assignment post.

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXSIZE 50
                        
                        
    typedef struct {
       char elements[MAXSIZE];
       int count;
    } Stack;
    
    void initialize(Stack *stackPtr); 
    int isEmpty(Stack *stackPtr); 
    void push(Stack *stackPtr, int x); 
    void pop(Stack *stackPtr, int * x);  
    
    
    int main()
    {
    
       char PostfixString[MAXSIZE + 1];
       int LeftOperand, RightOperand, Result;
       int i;
       char c;
       char s[MAXSIZE];  
       int howlong;
       Stack EvalStack;
    
    
       initialize(&EvalStack);
    
       printf("Enter the postfix string: ");
       fgets(PostfixString, sizeof(PostfixString), stdin); 
       
       howlong = strlen(PostfixString);
    
       for(i = 0; i < howlong; i++)
       {  
          c = PostfixString[i];
    
          s[0] = c;
          s[1] = '\0';
          
          PostfixString[i] = c;
          
          if(isdigit(c))
          {
             push(&EvalStack, (int)atoi(s));
          }
          else if(c == '+' || c == '-' || c == '*' || c == '/')
          {
             pop(&EvalStack, &RightOperand); 
             pop(&EvalStack, &LeftOperand);
             
             switch(c) {
                case '+' : push(&EvalStack, LeftOperand + RightOperand);
                           break;
                case '-' : push(&EvalStack, LeftOperand - RightOperand);
                           break; 
                case '*' : push(&EvalStack, LeftOperand * RightOperand);
                           break;                     
                case '/' : push(&EvalStack, LeftOperand / RightOperand);
                           break; 
                default: break;
             }
          }
       }
       
       pop(&EvalStack, &Result);
       printf("Value of the expression is: %d.\n", Result);
       
       
       return 0;
    }
                                  
    
    
    void initialize(Stack *stackPtr)
    {
         stackPtr->count = 0;
    }
    
    
     
    int isEmpty(Stack *stackPtr)
    {
         if(stackPtr->count == 0)
    	    return 1;
    	 else
            return 0;
    }   
    
    
    int isFull(Stack *stackPtr)
    {
         if(stackPtr->count == 50)
           return 1;
         else
           return 0;
    }
    
     
    void push(Stack *stackPtr, int x)
    
    {
         if(stackPtr->count == MAXSIZE)
    	 {
           printf("The stack is full.\n");	
           return;
         }
         else
         {
           stackPtr->elements[stackPtr->count] = x;
           stackPtr->count++;
         }
    }
    
     
    void pop(Stack *stackPtr, int *x) 
    {
      
        if(stackPtr->count == 0)
        {
           printf("The stack is empty, cannot delete.\n");
           exit(0);
        }
    	else
    	{
    	   stackPtr->count--;
    	   *x = stackPtr->elements[stackPtr->count];
    	}
    	
    	return;
    }
    Last edited by the_winky_files; 11-10-2005 at 10:30 AM.

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, you seem to be slowly cathing the concept. The one problem that you have with the code up here is that you're placing (and for the record, "pushing" something in a stack is not placing it on top...) the result back on top of the stack. You don't want to do this. After the initial operation, you then want to pop the remaining element of the stack one at a time and execute the corresponding operation with the result of the previous operation. You then repeat this process until the stack is empty. Then you can output the result.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    ok, sorry I'm having so much trouble with this. I don't understand what you trying to tell me. are you saying that I'm never popping the value of the expression? I don't get it. thanks for trying to help also.

  11. #11
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Here's an example :

    235 +*

    First you pile up everything in two stacks :
    Value Stack Op Stack
    5 *
    3 +
    2

    Then you pop an element from each stack :
    Value Stack Op Stack
    3 +
    2

    And pop another element from the value stack and execute the desired operation (3*5) :
    Value Stack Op Stack
    2 +

    Cumulative result : 15

    Again, pop an element from each stack and execute the desired operation to the cumulative result (15 + 2):
    Both Stacks Empty

    Cumulative Result : 17

    Now both stacks are empty, so you know you're done. Output the cumulative result.

    Hope that helps
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    (and for the record, "pushing" something in a stack is not placing it on top...)
    Actually, pushing something onto a stack is "placing it on top". popping something removes it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    ok, I think I am getting the problem. so my code is not pushing the operators onto a stack? therefore the problem with my coding would be here:

    Code:
    switch(c) {
                case '+' : push(&EvalStack, LeftOperand + RightOperand);
                           break;
                case '-' : push(&EvalStack, LeftOperand - RightOperand);
                           break; 
                case '*' : push(&EvalStack, LeftOperand * RightOperand);
                           break;                     
                case '/' : push(&EvalStack, LeftOperand / RightOperand);
                           break; 
                default: break;
    I should change it to where it just calls the function and it is automatically evaluated in the function instead of havng "LeftOperand + RightOperand". is this what you are trying to say?

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Actually, I wasn't trying to say anything. But yeah, you're probably right.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    haha, sorry, that was to Happy_Reaper. sorry dwks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 04:14 PM
  3. Finished Stack
    By the pooper in forum C Programming
    Replies: 11
    Last Post: 02-02-2005, 10:52 AM
  4. Stacks
    By Cmuppet in forum C Programming
    Replies: 19
    Last Post: 10-13-2004, 02:32 PM
  5. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM