Advertisement
dimon2242

Untitled

Apr 8th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. /* main.c */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. #define SIZEB 3
  7. #define CBLOCKOUTPUT 4
  8.  
  9. struct storage {
  10.     char currentCh;
  11.     char hyphen;
  12.     char additionalSymb;
  13.     char lastCh;
  14.     unsigned int hyphenStatus, lastChStatus, additionalSymbStatus, status;
  15. } symb;
  16.  
  17. void resetCh(void);
  18. void addCh(char *ch);
  19. int writeGroup(char *ptrSym, char *ptrSym2, char *ptrSym3);
  20.  
  21. int main(void) {
  22.     symb.additionalSymbStatus = symb.hyphenStatus = symb.lastChStatus = symb.status = 0;
  23.     FILE *fp;
  24.     if((fp = fopen("referat.txt","r"))==NULL) {
  25.         printf("Can't open file!\n");
  26.         return -1;
  27.     }
  28.     char buffer[SIZEB], ch;
  29.     char *buff = buffer;
  30.     int i;
  31.     do {
  32.         for(i=0;i<SIZEB;++i) {
  33.             ch = getc(fp);
  34.             if(feof(fp)) {
  35.                 *(buff+i) = '\0';
  36.                 break;
  37.             } else
  38.                 *(buff+i) = ch;
  39.         }
  40.         for(i=0;i<SIZEB;++i) {
  41.             if(isalpha(*(buff)) && *(buff+1)=='-' && isalpha(*(buff+2))) {
  42.                 printf("%c %c %c\n", *buff, *(buff+1), *(buff+2));
  43.                 if(writeGroup(buff, (buff+1), (buff+2))) printf("Error writeGroup call point\n");
  44.                 resetCh();
  45.                 break;
  46.             }
  47.             if(symb.status) {
  48.                 if(symb.lastChStatus) {
  49.                     if(*buff == '-') {
  50.                         if(isalpha(*(buff+1))) {
  51.                             printf("%c %c %c\n",symb.lastCh, *buff, *(buff+1));
  52.                             if(writeGroup(&symb.lastCh, buff, (buff+1))) printf("Error writeGroup call point\n");
  53.                             resetCh();
  54.                             break;
  55.                         }
  56.                     }
  57.                 }
  58.                 if(symb.hyphenStatus) {
  59.                     if(symb.additionalSymbStatus) {
  60.                         if(isalpha(*(buff))) {
  61.                             printf("%c %c %c\n", symb.additionalSymb, symb.hyphen, *buff);
  62.                             if(writeGroup(&symb.additionalSymb, &symb.hyphen, buff))  printf("Error writeGroup call point\n");
  63.                             resetCh();
  64.                             break;
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.             addCh(buff);
  71.     } while(!feof(fp));
  72. fclose(fp);
  73. return 0;
  74. }
  75.  
  76. int writeGroup(char *ptrSym, char *ptrSym2, char *ptrSym3) {
  77.     FILE *fpwrite;
  78.     char *ptrArr[3] = {ptrSym, ptrSym2, ptrSym3};
  79.     char str1[5];
  80.     int i;
  81.     for(i=0;i<3;++i) {
  82.         str1[i] = *ptrArr[i];
  83.     }
  84.     str1[3] = '\n';
  85.     str1[4] = '\0';
  86.     if((fpwrite = fopen("output.txt","a"))==NULL) {
  87.         printf("Error open file for writing!\n");
  88.         return -1;
  89.     }
  90.     if(fwrite(str1, sizeof(char), CBLOCKOUTPUT, fpwrite)!=CBLOCKOUTPUT) {
  91.             printf("End write\n");
  92.             fclose(fpwrite);
  93.             return -1;
  94.         }
  95.     fclose(fpwrite);
  96.     return 0;
  97. }
  98.  
  99. void resetCh(void) {
  100.     symb.additionalSymbStatus = symb.hyphenStatus = symb.lastChStatus = symb.status = 0;
  101. }
  102. void addCh(char *buff) {
  103.     if(*(buff+(SIZEB-1))=='-') {
  104.         symb.hyphen = *(buff+(SIZEB-1));
  105.         if(isalpha(*(buff+(SIZEB-2)))) {
  106.             symb.additionalSymb = *(buff+((SIZEB-2)));
  107.             symb.additionalSymbStatus = 1;
  108.             symb.hyphenStatus = 1;
  109.             symb.lastChStatus = 0;
  110.             symb.status = 1;
  111.         }
  112.         else
  113.             symb.additionalSymbStatus = 0;
  114.     }
  115.     if(isalpha(*(buff+(SIZEB-1)))) {
  116.         symb.lastCh = *(buff+(SIZEB-1));
  117.         symb.hyphenStatus = 0;
  118.         symb.lastChStatus = 1;
  119.         symb.status = 1;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement