#include<string.h> #include <malloc.h> #include <stdio.h> #define MaxSize 100 /* Maximum stack size*/ #define OK 1 #define ERROR 0 typedef int Status; typedef char ElemType; typedef int Position; typedef struct { ElemType Data[MaxSize]; /* Array to store elements */ Position top; /* stack top pointer, indicating the position after the top element of the stack */ } SeqStack; Status StackEmpty(SeqStack s); Status Push(SeqStack &L, ElemType e); Status Pop(SeqStack &L, ElemType &e); Status GetPop(SeqStack &L,ElemType &e); int match(SeqStack &L); intmain() { SeqStack L; L.top = 0; if(match(L)) printf("Successful match!\n"); else printf("Match failed!\n"); return 0; } int match(SeqStack &L) { char str[105]; printf("Please enter parentheses:\n"); // getchar(); scanf("%s",str); int len = strlen (str); int i; //printf("%s\n",str); char e = '0'; for(i=0; i<len; i++) { if(str[i]=='['||str[i]=='(') { Push(L,str[i]); } // GetPop(L,e); // printf("%c\n",e); else if(str[i] == ')') { if(GetPop(L,e)) { //printf("%c\n",e); if(e == '(') { //printf("pipei(\n"); Pop(L,e); } else { //printf("2\n"); Push(L,str[i]); } } else Push(L,str[i]); } else if(str[i] == ']') { if(GetPop(L,e)) { if(e == '[') { //printf("pipei[\n"); Pop(L,e); } else { Push(L,str[i]); } } else Push(L,str[i]); } } if(StackEmpty(L)) return 1; else return 0; /*while(!StackEmpty(L)) { Pop(L, e); printf("%c\n", e); }*/ return 0; } Status StackEmpty(SeqStack s) //Determine whether stack s is empty { return s.top == 0; } Status GetPop(SeqStack &L,ElemType &e) { if(StackEmpty(L)) return ERROR; else { e = L.Data[L.top-1]; //printf("shi%cshi",e); } return OK; } Status Push(SeqStack &L, ElemType e) { L.Data[L.top] = e; L.top++; return OK; } Status Pop(SeqStack &L, ElemType &e) { if(StackEmpty(L)) return ERROR; e = L.Data[--L.top]; return OK; }
bracket matching problem
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324824035&siteId=291194637
Recommended
Ranking