Bracket Operator

Oct 23, 2011 at 10:30pm
Hi, so iv been working on a program that checks bracket nesting:

i.e. str = ({}[]) would be nested properly
str = ({))[] would be wrong

this program has to use a stack for storing the unmatched operators.

how do?
Oct 23, 2011 at 10:33pm
i have this to check if they are balenced aswell

#include <iostream>
#include "simpio.h"
#include "genlib.h"
#include "stack.h"
#include <string>

void BracketsEven(string str);
void BracketsNested(string str);

int main() {
cout << "please enter the string for which bracket operator will be applied too: \n \n";
string str;
str = GetLine();
BracketsEven(str);
BracketsNested(str);

return 0;
}

void BracketsEven(string str) {
string character;
Stack<string> unmatched;;

for (int i = 0; i <= str.length(); i++){
character = str[i];

if (character == "(" || "{" || "[") {
unmatched.push(character);
}
if (character == ")" || "}" || "]") {
unmatched.pop();
}
}
if (unmatched.isEmpty() == true) {
cout << "\nThe bracketing operators are unbalenced.";
}
else cout << "The brackets operators are balenced";
}
Oct 24, 2011 at 12:02am
You need to learn how to structure your ifs correctly.

This is incorrect:
 
if (character == "(" || "{" || "[") 


this is correct:
 
if( (character == '(' ) || (character == '{') || (character == '[') )


I don't know why I keep seeing this assumption of c/c++ and if statements. we need to check character against each grouping symbol in this case. Otherwise we need to learn how to do select/case statements.
Oct 24, 2011 at 1:06am
regardless the question i asked was more geared toward the algorithm itself...
Oct 24, 2011 at 1:20am
Your algorithm looks right from where I was standing. It was the structure of the if's that were wrong. You could learn to use code /code tags though. It was different form the original post that I saw.
Topic archived. No new replies allowed.