Advertisement
Guest User

Untitled

a guest
Nov 16th, 2017
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void searchfile(char *txt);
  6. inline void filter(char **line, char *spam, char *legit, int nSpam, int nLegit, int *totspam, int *totlegit);
  7. inline bool scanword(char **source, char *filter);
  8. inline void cpsubstr(char *dest, char **src, char c);
  9. inline void jmpline(char **p);
  10.  
  11. int main()
  12. {
  13. /*legge tutto il file in una stringa*/
  14. size_t sizef = 0;
  15. char *text = 0;
  16. FILE *file;
  17.  
  18. file = fopen("input.txt", "rb");
  19. fseek(file, 0, SEEK_END);
  20. sizef = ftell(file);
  21. rewind(file);
  22. text = (char*)malloc(sizeof(char) * sizef);
  23. fread(text, 1, sizef, file);
  24. fclose(file);
  25. searchfile(text);
  26. free(text);
  27. //getchar();
  28. return 0;
  29. }
  30. /*
  31. * funzione searchfile
  32. * *txt pointer a text
  33. * inizializza tutti i parametri e inizia la ricerca
  34. */
  35.  
  36. void searchfile(char *txt)
  37. {
  38. char *line = (char*)malloc(sizeof(char) * 20480); /*crea un buffer di 20kB cioè 1000 parole da 20 caratteri l'una (il limite del correttore)*/
  39. char *spam = (char*)malloc(sizeof(char) * 20480);
  40. char *legit = (char*)malloc(sizeof(char) * 20480);
  41. int nMail = 0;
  42. int nSpam = 0;
  43. int nLegit = 0;
  44. int totalSpam = 0; /* counted mails.. */
  45. int totalLegit = 0;
  46.  
  47. cpsubstr(line, &txt, '\n');
  48. nSpam = atoi(line);
  49.  
  50. cpsubstr(spam, &txt, '\n');
  51.  
  52. cpsubstr(line, &txt, '\n');
  53. nLegit = atoi(line);
  54.  
  55. cpsubstr(legit, &txt, '\n');
  56.  
  57. cpsubstr(line, &txt, '\n');
  58. nMail = atoi(line);
  59.  
  60. while (nMail-- != 0)
  61. {
  62. jmpline(&txt);
  63. cpsubstr(line, &txt, '\n');
  64. filter (&line, spam, legit, nSpam, nLegit, &totalSpam, &totalLegit);
  65. }
  66.  
  67. FILE * output;
  68. output = fopen("output.txt", "w");
  69. fprintf(output, "%d %d", totalSpam, totalLegit);
  70. fclose(output);
  71.  
  72. //printf("TotalSpam = %d\nTotalLegit = %d\n", totalSpam, totalLegit);
  73.  
  74. free(line);
  75. free(spam);
  76. free(legit);
  77. }
  78.  
  79. inline void filter(char **line, char *spam, char *legit, int nSpam, int nLegit, int *totspam, int *totlegit)
  80. {
  81. char *line_tmp = *line;
  82. bool isSpam = 0;
  83. bool isLegit = 0;
  84.  
  85. while (nSpam != 0)
  86. {
  87.  
  88. if (*line_tmp == '\0')
  89. {
  90. line_tmp = *line; // se il primo carattere della linea è \0 allora resetta line_tmp in line cioè l'inzio della linea
  91. while (*spam++ != ' ' && *spam != '\0'); // scorri spam fin quando non trovi lo spazio
  92. nSpam--;
  93. }
  94. else if (*line_tmp != *spam)
  95. {
  96. while (*line_tmp++ != ' ' && *line_tmp != '\0');
  97. }
  98. else
  99. {
  100. isSpam = scanword(&line_tmp, spam); //&spam
  101. if (isSpam)
  102. break;
  103. }
  104. }
  105.  
  106. line_tmp = *line;
  107.  
  108. while (nLegit != 0)
  109. {
  110. if (*line_tmp == '\0')
  111. {
  112. line_tmp = *line;
  113. while (*legit++ != ' ' && *legit != '\0');
  114. nLegit--;
  115. }
  116. else if (*line_tmp != *legit)
  117. {
  118. while (*line_tmp++ != ' ' && *line_tmp != '\0');
  119. }
  120. else
  121. {
  122. isLegit = scanword(&line_tmp, legit);
  123. if (isLegit)
  124. break;
  125. }
  126. }
  127.  
  128. //printf("IS SPAM: %d || IS LEGIT: %d\n", isSpam, isLegit);
  129. if (isSpam != isLegit)
  130. {
  131. if (isSpam)
  132. *totspam += 1;
  133. if (isLegit)
  134. *totlegit += 1;
  135. }
  136.  
  137.  
  138. }
  139.  
  140. inline bool scanword(char **source, char *filter)
  141. {
  142. bool code = 1;
  143. char *src = *source;
  144.  
  145. while (1)
  146. {
  147. if (*src != ' ' && *filter != ' ' && *src != '\0' && *filter != '\0')
  148. {
  149. if (*src++ != *filter++)
  150. {
  151. code = 0;
  152. break;
  153. }
  154. }
  155. else
  156. break;
  157. }
  158. *source = src;
  159. return code;
  160. }
  161.  
  162. /*
  163. * funzione cpsubstr
  164. * *dest è il char di destinazione *
  165. * **src è il pointer da cui prendere la stringa *
  166. * char c è il delimiter *
  167. * returna la prossima riga di una stringa con più newline *
  168. */
  169.  
  170. inline void cpsubstr(char *dest, char **src, char c)
  171. {
  172.  
  173. char *tmp = *src;
  174. while(*tmp != c && *tmp != '\0')
  175. *dest++ = *tmp++;
  176. *dest = '\0'; //perchè se sovrascrive una parola più corta di quella già allocata rimangono dei pezzetti
  177. *tmp++; //perchè il loop si ferma al newline..
  178. *src = tmp; //returna il nuovo pointer
  179. }
  180. /*
  181. VERSIONE ALTERNATIVA DELLA FUNZIONE cpsubstr, parametri in più: size_t s_begin size_t s_end
  182. copia una substringa nella stringa a date coordinate inizio/fine
  183. avrei potuto usarla insieme a un altra funzione findline, che mi trovava la fine della riga.
  184. l'ho tolta perchè mi sono accorto che potevo fare in un modo più veloce
  185.  
  186. char tmp[(s_end - s_begin)+1] = dest;
  187. int counter = 0;
  188.  
  189. while(s_begin < s_end)
  190. tmp[counter++] = src[s_begin++];
  191.  
  192. tmp[++counter] = '\0';
  193. return tmp;
  194. */
  195.  
  196. inline void jmpline(char **p)
  197. {
  198. char *tmp = *p;
  199. while (*tmp++ != '\n');
  200. *p = tmp;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement