study/C_C++/sources/c_and_pointer/chapt2/practise/check.c

45 lines
811 B
C

#include <stdio.h>
#include <stdlib.h>
int main ()
{
char str[20];
printf("输入文件名:");
gets(str);
FILE *fp = fopen(str, "r");
if (fp == NULL )
{
printf("file open failed\n");
return 1;
}
char buff[255];
fgets(buff, 255, fp);
fclose(fp);
//printf("\n%s\n", buff);
int i = 0;
int left = 0, right = 0;
for ( i = 0 ; i < 255; i++ )
{
if ( buff[i] == '\0' || left < right )
{
break;
}
if ( buff[i] == '{' )
{
left ++;
}else if ( buff[i] == '}' )
{
right ++;
}
}
if ( left == right )
{
printf("花括号成对出现\n");
}
else
{
printf("花括号不成对出现\n");
}
return 0;
}