findout braket CBOSS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
std::stack<int> temp;
const std::string str("(x+y)*x + x * (12*(x-y))");
for (std::string::const_iterator iter = str.begin(); iter != str.end(); iter++) {
if ('(' == *iter) {
temp.push((int)(iter - str.begin()));
}
if (')' == *iter) {
std::cout<<temp.top()<<" - "<<(int)(iter - str.begin())<<std::endl;
temp.pop();
}
}
return 0;
}