Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.51 KB | None | 0 0
  1. //
  2.  
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <memory.h>
  7.  
  8. #define unsigned char bool;
  9.  
  10. const int MaxCount = 255;
  11. FILE* pFile = NULL;
  12. char* Buf;
  13.  
  14. typedef struct
  15. {
  16.     char* s;
  17.     int size, allocated_size;
  18. } string;
  19.  
  20. typedef struct
  21. {
  22.     string* S;
  23.     int size, allocated_size;
  24. } array_of_string;
  25.  
  26. array_of_string Words;
  27.  
  28. void init_s(string* S)
  29. {
  30.     S->size = 0;
  31.     S->allocated_size = 1;
  32.     S->s = (char*)malloc((S->allocated_size + 1) * sizeof(char));
  33.     S->s[0] = '\0';
  34. }
  35.  
  36. void init_as(array_of_string *A)
  37. {
  38.     A->size = 0;
  39.     A->allocated_size = 1;
  40.     A->S = (string*)malloc(A->allocated_size * sizeof(string));
  41. }
  42.  
  43. void push_back_s(string *S, char c)
  44. {
  45.     if (S->size++ == S->allocated_size)
  46.     {
  47.         S->allocated_size *= 2;
  48.         char *big_buf = (char*)malloc((S->allocated_size + 1) * sizeof(char));
  49.         strncpy(big_buf, S->s, S->size + 1);
  50.         free(S->s);
  51.         S->s = big_buf;
  52.     }
  53.     char *curr_word = (char*)malloc(2 * sizeof(char));
  54.     curr_word[0] = c;
  55.     curr_word[1] = '\0';
  56.     strcat(S->s, curr_word);
  57. }
  58.  
  59. void push_back_as(array_of_string *A, string S)
  60. {
  61.     if (A->size++ == A->allocated_size)
  62.     {
  63.         A->allocated_size *= 2;
  64.         string *big_buf = (string*)malloc((A->allocated_size) * sizeof(string));
  65.         memcpy(big_buf, A->S, (A->size - 1) * sizeof(string));
  66.         free(A->S);
  67.         A->S = big_buf;
  68.     }
  69.    
  70.     A->S[A->size - 1] = S;
  71. }
  72.  
  73. bool isAlpha(char c)
  74. {
  75.     return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') || c == '_' || c == '-';
  76. }
  77.  
  78. bool isSpace(char c)
  79. {
  80.     return c == ' ' || c == '\t' || c == '\0' || c == '&' || c == ',' || c == ';' || c == '.';
  81. }
  82.  
  83. bool isApostrophe(char c)
  84. {
  85.     return c == '\'';
  86. }
  87.  
  88. bool isQuotmark(char c)
  89. {
  90.     return c == '"';
  91. }
  92.  
  93. void read_line(const char* fileName)
  94. {
  95.     Buf = (char*)malloc(MaxCount * sizeof(char));
  96.     pFile = fopen(fileName, "r");
  97.     if (pFile != NULL)
  98.     {
  99.         fgets(Buf, MaxCount, pFile);
  100.         fclose(pFile);
  101.     }
  102. }
  103.  
  104. void init_s(char* str)
  105. {
  106.     free(str);
  107.     str = (char*)malloc(MaxCount * sizeof(char));
  108.  
  109. }
  110.  
  111. void parse()
  112. {
  113.     enum { collect, free, apostrophe, quotmark };
  114.  
  115.     char state = free;
  116.     int cnt = 0;
  117.     char ch = Buf[cnt++];
  118.     string curr_word;
  119.  
  120.     while (ch != '\n')
  121.     {
  122.         switch (state)
  123.         {
  124.             case collect: case free:
  125.                 if (isAlpha(ch))
  126.                 {
  127.                     if (state == collect)
  128.                         push_back_s(&curr_word, ch);
  129.                     else
  130.                     {
  131.                         init_s(&curr_word);
  132.                         push_back_s(&curr_word, ch);
  133.                         state = collect;
  134.                     }
  135.                 }
  136.                 else
  137.                 {
  138.                     if (state == collect)
  139.                         push_back_as(&Words, curr_word);
  140.                     state = free;
  141.                     init_s(&curr_word);
  142.                     if (isApostrophe(ch))
  143.                     {
  144.                         state = apostrophe;
  145.                         push_back_s(&curr_word, ch);
  146.                     }
  147.                     else
  148.                         if (isQuotmark(ch))
  149.                         {
  150.                             state = quotmark;
  151.                             push_back_s(&curr_word, ch);
  152.                         }
  153.                         else
  154.                             if (!isSpace(ch))
  155.                             {
  156.                                 push_back_s(&curr_word, ch);
  157.                                 push_back_as(&Words, curr_word);
  158.                                 init_s(&curr_word);
  159.                             }
  160.                 }
  161.                 break;
  162.  
  163.             case apostrophe:
  164.                 push_back_s(&curr_word, ch);
  165.                 if (isApostrophe(ch))
  166.                 {
  167.                     push_back_as(&Words, curr_word);
  168.                     init_s(&curr_word);
  169.                     state = free;
  170.                 }
  171.                 break;
  172.  
  173.             case quotmark:
  174.                 push_back_s(&curr_word, ch);
  175.                 if (isQuotmark(ch))
  176.                 {
  177.                     push_back_as(&Words, curr_word);
  178.                     init_s(&curr_word);
  179.                     state = free;
  180.                 }
  181.                 break;
  182.         }
  183.     }
  184.     if (curr_word.size != 0)
  185.     {
  186.         push_back_as(&Words, curr_word);
  187.         init_s(&curr_word);
  188.     }
  189. }
  190.  
  191. void print_words()
  192. {
  193.     for (int i = 0; i < Words.size; i++)
  194.         printf("%s\n", Words.S[i]);
  195.     printf("\n");
  196. }
  197.  
  198. int main(int argc, const char* argv[])
  199. {
  200.     if (argc > 1)
  201.         fprintf(stderr, "Wrong input!");
  202.  
  203.     read_line(argv[0]);
  204.  
  205.     parse();
  206.  
  207.     print_words();
  208.  
  209.     return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement