C   23
int compare
Guest on 20th September 2023 12:17:02 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.     if( !check[ 0 ] )
  44.         for( j = 0 ; j<i ; j++ )
  45.             puts( input[ Index[ j ] ] ) ;
  46.     else
  47.         for( j = i-1 ; j>=0 ; j-- )
  48.             puts( input[ Index[ j ] ] ) ;
  49. }
  50.  
  51. int compare (const void *a, const void *b)
  52. {
  53.     int i , j  ;
  54.     for( i = 0 , j = 0 ; j<=80 && i<=80 ; j++ , i++ )
  55.     {
  56.         while( check[ 2 ] && make[ *( int* )a ][ i ] == ' ' )
  57.             i++ ;
  58.         while( check[ 2 ] && make[ *( int* )b ][ j ] == ' ' )
  59.             j++ ;
  60.         if( make[ *( int* )a ][ i ] == 0 && make[ *( int* )b ][ j ] == 0  )
  61.         {
  62.             if( a<b ) return -1 ;
  63.             else return 1 ;
  64.         }
  65.         else if( make[ *( int* )a ][ i ] == 0 && make[ *( int* )b ][ j ] != 0 )
  66.             return -1 ;
  67.         else if( make[ *( int* )a ][ i ] != 0 && make[ *( int* )b ][ j ] == 0 )
  68.             return 1 ;
  69.         if( check[ 3 ] && make[ *( int* )a ][ i ] >= 97 && isalpha( make[ *( int* )a ][ i ] ) )
  70.             make[ *( int* )a ][ i ] -= 32 ;
  71.         if( check[ 3 ] && make[ *( int* )b ][ j ] >= 97 && isalpha( make[ *( int* )b ][ j ] ) )
  72.             make[ *( int* )b ][ j ] -= 32 ;
  73.         if( make[ *( int* )a ][ i ] < make[ *( int* )b ][ j ] )
  74.             return -1 ;
  75.         else if( make[ *( int* )a ][ i ] > make[ *( int* )b ][ j ] )
  76.             return 1 ;
  77.     }
  78. }

Raw Paste

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