- #include<stdio.h>
- #include<stdlib.h>
- #include<stdarg.h>
- #include<string.h>
- #include<ctype.h>
- int check[ 4 ] = {0} , Index[90] ;
- char file_name[50] , input[ 10000 ][ 90 ] , make[ 10000 ][ 90 ] ;
- int compare (const void *a, const void *b) ;
- int main( int argc , char *argv[] )
- {
- int i , j ;
- freopen( argv[ argc-1 ] , "r" , stdin ) ;
- for( i = 1 ; i<argc-1 ; i++ )
- {
- if( strcmp( argv[i] , "-asc" ) == 0 )
- check[ 0 ] = 0 ;
- else if( strcmp( argv[i] , "-des" ) == 0 )
- check[ 0 ] = 1 ;
- else if( strcmp( argv[i] , "-o" ) == 0 )
- {
- check[ 1 ] = 1 ;
- strcpy( file_name , argv[ ++i ] ) ;
- }
- else if( strcmp( argv[i] , "-nowhitespace" ) == 0 )
- check[ 2 ] = 1 ;
- else if( strcmp( argv[i] , "-ignorecase" ) == 0 )
- check[ 3 ] = 1 ;
- }
- i = 0 ;
- while( gets( input[ i ] ) )
- {
- strcpy( make[i] , input[i]) ;
- Index[ i ] = i ;
- i++ ;
- }
- qsort( Index , i , sizeof( int ) , compare ) ;
- if( check[ 1 ] )
- freopen( file_name , "w" , stdout ) ;
- if( !check[ 0 ] )
- for( j = 0 ; j<i ; j++ )
- puts( input[ Index[ j ] ] ) ;
- else
- for( j = i-1 ; j>=0 ; j-- )
- puts( input[ Index[ j ] ] ) ;
- }
- int compare (const void *a, const void *b)
- {
- int i , j ;
- for( i = 0 , j = 0 ; j<=80 && i<=80 ; j++ , i++ )
- {
- while( check[ 2 ] && make[ *( int* )a ][ i ] == ' ' )
- i++ ;
- while( check[ 2 ] && make[ *( int* )b ][ j ] == ' ' )
- j++ ;
- if( make[ *( int* )a ][ i ] == 0 && make[ *( int* )b ][ j ] == 0 )
- {
- if( a<b ) return -1 ;
- else return 1 ;
- }
- else if( make[ *( int* )a ][ i ] == 0 && make[ *( int* )b ][ j ] != 0 )
- return -1 ;
- else if( make[ *( int* )a ][ i ] != 0 && make[ *( int* )b ][ j ] == 0 )
- return 1 ;
- if( check[ 3 ] && make[ *( int* )a ][ i ] >= 97 && isalpha( make[ *( int* )a ][ i ] ) )
- make[ *( int* )a ][ i ] -= 32 ;
- if( check[ 3 ] && make[ *( int* )b ][ j ] >= 97 && isalpha( make[ *( int* )b ][ j ] ) )
- make[ *( int* )b ][ j ] -= 32 ;
- if( make[ *( int* )a ][ i ] < make[ *( int* )b ][ j ] )
- return -1 ;
- else if( make[ *( int* )a ][ i ] > make[ *( int* )b ][ j ] )
- return 1 ;
- }
- }
Raw Paste