一:判断表达式式中左右括号是否匹配出现。
代码:
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 40
typedef struct StackNode{
char data[50];
int top;
}SqStack;
//栈的初始化
SqStack * InitStack(SqStack *S){
SqStack *Stack;
if(Stack=(SqStack *)malloc(sizeof(S))){
Stack->top=-1;
return Stack;
}
return NULL;
}
//判空操作
int StackEmpty(SqStack *S){
if(S->top==-1){
return 1;
}else{
return -1;
}
}
//进栈操作
void Push(SqStack *S,char c){
if(S->top==MaxSize-1){
printf("栈满了");
return ;
}
S->top++;
S->data[S->top]=c;
}
//出栈
char Pop(SqStack *S){
if(S->top==-1){
printf("栈空\n");
return 0;
}
return S->data[S->top--];
}
//判断符号是否为空
int check(SqStack *S,char str[]){
int state=1,i;
char input,e;
for(i=0;i<str[i]!='\0';i++){
input=str[i];
switch(input){
case '(':
Push(S,input);
break;
case '[':
Push(S,input);
break;
case ')':
e=Pop(S);
if(e!='('){
state=0;
}
break;
case ']':
e=Pop(S);
if(e!='['){
state=0;
}
break;
}
if(!state){
break;
}
}
if(StackEmpty(S)&&state==1){
return 1;
}else{
printf("%d",state);
return -1;
}
}
int main(){
char str[40];
int result;
SqStack *S;
S=InitStack(S);
int i;
scanf("%s",&str);
result=check(S,str);
if(result==1){
printf("是的");
}else{
printf("否的");
}
return 0;
}