|
以下是引用xiix在2006-5-22 18:08:00的发言:
这是关于{[(匹配检测的 怎么我也弄不出来 到底错哪了
#include<iostream.h>
#include<malloc.h>
#define maxlen 200
typedef char elemtype;
typedef struct sqstack
{elemtype data[maxlen];
int top;
}stack;
void init(stack *st)
{st->top=0;
}
void push(stack *st,elemtype x)
{if(st->top==maxlen-1)
{cout<<"上溢"<<endl;
//return 0;
}
else
{st->top++;
st->data[st->top]=x;
//return 1;
}
}
void pop(stack*st,elemtype *x)
{if(st->top==0)
{cout<<" 下溢"<<endl;
//return 0;
}
else
{*x=st->data[st->top];
st->top--;
//return 1;
}
}
int empty(stack *st)
{if(st->top==0)
return 1;
else return 0;
}
int correct (char *str)
{stack st;
char x;int ok=1;
init (&st);
for(;*str!='\0';str++) // 一个小错误
{switch(*str)
{case '(':push(&st,'(');
case '[': push(&st,'[');
case'{':push(&st ,'{');
case')':pop(&st,&x);if( x!='(')
ok=0;break;
case']':pop(&st,&x);if(x!='[')
ok=0;break;
case'}':pop(&st,&x) ;if(x!='{')
ok=0;break;
}
if(!ok)break;
}
if(empty(&st)&&ok) return 1;
else return 0;}
void main()
{char *str;
str=(char*)malloc(10*sizeof(char));
cout<<"str :";
cin>>str;
if(correct(str)) cout<<"表达式括号匹配"<<endl;
else cout<<"表达式括号不匹配"<<endl;
}
以后要最好加上注释,让人比较好看嘛! |
|