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?
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?
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
ok that makes alot of sense! No there not in a file so Im guessing I have to enter them in manually?
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
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?
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; }
What is the variable type that you are using for your stack?How do you send my string to my class?
Don't you want to some how use your class member functions?
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.And what would I say that will determine how which is a good use and which is a bad use
Jim
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!
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?
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
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; }
Here's my new code..Im getting closer and closer. Something is off. I dont understand my error.
1>------ Build started: Project: Project1, Configuration: Debug Win32 ------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> 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 ==========
> 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.
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; }
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.