#include<iostream>usingnamespacestd;#defineMAX_SIZE50typedefstruct{intstack[MAX_SIZE];inttop;}Stack;intmain(){Stackst;st.top=-1;charch;boolflag=false;while(cin>>ch){switch(ch){case"(":st.stack[++st.top]=ch;break;case")":if(st.stack[st.top]=="("){st.top--;break;}else{cout<<"Dont"tmatch!"<<endl;flag=true;break;}case"[":st.stack[++st.top]=ch;break;case"]":if(st.stack[st.top]=="["){st.top--;break;}else{cout<<"Dont"tmatch!"<<endl;flag=true;break;}case"{":st.stack[++st.top]=ch;break;case"}":if(st.stack[st.top]=="{"){st.top--;break;}else{cout<<"Dont"tmatch!"<<endl;flag=true;break;}default:break;}if(flag){break;}}if(!flag){cout<<"matching!"<<endl;}return0;}簡單思路就是利用了棧,可以匹配3對括號,如果有什麼疑問可以再跟帖~~
#include<iostream>usingnamespacestd;#defineMAX_SIZE50typedefstruct{intstack[MAX_SIZE];inttop;}Stack;intmain(){Stackst;st.top=-1;charch;boolflag=false;while(cin>>ch){switch(ch){case"(":st.stack[++st.top]=ch;break;case")":if(st.stack[st.top]=="("){st.top--;break;}else{cout<<"Dont"tmatch!"<<endl;flag=true;break;}case"[":st.stack[++st.top]=ch;break;case"]":if(st.stack[st.top]=="["){st.top--;break;}else{cout<<"Dont"tmatch!"<<endl;flag=true;break;}case"{":st.stack[++st.top]=ch;break;case"}":if(st.stack[st.top]=="{"){st.top--;break;}else{cout<<"Dont"tmatch!"<<endl;flag=true;break;}default:break;}if(flag){break;}}if(!flag){cout<<"matching!"<<endl;}return0;}簡單思路就是利用了棧,可以匹配3對括號,如果有什麼疑問可以再跟帖~~