分类:
2007-06-07 09:59:49
{[()]} |
{[()]} : RIGHT! |
{[()]} : RIGHT! {[()]} : RIGHT! |
....................... while(feof(fp)){ fscanf(....); } |
....................... |
....................... while(1){ fscanf(....); if(feof(fp)) break; } | ....................... |
#include #include #include #include "Stack.h" #define F_INPUT "input.txt" #define M_INPUT "r" #define MAXBUFSIZE 80 int main() { FILE *fp; Stack *s; char buf[MAXBUFSIZE], c, k; int i, flag; if((fp = fopen(F_INPUT, M_INPUT)) == NULL){ printf("Can not open input file\n"); return 1; } if( (s = (Stack *)malloc(sizeof(Stack))) == NULL){ printf("Fail to stack allocation\n"); return 1; } initStack(s); while(1){ flag = 0; fscanf(fp, "%s", buf); if(feof(fp) || ferror(fp)) break; //printf("%s\n", buf); for(i = 0;i < strlen(buf);i++){ c = buf[i]; if(c == '{' || c == '[' || c == '(') pushStack(s, c); else if(c == '}' || c == ']' || c == ')'){ if(emptyStack(s)){ flag = 1; break; } popStack(s, &k); //printf("k = %c\n", k); if((c == '}' && k == '{') || (c == ']' && k == '[') || (c == ')' || k == '(')) continue; else{ flag = 1; break; } } } if(!emptyStack(s)) flag = 1; if(flag) printf("%s : ERROR!\n", buf); else printf("%s : RIGHT!\n", buf); } fclose(fp); return 0; } |