| 检测程序符号平衡的程序,我用了一天的时间才搞好的。目前比较简单 只能检测()[]{}的符号缺失。 #include <stdio.h> #include <stdlib.h> struct sta{ char a; struct sta *next; }; push(char c, struct sta **pp) { struct sta *new; new = malloc(sizeof(struct sta)); new->a = c; new->next = *pp; *pp = new; } pop(char c, struct sta **pp) { switch(c) { case ')': if((*pp)->a != '(') printf("error! lost \" %c... \"\n",(*pp)->a); break; case ']': if((*pp)->a != '[') printf("error! lost \" %c... \"\n",(*pp)->a); break; case '}': if((*pp)->a != '{') printf("error! lost \" %c... \"\n",(*pp)->a); break; } *pp = (*pp)->next; } main() { char c; struct sta *p; p = malloc(sizeof(struct sta)); p->next = NULL; while((c = getc(stdin)) != EOF) { if(c == '(') { push(c, &p); } else if(c == ')') { pop(c, &p); } if(c == '[') { push(c, &p); } else if(c == ']') { pop(c, &p); } if(c == '{') { push(c, &p); } else if(c == '}') { pop(c, &p); } } }
|