Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct list_item
  6. {
  7. char *value;
  8. struct list_item *next;
  9.  
  10. }list_item;
  11.  
  12. void print_help()
  13. {
  14. printf("Program sluzy do sprawdzania wystapien wrostka w pliku tekstowym.\n");
  15. printf("Jako argumenty nalezy podac nazwe pliku o rozszerzeniu .txt i tekst wrostka.\n");
  16. printf("Wrostek i slowa w pliku tekstowym musza skladac sie w pelni z wartosci alfabetycznych.\n");
  17. printf("Podanie nieprawidlowych parametrow skutkuje wyswietleniem tej wiadomosci i zamknieciem programu.\n\n");
  18. }
  19.  
  20. int is_alphabetical(char* string)
  21. {
  22. int i;
  23. for (i = 0; i<strlen(string); i++)
  24. {
  25. if ((string[i] < 'A') || (string[i] > 'Z' && string[i] < 'a') || (string[i] > 'z'))
  26. {
  27. return 0;
  28. }
  29. }
  30. return 1;
  31. }
  32.  
  33. list_item* create_item(char* string)
  34. {
  35. list_item* new_item = malloc(sizeof(list_item));
  36. new_item->value = string;
  37. new_item->next = 0;
  38. return new_item;
  39. }
  40.  
  41. void add_to_list(list_item** item, char* string)
  42. {
  43. list_item *ptr, *new_item = create_item(string);
  44.  
  45. //Jesli lista nie jest pusta
  46. if (*item)
  47. {
  48. ptr = *item;
  49. //Przejdz do konca listy
  50. while (ptr->next)
  51. {
  52. ptr = ptr->next;
  53. }
  54. //Dodaj element na koncu listy
  55. ptr->next = new_item;
  56. }
  57. else
  58. {
  59. //Dodaj pierwszy element listy
  60. *item = new_item;
  61. }
  62. }
  63.  
  64. char* cut_string(char *input)
  65. {
  66. char* output = (char*)malloc((strlen(input) + 1)*sizeof(char));
  67. strcpy(output, input);
  68.  
  69. output += 1;
  70. output[strlen(output) - 1] = '\0';
  71.  
  72. return output;
  73. }
  74.  
  75. int* count_substrings(char* base, char* substring)
  76. {
  77.  
  78. //Pierwsza pozycja zawiera ilosc wrostkow, kolejne ich pozycje
  79. int* results = (int*)malloc(sizeof(int));
  80. const char *tmp_base = base;
  81. int count = 0;
  82. while (tmp_base == strstr(tmp_base, substring))
  83. {
  84. count++;
  85. void *tmp = realloc(results, (count + sizeof(int)));
  86. if (!tmp)
  87. {
  88. return 0;
  89. }
  90. else
  91. {
  92. results = tmp;
  93. }
  94. results[count] = tmp_base - base;
  95. tmp_base += strlen(substring);
  96. }
  97. results[0] = count;
  98. return results;
  99. }
  100.  
  101. char* cut_string_length(char* input, int pos, int length)
  102. {
  103. int i;
  104. char* output = (char*)malloc((length+1)*sizeof(char));
  105. for(i = 0; i < length; i++)
  106. {
  107. output[i] = input[pos];
  108. pos++;
  109. }
  110. output[length] = 0;
  111. return output;
  112. }
  113.  
  114. char* load_string_from_file(char* filename)
  115. {
  116. FILE *file = fopen(filename, "rb");
  117. fseek(file, 0, SEEK_END);
  118. long fsize = ftell(file);
  119. rewind(file);
  120.  
  121. char *response = malloc(fsize + 1);
  122. fread(response, fsize, 1, file);
  123. fclose(file);
  124.  
  125. response[fsize] = 0;
  126.  
  127. return response;
  128. }
  129.  
  130. list_item* load_list_from_string(char* string)
  131. {
  132. list_item* item;
  133. char* word;
  134. int count = strlen(string);
  135. int word_length = 0, word_count = 0, i;
  136. for(i = 0; i < count; i++)
  137. {
  138. if ((string[i] <= 'z' && string[i] >= 'a') || (string[i] <= 'Z' && string[i] >= 'A'))
  139. {
  140. word_length++;
  141. }
  142. else if (word_length > 0)
  143. {
  144. if(word_count == 0)
  145. {
  146. item = malloc(sizeof(list_item));
  147.  
  148. }
  149. else
  150. {
  151. word = cut_string_length(string, i, word_length);
  152. add_to_list(&item, word);
  153. printf("%s", word);
  154. }
  155. word_count++;
  156. word_length = 0;
  157. }
  158. }
  159. return item;
  160. }
  161.  
  162. int main(int argc, char* argv[])
  163. {
  164. int flag_help = 0, flag_error = 0, arg_file = 0, arg_text = 0, arg_help = 0, i = 0;
  165.  
  166. //Znajdz parametry - moga byc podane w dowolnej kolejnosci
  167. //Pomin pierwszy parametr - nazwe pliku
  168. for (i = 1; i < argc; i++)
  169. {
  170. if (strstr(argv[i], ".txt")) arg_file = i;
  171. if (strstr(argv[i], "-h"))
  172. {
  173. arg_help = i;
  174. flag_help = 1;
  175. }
  176. if (arg_text == arg_file || arg_text == arg_help) arg_text++;
  177. }
  178.  
  179. if ((arg_help + arg_file) == 0) flag_error = 1;
  180. while (arg_text < argc)
  181. {
  182. if (!is_alphabetical(argv[arg_text]))
  183. {
  184. arg_text++;
  185. flag_error = 1;
  186. }
  187. else
  188. {
  189. flag_error = 0;
  190. break;
  191. }
  192. }
  193.  
  194. if (flag_error)
  195. {
  196. print_help();
  197. return 0;
  198. }
  199.  
  200. if (flag_help) print_help();
  201.  
  202. printf("Nazwa pliku : %s\n", argv[arg_file]);
  203. printf("Poszukiwany wrostek : %s\n\n", argv[arg_text]);
  204. printf("Nacisnij Enter, aby potwierdzic.\n");
  205.  
  206. if (getchar() == 10)
  207. {
  208. //Wybrano enter, wykonaj operacje
  209. //list_item *strings_to_scan = load_list_from_file(argv[arg_file]);
  210. char* text = load_string_from_file(argv[arg_file]);
  211. if (text)
  212. {
  213. list_item* item = load_list_from_string(text);
  214. free(text);
  215. }
  216. printf("\n\nNacisnij dowolny przycisk, aby zakonczyc dzialanie programu.");
  217. }
  218. else
  219. {
  220. return 0;
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement