CPP   71
getword
Guest on 30th June 2022 02:46:16 AM


  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<ctype.h>
  4. #define MAX 1100
  5.  
  6. char in[MAX] , check[MAX][200] , *str , word[MAX] ;
  7. char *reserve[32] = { "auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while" } ;
  8. int i , j , check_length = -1 , sort[MAX] , line = 0 , number[MAX][200] = {0} ;
  9. void getword() ;
  10. int search() ;
  11. int main()
  12. {
  13.     while( gets( in ) )
  14.     {
  15.         line++ ;
  16.         for( i = 0 ; in[i] ; i++ )
  17.              if( isalpha( in[i] ) || in[i] == '_' )
  18.              {
  19.                  getword() ;
  20.                  if( search() )
  21.                  {
  22.                      check_length++ ;
  23.                      strcpy( check[ check_length ] , word ) ;
  24.                      number[ check_length ][ 0 ] = 1 ;
  25.                      number[ check_length ][ 1 ] = line ;
  26.                  }
  27.              }
  28.     }
  29.     for( i = 0 ; i<=check_length ; i++ )
  30.         sort[ i ] = i ;
  31.     for( i = 0 ; i<check_length ; i++ )
  32.         for( j = i+1 ; j<=check_length ; j++ )
  33.             if( strcmp( check[ (sort[j]) ] , check[ (sort[i]) ] ) < 0 )
  34.                 sort[ i ] ^= sort[ j ] ^= sort[ i ] ^= sort[ j ] ;
  35.     for( i = 0 ; i<=check_length ; i++ )
  36.     {
  37.         printf( "%s:" , &check[ sort[i] ] ) ;
  38.         for( j = 1 ; j <= number[ sort[i] ][ 0 ] ; j++ )
  39.             printf( " %d" , number[ sort[i] ][j] ) ;
  40.         putchar( '\n' ) ;
  41.     }
  42.     scanf( " " ) ;
  43. }
  44. void getword()
  45. {
  46.     int tmp_length = 1 ;
  47.     word[ 0 ] = in[ i ] ;
  48.     for( ++i ; isalnum( in[ i ] ) || in[i] == '_' ; tmp_length++ , i++ )
  49.         word[ tmp_length ] = in[ i ] ;
  50.     i-- ;
  51.     word[ tmp_length ] = '\0' ;
  52. }
  53.  
  54. int search()
  55. {
  56.     for( j = 0 ; j<32 ; j++ )
  57.         if( !strcmp( word , reserve[j] ) )
  58.             return 0 ;
  59.     for( j = 0 ; j <= check_length ; j++ )
  60.     {
  61.         if( !strcmp( word , check[ j ] ) )
  62.         {
  63.             if( number[ j ][ number[ j ][ 0 ] ] == line )
  64.                 return 0 ;
  65.             ++number[ j ][ 0 ] ;
  66.             number[ j ][ number[ j ][ 0 ] ] = line ;
  67.             return 0 ;
  68.         }
  69.     }
  70.     return 1 ;
  71. }

Raw Paste

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