Advertisement
Guest User

Untitled

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