非常实用的word文本括号匹配是否正确的code,可按照此思路扩展到python处理大批量文本文件的检错中,非常实用!!!
原理:
栈
代码:
#include <stdio.h>
#include <stdlib.h>
#include"stack.h"
int togetherSymbol(char a, char b) {//符号括号对if (a == '(' && b == ')' || a == '[' && b == ']' || a == '{' && b == '}')return 1;return 0;//不是符号对,返回0
}
int isProper(char *a,int n) {//括号匹配检查函数stack Stack;init(Stack);for (int i = 0; i < n; i++) {if (a[i] == '(' || a[i] == '[' || a[i] == '{') {push(Stack, a[i]);//入栈}else {if (!empty(Stack)) {//不为空char temp;pop(Stack, temp);int situation = togetherSymbol(temp, a[i]);if (situation == 0) {//左右括号不匹配return 0;}}else {return 0;//右括号有多}}}if (empty(Stack))return 1;return 0;//左括号有多
}
int main() {char kuo[10] = "(([])){}";if (isProper(kuo,8)) {//括号搭配成功!printf("括号搭配成功!\n");}else {printf("括号搭配失败!\n");}system("pause");return 0;
}