December 1st, 2010, 01:40 AM
#1
Im so close and Im stumped
Write a program in C++ that checks various lines of text (taken as arithmetical expressions) to see if the following 3 pairs of grouping symbols: '(', ')'; '[', ']'; '{', '}' are being used properly.
You must use a Stack with the array implementation using a class
Test program on the following:
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)))))
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;
}
};
bool Opener(char ch){
if((ch == '(') || (ch =='[') || (ch =='{'))
return true;
else
return false;
}/*end of opener*/
bool Match(char Lc, char Rc){
if(( Lc == '(') && (Rc == ')') || ((Lc == '[') && (Rc == ']')) ||((Lc == '{') && (Rc == ']')))
return true;
else
return false;
}/*end of Match*/
bool Closer(char ch){
if((ch == ')') || (ch == ']') || (ch == '}'))
return true;
else
return false;
}/*end of closer*/
int main(){
Astack S;
char s;
cout <<"Enter data:";
cin >> s;
if(Opener(s)){
S.push (s);
}
if(Closer(s)){
S.pop(s);
}
while(S.pop(s)){
if(Match)
cout << "Grouping symbols used properly";
else
cout << "Unmatched grouping symbols";
}
}
I know I should take whatever I pop off the stack and then have Match check to see if if their are matches for each closer I pop. When I compile it just gives me everything is used properly which is incorrect.
December 1st, 2010, 02:42 AM
#2
Re: Im so close and Im stumped
The line
is for sure not what you want.
gcc says: "warning: the address of `bool Match(char, char)', will always evaluate as `true' "
and IMHO it's right.
"if (Match) " checks if Match is different from zero. Match is a function and you'll check the adress of the function here (which will always be different from zero).
If you want to call Match use brackets ( ) and parameters.
With regards
Programartist
Last edited by ProgramArtist; December 1st, 2010 at 02:59 AM .
December 1st, 2010, 06:31 AM
#3
Re: Im so close and Im stumped
Ok I finally figured it out. Thanks for the help. Now the problem is that it always displays "Unmatched grouping symbols".
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;
}
};
bool Opener(char ch){
if((ch == '(') || (ch =='[') || (ch =='{'))
return true;
else
return false;
}/*end of opener*/
bool Match(char Lc, char Rc){
if(( Lc == '(') && (Rc == ')') || ((Lc == '[') && (Rc == ']')) ||((Lc == '{') && (Rc == ']')))
return true;
else
return false;
}/*end of Match*/
bool Closer(char ch){
if((ch == ')') || (ch == ']') || (ch == '}'))
return true;
else
return false;
}/*end of closer*/
int main(){
Astack S;
char s;
cout <<"Enter data:";
cin >> s;
if(Opener(s)){
S.push (s);
}
else
cin.ignore(s);
if(Closer(s)){
S.pop(s);
}
while(S.pop(s)){
if(Match(Opener(s), Closer(s)))
cout << "Grouping symbols used properly";
else
cout << "Unmatched grouping symbols";
}
}
December 1st, 2010, 07:14 AM
#4
Re: Im so close and Im stumped
Use the debugger to step through the Match function to see what it's doing.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks