棧的順序儲存 括號匹配
#include "strack.h" int main() { char e; char a; StrackPtr s = InitStrack();//初始化棧 int count = 0; char string[50] = {0}; gets(string); int n = strlen(string); //printf("%d", n); while (count < n) { if (string[count] == '(' || string[count] == ')' || string[count] == '[' || string[count] == ']' || string[count] == '{' || string[count] == '}') { switch (string[count]) { case '(': PushStrack(s, string[count]); //puts("111"); break; case '[': PushStrack(s, string[count]); break; case '{': PushStrack(s, string[count]); break; case ')': if (!StrackEmpty(s)) { GetTop(s, &e); if (e == '(') { Pop(s, &a); } } break; case ']': if (!StrackEmpty(s)) { GetTop(s, &e); if (e == '[') { Pop(s, &a); } } break; case '}': if (!StrackEmpty(s)) { GetTop(s, &e); if (e == '{') { Pop(s, &a); } } break; defaule: break; } } count++; } if (StrackEmpty(s)) { puts("匹配成功"); } else { puts("匹配失敗"); } system("pause"); return 0; }