70,037
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#define STACKSIZE 100
#define PATHSIZE 30
#define TABSIZE 4
#define OK 0
#define ERROR 1
#define RETURN(stat)\
{\
getchar();\
return(stat);\
}
struct stack
{
int top;
int array[STACKSIZE];
};
typedef struct stack STACK;
STACK bracket,row,col;
bracket.top = 0;//parse error before `.' ????????????
row.top = 0;//parse error before `.' ????????????
col.top = 0;//parse error before `.' ????????????
void push(STACK stack,int data)
{
if(STACKSIZE == stack.top)
{
printf("The stack is full!\n");
return;
}
stack.array[stack.top++] = data;
}
int pop(STACK stack)
{
if(0 == (stack.top--))
{
printf("The stack is empty!\n");
RETURN(ERROR);
}
return(stack.array[stack.top]);
}
char get_file(char *dest)
{
unsigned int i = 0;
printf("First,input the path of the file you want to open:\n");
if(NULL == fgets(dest,PATHSIZE,stdin))
{
printf("fgets error!\n");
RETURN(ERROR);
}
while('\n' != *(dest + (i++)));
*(dest + i) = '\0';
RETURN(OK);
}
void check_stack(void)
{
int data;
int numCol = 0,numRow = 0;
while(ERROR != (data = pop(bracket)))
{
numCol = pop(col);
numRow = pop(row);
printf("row [%3d] col [%3d] : { not matched!\n\n",numRow,numCol);
}
}
int main(void)
{
char file[PATHSIZE];
char ch = '\0';
unsigned int numRow = 0,numCol = 0;
FILE *fp;
if(ERROR == get_file(file))
{
printf("get_file error!\n");
RETURN(ERROR);
}
if(NULL == (fp = fopen(file,"r")))
{
printf("fopen error: %s!\n",file);
RETURN(ERROR);
}
while(EOF != (ch = fgetc(fp)))
{
if('{' == ch)
{
push(bracket,'{');
push(col,numCol);
push(row,numRow);
}
else if('}' == ch)
{
pop(bracket);
pop(col);
pop(row);
}
else if('\n' == ch)
{
numCol = 0;
numRow++;
}
else if('\t' == ch)
{
numCol += TABSIZE;
continue;
}
numCol++;
}
fclose(fp);
check_stack();
RETURN(OK);
}