Advertisement
kolesnikov

Untitled

Nov 7th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. #include <stdio.h>
  2. int k; // tyle razy keyword
  3. int p; // licznik nawiasow
  4. int f; // flaga do wykrywania nawiasow
  5. int i, j, h; // iterator dla petli
  6. int match; // flaga 1 gdy sie zgadza slowo
  7. int wordlength; // dlugosc slowa, licz od 1
  8. int tmp;
  9. int smiec;
  10. char c; // bierzacy char
  11. char prev; // poprzedni znak wczytany
  12. char last; // ostatni zapisany znak
  13. char next; // nastepny znaczek
  14.  
  15. char word[10];
  16. char pword[10];
  17.  
  18. int main (int narg, char *arg[])
  19. {
  20.     FILE* in = fopen (arg[1], "rt");
  21.     FILE* out = fopen (arg[2], "wt");
  22.     strcpy (word, arg[3]); // wkleja do word keyword
  23.     wordlength=strlen(word);
  24.     last=' ';
  25.    
  26.     for (i = 0; word[i] != '\0'; i++) { // podmienia slowo kluczowe
  27.         if(isdigit(word[i]))
  28.         {
  29.             if(word[i]=='0') pword[i] = '9';
  30.             else pword[i]=word[i]-1;
  31.         } else
  32.         {
  33.             pword[i]=tolower(word[i]);
  34.         }
  35.     }
  36.     pword[i]='\n';
  37.    
  38.     do {
  39.         f = 1;
  40.         c = fgetc(in);
  41.         if (c == '*') { // jesli komentarz sie konczy
  42.        
  43.             tmp = fgetc(in);
  44.             if(tmp == '/'){
  45.                 p--;
  46.                 c = ' ';
  47.                 f = 0;
  48.             } else ungetc(tmp, in);
  49.         }
  50.         if (c == '/') { // jesli komentarz sie zaczyna
  51.             tmp = fgetc(in);
  52.             if(tmp == '*'){
  53.                 c = tmp;
  54.                 p++;
  55.                 f = 0;
  56.             } else ungetc(tmp, in);
  57.         }
  58.        
  59.         if ((p == 0)&&(f == 1)) // ten kod wykona sie poza komentarzem
  60.         {
  61.             if (isblank(last) && c==word[0]){
  62.                 //printf("tutaj");
  63.                 j=0;
  64.                 while(1)
  65.                 {  
  66.                    
  67.                     if (c!=word[j]) break;
  68.                     c = fgetc(in);
  69.                     j++;
  70.                     if (c == EOF) break;
  71.                     //printf("%d", j);
  72.                 }
  73.                 if (j==wordlength){
  74.                     //fscanf(in, "%c", &tmp);
  75.                     //next = tmp;
  76.                     //ungetc(tmp, in);
  77.                     if ((!(isalnum(c)))||c==EOF){
  78.                         match = 1;
  79.                         k++;
  80.                     }
  81.                    
  82.                 } else match = 0;
  83.                
  84.                 if (match){
  85.                     for (i=0;i<j;i++) {
  86.                         putc(word[i], out);
  87.                     }
  88.                 } else {
  89.                     for (i=0;i<j;i++) {
  90.                    
  91.                         putc(pword[i], out);
  92.  
  93.                     }
  94.                 }
  95.             }
  96.  
  97.            
  98.             if (isdigit(c)){if(c=='0') c='9'; else c--;}
  99.             if (isupper(c)){c=tolower(c);}
  100.             if (c!=EOF) {putc(c, out);}
  101.             last = c;
  102.             if(ispunct(c)) last = ' '; //??
  103.             //printf("%d ",c);
  104.         }
  105.        
  106.        
  107.         prev = c;
  108.     } while (c!=EOF);
  109.  
  110.     printf ("%d",k);
  111.     fclose (in);
  112.     fclose (out);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement