栈--括号匹配
<span style="font-size:18px">#include<iostream> #include<stdlib.h> #include<string> #include<string.h> using namespace std; struct node{ int size; char *base,*top; }; node stack; const int stacksize=10; bool isbracket(char x) { if(x=='('||x==')'||x=='['||x==']') return true; return false; } int main() { string s; int i,flag; flag=1; stack.size=stacksize; stack.base=(char*)malloc(stack.size*sizeof(char)); if(!stack.base) exit(OVERFLOW); stack.top=stack.base; getline(cin,s); for(i=0;i<s.size();i++) { if(stack.top-stack.base>=stack.size) { stack.base=(char*)realloc(stack.base,(stack.size+stacksize)*sizeof(char)); stack.top=stack.base+stack.size; stack.size+=stacksize; } if(isbracket(s[i])) { if(s[i]=='('||s[i]=='[') { *stack.top=s[i]; stack.top+=1; } else { if(stack.base==stack.top) { flag=0; cout<<"error"<<endl; break; } if((*(stack.top-1)=='('&&s[i]==')')||(*(stack.top-1)=='['&&s[i]==']')) { stack.top=stack.top-1; } else { flag=0; cout<<"error"<<endl; break; } } } } if(stack.base==stack.top&&flag==1) cout<<"right"<<endl; else if(flag==1) cout<<"error"<<endl; system("pause"); return 0; }</span>
补充:软件开发 , C++ ,