利用栈规则做就近括号匹配
从第一个字符开始扫描
当遇见普通字符时忽略,
当遇见左括号时压入栈中
当遇见右括号时从栈中弹出栈顶符号,并进行匹配
匹配成功:继续读入下一个字符
匹配失败:立即停止,并报错
结束:
成功: 所有字符扫描完毕,且栈为空
失败:匹配失败或所有字符扫描完毕但栈非空
FILE1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #ifndef _SEQSTACK_H #define _SEQSTACK_H
#include<stdio.h> #include<string.h> #include<stdlib.h> #include <stdbool.h>
#define MAX 1024
struct SStack { void *data[MAX]; int m_Size; };
typedef void *SeqStack;
SeqStack init_SeqStack();
void push_SeqStack(SeqStack stack, void *data);
void pop_SeqStack(SeqStack stack);
void *top_SeqStack(SeqStack stack);
int size_SeqStack(SeqStack stack);
bool isEmpty_SeqStack(SeqStack stack);
void destroy_SeqStack(SeqStack stack);
#endif
|
FILE2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include<stdio.h> #include<string.h> #include<stdlib.h> #include "SeqStack.h"
SeqStack init_SeqStack() { struct SStack *myStack = malloc(sizeof(struct SStack));
if (myStack == NULL) { return NULL; }
memset(myStack->data, 0, sizeof(void *) * MAX);
myStack->m_Size = 0;
return myStack; }
void push_SeqStack(SeqStack stack, void *data) { if (stack == NULL) { return; } if (data == NULL) { return; }
struct SStack *mystack = stack; if (mystack->m_Size == MAX) { return; }
mystack->data[mystack->m_Size] = data;
mystack->m_Size++; }
void pop_SeqStack(SeqStack stack) { if (stack == NULL) { return; }
struct SStack *mystack = stack;
if (mystack->m_Size == 0) { return; }
mystack->data[mystack->m_Size - 1] = NULL;
mystack->m_Size--;
}
void *top_SeqStack(SeqStack stack) { if (stack == NULL) { return NULL; }
struct SStack *mystack = stack;
if (mystack->m_Size == 0) { return NULL; } return mystack->data[mystack->m_Size - 1]; }
int size_SeqStack(SeqStack stack) { if (stack == NULL) { return -1; }
struct SStack *mystack = stack;
return mystack->m_Size;
}
bool isEmpty_SeqStack(SeqStack stack) { if (stack == NULL) { return true; }
struct SStack *mystack = stack;
if (mystack->m_Size == 0) { return true; }
return false;
}
void destroy_SeqStack(SeqStack stack) { if (stack == NULL) { return; }
free(stack); stack = NULL; }
|
FILE3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| int isLeft(char ch) { return ch == '('; }
int isRight(char ch) { return ch == ')'; }
static void test01() {
char *str = "123*(was)s(sd)sdb(fs())())))(()";
char *p = str;
SeqStack *stack = init_SeqStack();
while (*p != '\0') {
if (isLeft(*p)) { push_SeqStack(stack, p); } else if (isRight(*p)) { if (size_SeqStack(stack) > 0) { pop_SeqStack(stack); } else { error(str, "Error!", p);
break; } }
p++; }
while (size_SeqStack(stack) > 0) {
error(str, "Error", top_SeqStack(stack)); pop_SeqStack(stack); }
destroy_SeqStack(stack);
stack = NULL;
bool b = isEmpty_SeqStack(stack);
printf("\n=-=-=-=-=-=-=-====-=-=\n%d\n",b);
}
static void error(char *str, char *string, const char *p) {
printf("\n%s\n", string);
printf("%s\n", str);
int num = (int) (p - str);
for (int i = 0; i < num; i++) { printf(" "); }
printf("I");
}
int main(int argc, char *argv[]) {
test01();
bool b = true;
printf("%d\n", b);
return 0; }
|