标签:
#include<stdio.h> #include<string.h> #define MaxSize 100 void ExpIsCorrect(char exp[],int n); typedef char DataType; typedef struct { DataType stack[MaxSize]; int top; }SeqStack; void StackInitiate(SeqStack *s)//初始化 { s->top=0; } int StackNotEmpty(SeqStack s)//非空否 { if(s.top<= 0) return 0; else return 1; } int StackPush(SeqStack *s,DataType x)//入栈 { if(s->top>=MaxSize) { return 0; } else { s->stack[s->top]=x; s->top++; return 1; } } int StackPop(SeqStack *s,DataType *d)//出栈 { if(s->top<=0) { return 0; } else { s->top--; *d=s->stack[s->top]; return 1; } } int StackTop(SeqStack s,DataType *d)//取栈顶数据 { if(s.top<=0) { return 0; } else { *d=s.stack[s.top-1]; return 1; } } main(void) { SeqStack l; int n; char a[100]; scanf("%s",a); n=strlen(a); ExpIsCorrect(a,n); } void ExpIsCorrect(char exp[],int n) { SeqStack mystack; int i; char c; StackInitiate(&mystack); for(i=0;i<n;i++) { if((exp[i]==‘(‘)||(exp[i]==‘{‘)||(exp[i]==‘[‘)) StackPush(&mystack,exp[i]); else if(exp[i]==‘)‘&&StackNotEmpty(mystack)&&StackTop(mystack,&c)&&c==‘(‘) StackPop(&mystack,&c); else if(exp[i]==‘]‘&&StackNotEmpty(mystack)&&StackTop(mystack,&c)&&c==‘[‘) StackPop(&mystack,&c); else if(exp[i]==‘}‘&&StackNotEmpty(mystack)&&StackTop(mystack,&c)&&c==‘{‘) StackPop(&mystack,&c); else if(exp[i]==‘)‘&&StackNotEmpty(mystack)&&StackTop(mystack,&c)&&c!=‘(‘) { printf("error\n"); return; } else if(exp[i]==‘}‘&&StackNotEmpty(mystack)&&StackTop(mystack,&c)&&c!=‘{‘) { printf("error\n"); return; } else if(exp[i]==‘]‘&&StackNotEmpty(mystack)&&StackTop(mystack,&c)&&c!=‘[‘) { printf("error\n"); return; } else if((exp[i]==‘)‘)||(exp[i]==‘]‘)||(exp[i]==‘}‘)&&!StackNotEmpty(mystack)) { printf("1"); return; } } if(StackNotEmpty(mystack)) printf("-1"); else printf("right"); }
标签:
原文地址:http://www.cnblogs.com/yc721274287/p/4438365.html