C   30
check index
Guest on 11th February 2023 01:15:59 AM


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdarg.h>
  4. #include<string.h>
  5. #include<ctype.h>
  6.  
  7. int check[ 4 ] = {0} , Index[90] ;
  8. char file_name[50] , input[ 10000 ][ 90 ] , make[ 10000 ][ 90 ] ;
  9.  
  10. int compare (const void *a, const void *b) ;
  11.  
  12. int main( int argc , char *argv[] )
  13. {
  14.     int i , j ;
  15.     freopen( argv[ argc-1 ] , "r" , stdin ) ;
  16.     for( i = 1 ; i<argc-1 ; i++ )
  17.     {
  18.         if( strcmp( argv[i] , "-asc" ) == 0 )
  19.             check[ 0 ] = 0 ;
  20.         else if( strcmp( argv[i] , "-des" ) == 0 )
  21.             check[ 0 ] = 1 ;
  22.         else if( strcmp( argv[i] , "-o" ) == 0 )
  23.         {
  24.             check[ 1 ] = 1 ;
  25.             strcpy( file_name , argv[ ++i ] ) ;
  26.         }
  27.         else if( strcmp( argv[i] , "-nowhitespace" ) == 0 )
  28.             check[ 2 ] = 1 ;
  29.         else if( strcmp( argv[i] , "-ignorecase" ) == 0 )
  30.             check[ 3 ] = 1 ;
  31.     }
  32.        
  33.     i = 0 ;
  34.     while( gets( input[ i ] ) )
  35.     {
  36.         strcpy( make[i] , input[i]) ;
  37.         Index[ i ] = i ;
  38.         i++ ;
  39.     }
  40.     qsort( Index , i , sizeof( int ) , compare ) ;
  41.     if( check[ 1 ] )
  42.         freopen( file_name , "w" , stdout ) ;
  43.     for( j = 0 ; j<i ; j++ )
  44.         puts( input[ Index[ j ] ] ) ;
  45.  
  46. }
  47.  
  48. int compare (const void *a, const void *b)
  49. {
  50.     int i , j  , order = 1 , a_value = *( int* )a, b_value = *( int* )b;
  51.     if( check[0] ) order = -1 ;
  52.     for( i = 0 , j = 0 ; j<=80 && i<=80 ; j++ , i++ )
  53.     {
  54.         while( check[ 2 ] && make[ a_value ][ i ] == ' ' )
  55.             i++ ;
  56.         while( check[ 2 ] && make[ b_value ][ j ] == ' ' )
  57.             j++ ;
  58.         if( make[ a_value ][ i ] == 0 && make[ b_value ][ j ] == 0  )
  59.         {
  60.             if( a_value<b_value ) return -1*order ;
  61.             else return 1 *order;
  62.         }
  63.         else if( make[ a_value ][ i ] == 0 && make[ b_value ][ j ] != 0 )
  64.             return -1 *order;
  65.         else if( make[ a_value ][ i ] != 0 && make[ b_value ][ j ] == 0 )
  66.             return 1 *order;
  67.         if( check[ 3 ] && make[ a_value ][ i ] >= 97 && isalpha( make[ a_value ][ i ] ) )
  68.             make[ a_value ][ i ] -= 32 ;
  69.         if( check[ 3 ] && make[ b_value ][ j ] >= 97 && isalpha( make[ b_value ][ j ] ) )
  70.             make[ b_value ][ j ] -= 32 ;
  71.         if( make[ a_value ][ i ] < make[ b_value ][ j ] )
  72.             return -1 *order;
  73.         else if( make[ a_value ][ i ] > make[ b_value ][ j ] )
  74.             return 1 *order;
  75.     }
  76. }

Raw Paste

Login or Register to edit or fork this paste. It's free.