Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Parenthesis checker
//Title of this code #include <iostream> #include <string> #include <list> using namespace std; bool matchParenthesis(char a, char b) { if (a == '(' && b == ')') return true; if (a == '[' && b == ']') return true; if (a == '{' && b == '}') return true; if (a == '<' && b == '>') return true; return false; } // () {} [] <> bool checkMultiParenthesis(const string& s) { list<char> stack; for (int i = 0; i < s.length(); ++i) { // opener if (s[i] == '(' || s[i] == '{' || s[i] == '[' || s[i] == '<') stack.push_back(s[i]); // closer if (s[i] == ')' || s[i] == '}' || s[i] == ']' || s[i] == '>') { if (stack.empty() || !matchParenthesis(stack.back(), s[i])) return false; stack.pop_back(); } } return true; } //eg. ((alf)ls) ? valid // )( (dkk)() ? invalid // // )( )(( ))( // bool checkParentheses(const string& s) { bool foundFirst = false; int sum = 0; int i = 0; while (i < s.length()) { if (!foundFirst && s[i] == ')') return false; else if (s[i] == ')') --sum; else if (s[i] == '(') { foundFirst = true; ++sum; } ++i; } return !sum; } int main() { std::cout << boolalpha << checkParentheses("a(a)a(a)a") << endl; std::cout << boolalpha << checkParentheses("ae()w()(a()()r()(d)()()g()(e))()aa") << endl; cout << boolalpha << checkMultiParenthesis(" < ( [] { {}{<>} } ) > ") << endl; }
run
|
edit
|
history
|
help
0
Simple use of function templete and namespace
Size of data type test
345325
Segmented Sieve
teste
replace_copy-30-Seconds-of-C++
div
generating all valid parenthesis
Procesos estocasticos Beta 2.0
temporaries and move constructor