Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. #define MAXLENGTH 100
  6.  
  7. enum STATES {
  8.     BEGIN,
  9.     ERROR,
  10.     DOT,
  11.     CAPITAL_BEGIN,
  12.     CAPITAL_END,
  13.     SMALL_BEGIN,
  14.     SMALL_END,
  15.     WHITE,
  16.     END
  17. };
  18.  
  19. void stringInsert(char dst[][MAXLENGTH], char *src, int *size ){
  20.     int i;
  21.     int a;
  22.     int enable = 1;
  23.     for (i = 0; i <= *size; i++){
  24.         a = strcmp(dst[i],src);
  25.         if(a == 0)
  26.             enable = 0;
  27.     }
  28.     if(enable == 1){
  29.         strcpy(dst[(*size)+1],src);
  30.         (*size)++;
  31.     }
  32. }
  33.  
  34. void stringPrint(char str[][MAXLENGTH], int size){
  35.     int i;
  36.     printf("%d",size);
  37.     for (i = 0; i <= size; i++){
  38.         printf("\n%s",str[i]);
  39.     }
  40.     printf("\n");
  41. }
  42.  
  43. void skener(){
  44.     enum STATES STATE = BEGIN;
  45.     int c,i,j,k,printX,printY;
  46.     int FirstWord = 1;
  47.     int x = 0;
  48.     int y = 1;
  49.     int f_count = 0;
  50.     int l_count = 0;
  51.     int e_count = 0;
  52.    
  53.     char FIRST[MAXLENGTH][MAXLENGTH];
  54.     char LAST[MAXLENGTH][MAXLENGTH];
  55.     char ELSE[MAXLENGTH][MAXLENGTH];
  56.     char buffer[MAXLENGTH];
  57.     char help_buffer[MAXLENGTH];
  58.    
  59.  
  60.     while(1) {
  61.     switch(STATE){
  62.         case BEGIN:
  63.             c = getc(stdin);
  64.             x++;
  65.             if(c == '.'){
  66.                 STATE = DOT;
  67.                 break;
  68.             }
  69.             else if( isupper(c)){
  70.                 STATE = CAPITAL_BEGIN;
  71.                 printX = x;
  72.                 printY = y;
  73.                 i = 0;
  74.                 k = 0;
  75.                 help_buffer[0] = '\0';
  76.                 break;
  77.             }
  78.             else if ( islower(c) ){
  79.                 STATE = SMALL_BEGIN;
  80.                 printX = x;
  81.                 printY = y;
  82.                 j = 0;
  83.                 k = 0;
  84.                 help_buffer[0] = '\0';
  85.                 break;
  86.             }
  87.             else if ( c == -1){
  88.                 STATE = END;
  89.                 break;
  90.             }
  91.             else if ( c == ' ' || c == '\t' || c == '\n'){
  92.                 STATE = WHITE;
  93.                 break;
  94.             }
  95.             else
  96.                 STATE = ERROR;
  97.             break;
  98.         case CAPITAL_BEGIN :
  99.             buffer[i] = c;
  100.             buffer[i+1] = '\0';
  101.             help_buffer[k] = c;
  102.             help_buffer[k+1] = '\0';
  103.             k++;
  104.             i++;
  105.             c = getc(stdin);
  106.             x++;
  107.             if ( islower(c)){
  108.                 STATE = CAPITAL_BEGIN;
  109.                 break;
  110.             }else{
  111.                 STATE = CAPITAL_END;
  112.                 break;
  113.             }  
  114.         case CAPITAL_END :
  115.             if(FirstWord == 1){
  116.             stringInsert(FIRST, buffer, &f_count );
  117.             }
  118.             if(FirstWord == 0){
  119.             stringInsert(ELSE, buffer, &e_count );
  120.             }
  121.             i = 0;
  122.             printf("\n%s\t_CAPITAL_WORD (%d,%d)",buffer,printX, printY);
  123.             buffer[0] = '\0';
  124.             ungetc(c,stdin);
  125.             x--;
  126.             STATE = BEGIN;
  127.             FirstWord = 0;
  128.             break;
  129.         case SMALL_BEGIN :
  130.             buffer[j] = c;
  131.             buffer[j+1] = '\0';
  132.             j++;
  133.             help_buffer[k] = c;
  134.             help_buffer[k+1] = '\0';
  135.             k++;
  136.             c = getc(stdin);
  137.             x++;
  138.             if ( islower(c)){
  139.                 STATE = SMALL_BEGIN;
  140.                 break;
  141.             }else{
  142.                 STATE = SMALL_END;
  143.                 break;
  144.             }  
  145.         case SMALL_END :
  146.             if(FirstWord == 1){
  147.             stringInsert(FIRST, buffer, &f_count );
  148.             }
  149.             if(FirstWord == 0){
  150.             stringInsert(ELSE, buffer, &e_count );
  151.             }
  152.             j = 0;
  153.             printf("\n%s\t_WORD (%d,%d)",buffer,printX, printY);
  154.             buffer[0] = '\0';
  155.             ungetc(c,stdin);
  156.             x--;
  157.             STATE = BEGIN;
  158.             FirstWord = 0;
  159.             break;
  160.         case DOT :
  161.             --e_count;
  162.             stringInsert(LAST, help_buffer, &l_count );
  163.             FirstWord = 1;
  164.             printf("\n.\t_DOT\t(%d,%d)",x,y);
  165.             STATE = BEGIN;
  166.             break;
  167.         case ERROR :
  168.             printf("\nGRESKA! ZNAK: %c SE NE NALAZI U RECNIKU\t(%d,%d)",c,x,y);
  169.             STATE = BEGIN;
  170.             break;
  171.         case WHITE:
  172.             if( c == '\n' ){
  173.             x = 0;
  174.             y++;
  175.             }
  176.             STATE = BEGIN;
  177.             break;
  178.         case END :
  179.             printf("\nULAZNE RECI:");
  180.             stringPrint(FIRST,f_count);
  181.             printf("\nLAST RECI:");
  182.             stringPrint(LAST,l_count);
  183.             printf("\nELSE RECI:");
  184.             stringPrint(ELSE,e_count);
  185.             return;
  186.     }
  187.        
  188.     }
  189. }
  190.  
  191.  
  192. int main(){
  193.     skener();
  194.     printf("\n");
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement