data structures - C++ parenthesis matching application -
i have write function accepts string , returns bool. string passed in series or different parenthesis opened or closed ex.({[]}) , returns whether 'parens' well-balanced. has implemented adding items stack. getting following error:
parenmatching_demo.cpp:18:12: error: no match 'operator==' in 'c == '('
the psudocode is:
matcher(expression) each character in expression if character opener push on stack else if character closr if stack empty return false if (opener at) top of stack not match closer return false pop stack if stack not empty return false return true
this have.
template <typename t> bool parenmatching(string c) { stack<string> s; (int = 0; < s.size(); i++) { if (c == '(' || c == '[' || c == '{' || c == '<') s.push(c); else if (c == ')' || c == ']' || c == '}' || c == '>') { if (s.empty()) return false; if (s.top() != '(' || s.top() != '[' || s.top() != '{' || s.top() != '<') return false; s.pop(); } } }
one problem type of stack, should be
stack<char> s;
then for
loop, condition should be
i < c.size()
the next problem is
if (c == '(' || c == '[' || c == '{' || c == '<')
does not compare character of string c
@ i
, need
const char c2 = c[i]; if (c2 == '(' || c2 == '[' || c2 == '{' || c2 == '<')
if need fix rest, let me/us know :)
Comments
Post a Comment