HW09
user_8384735
c_cpp
3 years ago
1.2 kB
7
Indexable
// main.cpp #include<iostream> using namespace std; #define shift(x) x = x->next class linklist; class node{ private: char data; node *next; public: node():data(0), next(0){}; node(char x):data(x), next(0){}; friend class linklist; }; class linklist{ private: node *first; public: linklist():first(0){}; void push(char x); void pop(); char top(); bool empty(); }; void linklist::push(char x){ node *newnode = new node(x); newnode->next = first; first = newnode; } void linklist::pop(){ if (first){ shift(first); } } char linklist::top(){ return first->data; } bool linklist::empty(){ return !first; } int solve(string s){ linklist l1; for (char x : s){ if (x == '(') l1.push(x); else if (!l1.empty() && x == ')') l1.pop(); else if (x == ')') return 1; } return l1.empty() ? 0 : 2; } int main(){ string m[3] = { "Balanced Parentheses\n", "Error!! Left parentheses have no matching right parentheses\n", "Error!! Right parentheses have no matching left parentheses\n" }; string s; while (1){ cout << "Enter your expression: "; if (cin >> s && s != "EOF"){ cout << s << endl; cout << m[solve(s)]; cout << endl; }else break; } }
Editor is loading...