#include<iostream>
#include<fstream>
using namespace std;
const int N=10001;int k=0,sum=0;
class Stack{
public:
int sTop,sBase;
char stack[N];
Stack(){
sTop=sBase=0;
}
char pop(){
if(sTop!=sBase) return stack[sBase---1];
else cout<<"栈已空,无法pop\n";
}
void push(char s){
stack[sBase++]=s;
}
};
char bracket[7]={'{','}','[',']','(',')'};
int get_index(char c){
int i=0;
while(bracket[i]!=c) i++;
return i;
}
int main(){
ifstream ifs;char val;
Stack stk;
ifs.open("data.txt",ios::in);
while(ifs>>val){
if(val=='{'||val=='}'||val=='['||val==']'||val=='('||val==')'){
if(stk.sBase==stk.sTop) stk.push(val);//如果是第一个元素,直接进栈
else if(stk.stack[stk.sBase-1]==bracket[get_index(val)-1]){
stk.pop();//如果和前一个括号匹配,则前一个括号退栈
sum++;
}
else stk.push(val);//进栈
}
}
if(stk.sTop==stk.sBase) cout<<"匹配的对数为"<<sum<<endl;
else {
cout<<"不匹配,输出不匹配的部分\n";
while(k!=stk.sBase) cout<<stk.stack[k++];
}
return 0;
}