代码表示运行时错误
#include
#include<stdlib.h>
#define MaxSize 50
using namespace std;
typedef struct Stack
{
char date[MaxSize];
int top;
}Stack;
//初始化栈
void InitStack(Stack s)
{
s->top=0;
}
int topStack(Stack s){
return s->top;
}
int StackEmpty(Stack* s)
{
// assert(s != NULL);
if (0 == s->top)
return 1;
else
return 0;
}
void push(Stack* p,char item){
if(p->top==MaxSize-1){
printf("false");
}
else {
p->date[++(p->top)]=item;
return;
}
}
char pop(Stack* p){
if(p->top==-1) printf("false");
else {
return (p->date[(p->top)--]);
}
}
int main(){
char c[50],ch;
int i=0,t;
Stack s;
scanf("%c",&c[i]);
while(c[i]!='\n'){
i++;
scanf("%c",&c[i]);
}
t=i;
for(i=0;i<t;i++){
ch=c[i];
if((ch=='(')||(ch=='[')||ch=='{'){
push(&s,ch);
continue;
}
else if((ch==')')||(ch==']')||ch=='}'){
if(-1==topStack(&s)){
printf("false");
return 0;
}
else {
char sh=pop(&s);
if (((')' == c[i]) && ('(' == sh)) ||
((']' == c[i]) && ('[' == sh)) ||
(('}' == c[i]) && ('{' == sh))){
continue;
}
else {
printf("false");
return 0;
}
}
}
}
if(!StackEmpty(&s))
{
printf("false");
}
else {
printf("true");
}
return 0;
}
wow,你不嫌麻烦啊,手写一个栈,直接用STL的stack不好吗
其它有没有错不知道,但是这个stack就有问题,你初始化top是0,然后0并没有用,但是pop的时候,检查边界值确实-1,这妥妥的不协调啊。