Thread: I need help

  1. #16
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    My question now is dont I have to come up with something that lets me know if the 3 grouping symbols are being use properly? And what is a class used for?

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Yes you have to come up with the procedure to determine if the symbols are being used properly.

    If your list:

    a+b
    (a-b)
    ((c-d)
    [(a-b)+(c+d)]
    (e+f]
    (f+g))
    [f-g}
    {c+d)
    {a*[(c-d)*(e+f)+(g-h)/(f+g)]+h}*b+c
    (((((a)))))

    is in a file on disk you will have to first read the file and insert the Items into your class.

    The class will be used to hold the individual symbols from your list:

    example: a + b

    You would push "a" into you class, then "+" then "b".

    Jim

  3. #18
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    ok that makes alot of sense! No there not in a file so Im guessing I have to enter them in manually?

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Yes, you will probably need to get them from the user. For initial design and test you can put your list into std::strings/Cstrings. Then parse the strings to insert the items into your stack.

    Example:
    Code:
       std::string var1 = "a+b";
       std::string var2 = "(a-b)";

    This will save typing during development.


    Jim

  5. #20
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    I think I understand more. So I will have the user input them in, then have whatever gets inputted go throught the class and then have it return good use or bad use. Is that correct?

  6. #21
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Ok I just need to know couple things. How do you send my string to my class? And what would I say that will determine how which is a good use and which is a bad use. Here is my latest edit version:
    Code:
    #include <iostream>
    using namespace std;
    
    const int DefaultListSize = 50;
    typedef char Elem;
    
    class Astack 
    {
    private:
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	Elem*listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    	};
    int main(){
    
    	char s[36];
    
    cout<<"Enter data:";
    cin.getline (s, 36, '\n');
    	while(s)
    		if()
    			cout<<"Good use";
    		else
    			cout<<"Bad use";
    
    return 0;
    }

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    How do you send my string to my class?
    What is the variable type that you are using for your stack?

    Don't you want to some how use your class member functions?

    And what would I say that will determine how which is a good use and which is a bad use
    I would be willing to bet that this is covered either in your text book or it was covered by your instructor in one of his lectures. After all this is the whole purpose of the program.

    Jim

  8. #23
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    oh ok! Actually the class is data structures. We learned about the stack and the other stuff we are on our own. The text book is not a good for explaing nothing but just the data structures. But I think I got it!

  9. #24
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Im stuck! I want to enter a string. Read whatever I enter as my string then push it all through the stack. What Im confused about is the pop. Like is the stack checking for the usage of the grouping symbols and I pop the whol string back out? Or am I suppose to push only when there is a grouping symbol and ignore anything else?

  10. #25
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Remember the stack is based on a single character so you will have to push each character onto the stack one at a time.

    You may only need to push the grouping symbols onto the stack not the variables. But I may be wrong.

    Jim

  11. #26
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    I talk to my professor and he said you want to push the opening symbols and pop the closing symbols. So I guess when I push a symbol onto the stack I have to pop a closer and if there isn't a closer than it's a bad use. Here's what I got so far:
    Code:
    #include <iostream>
    using namespace std;
    
    const int DefaultListSize = 50;
    typedef char Elem;
    
    class Astack 
    {
    private:
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	Elem*listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    	};
    int main(){
    	
    	Astack S;
    	char s[40];
    
    cout<<"Enter data:";
    cin.getline (s, 40, '\n');
    
    S.push('('|| '['|| '{');
    
    return 0;
    }

  12. #27
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Here's my new code..Im getting closer and closer. Something is off. I dont understand my error.

    Code:
    #include <iostream>
    using namespace std;
    
    const int DefaultListSize = 50;
    typedef char Elem;
    
    class Astack 
    {
    private:
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	Elem*listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    	};
    int main(){
    	
    	Astack S;
    	char s[40];
    	int j;
    
    cout<<"Enter data:";
    cin.getline (s, 40, '\n');
    
    while(s){
    	S.push('('|| '['|| '{');
    	for(j=0; j<40; j++)
    		cout << S.pop(')' || ']' || '}');
    	
    }
    
    return 0;
    }
    1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
    1> Project1.cpp
    1>c:\users\justin\documents\visual studio 2010\projects\project1\project1\project1.cpp(53): error C2664: 'Astack:op' : cannot convert parameter 1 from 'bool' to 'Elem &'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  13. #28
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > S.push('('|| '['|| 'x');
    Well this doesn't mean push one of 3 different characters.

    The result of || is a boolean expression, and it doesn't want to push a boolean (this is your error message).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #29
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Here's my new code and it works but doesn't do what I want it to do. I want it to display matched symbols founds when use properly and unmatched when not.
    Code:
    #include <iostream>
    using namespace std;
    
    const int DefaultListSize = 50;
    typedef char Elem;
    
    class Astack 
    {
    private:
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	Elem*listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    	};
    int main(){
    	
    	Astack S;
    	char s;
    
    cout <<"Enter data:";
    cin >> s;
    
    while(s != '#'){
    	if(s == '(' || s == '[' || s == '{'){
    		S.push(s);
    	}
    	else
    		cin.ignore(s);
    
    	if(s == ')' || s == ']' || s == '}'){
    		cout << S.pop(s);
    	}
    
    	if(S.IsEmpty())
    		cout << "The expression has only matching grouping symbols" << endl;
    	else
    		cout << "The expression has some unmatched grouping symbols" << endl;
    }
    return 0;
    }

  15. #30
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Here are some thoughts on your main()
    Code:
    int main()
    {
        Astack S;
        char s;
    
        cout << "Enter data:";
    //!!cin >> s;
    
        //!! you want to loop, reading symbols until # is read
        while (cin >> s && s != '#') {
            if (s == '(' || s == '[' || s == '{') {
                S.push(s);
            }
    //!!    else
    //!!        cin.ignore(s);
    
            if (s == ')' || s == ']' || s == '}') {
                //cout << S.pop(s);
                char other;
                S.pop(other);
                //!! now check '(' == ')' etc
                //!! [1+2}-1 is NOT a matched set of symbols
            }
        }                           //!! while loop ends here
        if (S.IsEmpty())
            cout << "The expression has only matching grouping symbols" <<
                endl;
        else
            cout << "The expression has some unmatched grouping symbols" <<
                endl;
    //!!} not here
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed