Thread: stacks

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    53

    stacks

    Hello,

    I'm trying to do this stack container that reads a string and is supposed to find if the string has a balanced parentheses (opening and closing parentheses). for example

    Z+ X- Y //no parentheses
    Z * X / (Y+10) //Matching parenthesis
    Z * ((X / Y) + D + (F-10) //Parenthesis don’t match. Missing right parenthesis
    Z * (X / Y) + W + (F-10)) //Parenthesis don’t match. Missing left parenthesis

    so far this is what I got

    Code:
    #define STACK_MAX 100
    
    class Stack {
    
        private:
            int        data[STACK_MAX];
            int        size;
    
        public:
            Stack() {       // Constructor
                size = 0;
            }
    
            ~Stack() { }    // Destructor
    
            int top() {
    
                if (size == 0) {
                    printf("Error: stack empty\n");
                    return -1;
                }
                return data[size-1];
            }
    
            void push(int d) {
    
                if (size < STACK_MAX)
                    data[size++] = d;
                else
                    printf("Error: stack full\n");
            }
    
            void pop() {
    
                if (size == 0)
                    printf("Error: stack empty\n");
                else
                    size--;
            }
    };
    
    int
     main()
    {
      
    
    
      return 0;
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    The stack looks okay.
    The problem in question doesn't really require a stack though..
    All you have to do is to read the expression character by character.
    When you find a '(' increment a counter, and ')' to decrement.
    The final count determines the result. ( 0 depicting no error).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please delete the empty non-virtual destructor. It has become annoyingly common for people to think they need to add that when in fact they don't.

    Your next steps would appear to be reading in the string, and then processing it, possibly making use of yout stack. You don't actually need a stack for this task as it is written, but presumably you need to be doing slightly more than you've told us.
    So what's the problem?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    53
    This is where I am right now. I think this should work, but it's not doing what's supposed to do.

    Code:
    // A simple Integer stack, Array based
    #include <algorithm>
    #include <cassert>
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    #define STACK_MAX 100
    
    class Stack {
    
        private:
            int        data[STACK_MAX];
            int        size;
    
        public:
            Stack() {       // Constructor
                size = 0;
            }
    
            ~Stack() { }    // Destructor
    
            int top() {
    
                if (size == 0) {
                   cout << "Error: stack empty\n";
                    return -1;
                }
                return data[size-1];
            }
    
            void push(int d) {
    
                if (size < STACK_MAX)
                    data[size++] = d;
                else
                    cout << "Error: stack full\n";
            }
    
            void pop() {
    
                if (size == 0)
                    cout << "Error: stack empty\n";
                else
                    size--;
            }
    
            bool isEmpty() {
                if (size == 0)
                    return -1;
                        else
                            return 1;
            }
    };
    
    int main()
    {
    const char left_p = '(';
    const char right_p = ')';
    bool error = false;
    char ch;
    
    Stack s;
    
    cout << "Please enter an algebraic expression: ";
    cin.get(ch);
    
    	while (( ch != '\n')  && (!error) )
        {
    		if (ch == left_p)
    			 s.push(ch);
    
    
    		if (ch == right_p)
    			//if (s.isEmpty())
                 //   error = true;
                  //  else
                        s.pop();
                        cin.get(ch);
    	}
    		if ((!error) && s.isEmpty())
    		cout<<"\nResult: This expression is valid. \n"<<endl;
                else
                    cout<<"\nResult: This expression is invalid. \n"<<endl;
      return 0;
    }

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by manasij7479 View Post
    When you find a '(' increment a counter, and ')' to decrement.
    The final count determines the result. ( 0 depicting no error).
    Just want to add that the counter should be decremented only when it's larger than zero, to avoid situations like the following being interpreted as correct:
    X * ) Y + Z (
    Devoted my life to programming...

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please give an example input, the output you are getting, and for good measure also state the expected output.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stacks
    By marquis1431 in forum C Programming
    Replies: 3
    Last Post: 05-06-2008, 02:28 PM
  2. Stacks?
    By Cloud6 in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2007, 03:47 PM
  3. Need help with stacks
    By muran_pling in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2007, 09:16 PM
  4. Stacks
    By Kunzy in forum C++ Programming
    Replies: 7
    Last Post: 01-28-2003, 04:10 PM
  5. Stacks
    By mrmajetic4 in forum C Programming
    Replies: 1
    Last Post: 10-09-2001, 04:49 AM