Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <vector>
- #include <utility>
- using namespace std;
- struct brace
- {
- char c;
- int pos;
- };
- const vector<pair<char, char>> braces =
- { make_pair('{', '}')
- , make_pair('(', ')')
- , make_pair('<', '>')
- };
- int main()
- {
- stack<brace> stk;
- char c;
- int pos = 0;
- while (cin.get(c)) {
- ++pos;
- for (const auto &i : braces) {
- if (c == i.first) {
- stk.emplace(brace{ i.second, pos });
- }
- else if (c == i.second) {
- if (stk.empty()) {
- cerr << "Got '" << c << "' bracket without an opening one at "
- << pos << endl;
- } else {
- if (stk.top().c != c) {
- cerr << "Expected '" << stk.top().c << "' bracket (opened at " << stk.top().pos
- << "), got '" << c << "' instead at " << pos << endl;
- }
- stk.pop();
- }
- }
- }
- }
- while (!stk.empty()) {
- cerr << "Expected '" << stk.top().c << "' bracket (opened at " << stk.top().pos
- << "), got EOF" << endl;
- stk.pop();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement