| Код | #include<ctype.h> #include<conio.h>
#include<stdio.h> #include<string.h>
#include<stdlib.h>
FILE *in;
//баланс скобок
long balance( int bracket ) { int i, FLAG=-1, opposit; long curpos=0, retcurpos=0; curpos=ftell( in );
if( bracket==']' ) opposit='['; else if( bracket=='[' ) opposit=']';
else if( bracket=='(' ) opposit=')'; else if( bracket==')' ) opposit='(';
else if( bracket=='{' ) opposit='}'; else if( bracket=='}' ) opposit='{';
else if( bracket=='|' ) opposit='|';
fseek( in, -1L, SEEK_CUR ); for( i=curpos; i>=0; i-- ) { if( fgetc(in)==bracket ) { FLAG=0; break; } fseek( in, -3L, SEEK_CUR ); }
if( FLAG==0 ) for( ; !feof(in); ) if( fgetc(in)==opposit || feof(in) ) { FLAG=1; break; }
clearerr( in ); fseek( in, -1L, SEEK_CUR );
fseek( in, -1L, SEEK_CUR ); retcurpos=ftell(in); fseek( in, curpos, SEEK_SET );
/* FLAGS-value -1-ошибка(ненайдена даже первая скобка) 0-найдена первая скобка но не найдена противоположная скобка 1-баланс соблюден */ return( FLAG<1 ) ? FLAG : retcurpos; }
void block_where( long startblock, long endblock ) { char *tmp, *p; int ch, j, c,i; long curpos; curpos=ftell( in );
tmp=new char[ endblock-startblock ];
fseek( in, startblock, SEEK_SET ); for( i=0; !feof(in); ) if( (tmp[ i ]=fgetc(in))!=EOF && ftell(in)<endblock && !feof(in) ) ++i; tmp[ i ]='\0';
printf( "%s\n", tmp );
delete [] tmp; clearerr( in ); fseek( in, endblock, SEEK_SET ); return; }
// -|-||-||MAIN||-||-|-
void main( ) { char buf[ 100 ]; int i, j, c, x;
in=fopen( ".\\Dummy.$$$", "r" );
if( !in ) exit( 1 );
//ЧТЕНИЕ БЛОКОВ fseek( in, 0L, SEEK_SET ); for( i=0; fgets(buf, 100, in); i++ ) { for( j=0; j<(signed)strlen( buf ); j++ ) { if( buf[ j ]=='{' && ((x=balance('{')))>0 ) block_where( ftell(in), x ); } }
fcloseall(); return; }
|
M Guedda | Модератор: Пользуйтесь кнопкой "Код!" |
|