1、从文件peidui.txt按字符读取
2、如果是 ( [ { 则入栈
3、如果是) ] },如果栈空错误,否则弹出,如果弹出元素不配对,错误
4、文件读取结束,如果栈非空,错误。
代码如下:
#include
#include
typedef int ElementType;
#include "_Stack.h"
#include "_Stack.c"
#define SMALL_LEFT ( 40 )
#define SMALL_RIGHT ( 41 )
#define MEDIUM_LEFT ( 91 )
#define MEDIUM_RIGHT ( 93 )
#define BIG_LEFT ( 123 )
#define BIG_RIGHT ( 125 )
void PrintError( int line, int column, int type );
void PrintError( int line, int column, int type )
{
if( type == 1 )
printf( "Unexpected start on line %d, char %d", line, column );
if( type == 2 )
printf( "Unexpected end on line %d, char %d", line, column );
}
void Error(char* ErrorMsg)
{
printf("%s", ErrorMsg);
exit(1);
}
void FatalError(char* ErrorMsg)
{
printf("%s", ErrorMsg);
exit(2);
}
int main()
{
FILE *fp;
Stack S;
int line = 1, column = 0;
ElementType c, top;
if( ( fp = fopen( "peidui.txt", "r" ) ) == NULL )
{
printf( "Cannot open this file.\n" );
return 0;
}
S = CreateStack();
while( !feof ( fp ) )
{
if( c == '\n' )
{
++line;
column = 0;
}
else
++column;
c = fgetc( fp );
if( c == '\n' )
++line;
if( c == SMALL_LEFT || c == MEDIUM_LEFT || c == BIG_LEFT )
{
Push( c, S );
}
if( c == SMALL_RIGHT )
{
if( IsEmpty( S ) )
PrintError( line, column, 1 );
else
{
top = Top( S );
if( top != SMALL_LEFT )
{
PrintError( line, column, 2 );
return 0;
}
}
}
if( c == MEDIUM_RIGHT )
{
if( IsEmpty( S ) )
PrintError( line, column, 1 );
else
{
top = Top( S );
if( top != MEDIUM_LEFT )
{
PrintError( line, column, 2 );
return 0;
}
}
}
if( c == BIG_RIGHT )
{
if( IsEmpty( S ) )
PrintError( line, column, 1 );
else
{
top = Top( S );
if( top != BIG_LEFT )
{
PrintError( line, column, 2 );
return 0;
}
}
}
}
if( !IsEmpty( S ) )
PrintError( line, column, 2 );
return 0;
}