用栈实现括号配对!!
#include<stdio.h>#include<stdlib.h>
#include<Stack>
int MatchPairs(char *expr)
{
Stack s;
char ch;
int i,j;
InitStack(&s);
for(i=0;i<100;i++)
{
ch=expr[i];
switch(ch)
{
case '(':
case '[':Push (&s,ch);break;
case ')':
GetTop(s,&ch);
if(ch=='(')
{
j=Pop(&s);
if(j==0) return 0;
}
else return 0;
break;
case ']':
GetTop(s,&ch);
if(ch=='[')
{
j=Pop(&s);
if(j==0) return 0;
}
else return 0;
break;
}
}
if(!StackEmpty(s))
return 0;
return 1;
}
void main()
{
char expr[80];
int flag;
printf("input expression\n");
gets(expr);
flag=MatchPairs(expr);
if (flag) printf("Matched.\n");
else printf("Not Matched.\n");
}
编译结果:1>e:\vc6.0实验\括号配对\括号配对\源.cpp(6): error C2065: “Stack”: 未声明的标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(6): error C2146: 语法错误: 缺少“;”(在标识符“s”的前面)
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(6): error C2065: “s”: 未声明的标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(9): error C2065: “s”: 未声明的标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(9): error C3861: “InitStack”: 找不到标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(16): error C2065: “s”: 未声明的标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(16): error C3861: “Push”: 找不到标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(18): error C2065: “s”: 未声明的标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(18): error C3861: “GetTop”: 找不到标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(21): error C2065: “s”: 未声明的标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(21): error C3861: “Pop”: 找不到标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(27): error C2065: “s”: 未声明的标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(27): error C3861: “GetTop”: 找不到标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(30): error C2065: “s”: 未声明的标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(30): error C3861: “Pop”: 找不到标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(37): error C2065: “s”: 未声明的标识符
1>e:\vc6.0实验\括号配对\括号配对\源.cpp(37): error C3861: “StackEmpty”: 找不到标识符
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========