Advertisement
Guest User

Untitled

a guest
May 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. //Struktura elementu listy slow
  7.  
  8. typedef struct ListWordNode {
  9. struct ListWordNode* next;
  10. char* word;
  11. } wordNode;
  12.  
  13. //Deklaracje wybranych funkcji
  14. void error(char* error);
  15. void clear(wordNode** head);
  16.  
  17. //Porownoje dane slowa. Jezeli dane pierwsze slowo jest alfabetycznie przed drugim zwraca wartosc wieksza od 0. Jezeli po, zwraca wartosc mniejsza od 0. Jezeli slowa sa takie same, zwraca 0.
  18. //Zwrocona wartosc odpowiada odleglosci znakow w tablicy ascii
  19. int compareNoCase(char const *a, char const *b)
  20. {
  21. int i = 0;
  22. int d = 0;
  23. do
  24. {
  25. d = tolower(a[i]) - tolower(b[i]);
  26. ++i;
  27. } while (a[i] != '0' && d == 0); //Wartosc 0 odpowiada "\0" - koniec lancucha
  28. return d;
  29. }
  30.  
  31. //Zwalnia pamieci elementu listy
  32. void deleteNode(wordNode* node)
  33. {
  34. free(node->word);
  35. free(node);
  36. }
  37.  
  38. //Zamienia pola danych zadanych elementow listy
  39. void swap(wordNode* node1, wordNode* node2)
  40. {
  41. char* temp = node1->word;
  42. node1->word = node2->word;
  43. node2->word = temp;
  44. }
  45.  
  46. //Sortuje alfabetycznie elementy listy
  47. void bubblesort(wordNode* head)
  48. {
  49. wordNode *prev = head;
  50. wordNode *current;
  51. if (head == NULL)
  52. {
  53. return;
  54. }
  55. current = head->next;
  56. while (current != NULL)
  57. {
  58. if (compareNoCase(prev->word, current->word) > 0)
  59. {
  60. swap(current, prev);
  61. }
  62. prev = current;
  63. current = current->next;
  64. }
  65. }
  66.  
  67. //Usuwa znaki specjalne z slowa
  68. void parseword(char* word)
  69. {
  70. char buf[64] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  71.  
  72. int startIndex = 0;
  73. int endIndex = strlen(word);
  74. while (!isalpha(word[startIndex]))
  75. {
  76. ++startIndex;
  77. }
  78. while (!isalpha(word[endIndex]))
  79. {
  80. --endIndex;
  81. }
  82. memcpy(buf, &word[startIndex], 1 + endIndex - startIndex);
  83. strcpy(word, buf);
  84.  
  85. }
  86.  
  87. //Dodaje element na poczatku listy
  88. void push(wordNode** head, char* word, int wordLength)
  89. {
  90.  
  91. wordNode* newNode = (wordNode*)malloc(sizeof(*newNode));
  92. if (newNode == NULL)
  93. {
  94. clear(head);
  95. error("Malloc error");
  96. }
  97. newNode->word = (char*)malloc((sizeof(char)*wordLength) + 1);
  98.  
  99. if (newNode->word == NULL)
  100. {
  101. free(newNode);
  102. clear(head);
  103. error("Malloc error");
  104. }
  105. strcpy(newNode->word, word);
  106. newNode->next = *head;
  107. *head = newNode;
  108.  
  109. }
  110.  
  111. //Czysci cala liste
  112. void clear(wordNode** head)
  113. {
  114. wordNode* current = *head;
  115. wordNode* next = NULL;
  116. while (current != NULL)
  117. {
  118. next = current->next;
  119. deleteNode(current);
  120. current = next;
  121. }
  122. *head = NULL;
  123. }
  124.  
  125. //Konczy program w przypadku bledu
  126. void error(char* error)
  127. {
  128. perror(error);
  129. exit(1);
  130. }
  131.  
  132.  
  133. //Wyswietla liste w konsoli
  134. void printList(wordNode* const head)
  135. {
  136. wordNode* current = head;
  137. while (current != NULL)
  138. {
  139. printf("%s\n", current->word);
  140. current = current->next;
  141. }
  142.  
  143. }
  144.  
  145. //Wyswietla pomoc w konsoli
  146. void printHelp()
  147. {
  148. printf("program used to find the longest word in .txt files\n");
  149. printf("-i [foo.txt] - desired input filename used to read data\n");
  150. printf("-o [bar.txt] - desired output filename used to save processed result\n");
  151. printf("Example: najwiekszy.exe -i foo.txt -o bar.txt\n");
  152. exit(0);
  153. }
  154.  
  155. //Interpretuje argumenty programu (indeksy nazw plikow wejscia i wyjscia ustawiane sa poprzez wskazniki w liscie parametrow)
  156. void parseArgs(int *Input, int *Output, int argc, char const *argv[])
  157. {
  158. if (argc == 2)
  159. {
  160. if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-H") == 0)
  161. {
  162. printHelp();
  163. }
  164. }
  165. if (argc == 5)
  166. {
  167. if (strcmp(argv[1], "-i") == 0 && strcmp(argv[3], "-o") == 0) {
  168. *Input = 2;
  169. *Output = 4;
  170. return;
  171. }
  172. else if (strcmp(argv[3], "-i") == 0 && strcmp(argv[1], "-o") == 0) {
  173. *Input = 4;
  174. *Output = 2;
  175. return;
  176. }
  177. }
  178.  
  179. error("Invalid arguments. Run with -h for help.");
  180. }
  181.  
  182. //Przetlumacza plik na liste najdluzszych slow z tego pliku
  183. wordNode* parseFile(char const* inputFileName)
  184. {
  185. wordNode* head = NULL;
  186. int currentLength = 0;
  187. int maxLength = 0;
  188. char buf[64];
  189. printf("Opening file %s...\n", inputFileName);
  190. FILE* input = fopen(inputFileName, "r");
  191. if (input == NULL)
  192. {
  193. error("Cannot open specified input file");
  194. }
  195. printf("Reading from input file...");
  196. while (fscanf(input, " %64s", buf) == 1)
  197. {
  198. parseword(buf);
  199. currentLength = strlen(buf);
  200. if (currentLength >= maxLength)
  201. {
  202. if (currentLength != maxLength)
  203. {
  204. clear(&head);
  205. maxLength = currentLength;
  206. }
  207.  
  208. push(&head, buf, currentLength);
  209. }
  210.  
  211. }
  212. printf("Done!\n");
  213. fclose(input);
  214. return head;
  215. }
  216.  
  217. //Sortuje i zapisuje do pliku slowa z listy
  218. void writeToFile(wordNode *head, const char* outputFileName)
  219. {
  220. bubblesort(head);
  221. wordNode* current = head;
  222. printf("Opening file %s...\n", outputFileName);
  223. FILE* output = fopen(outputFileName, "w");
  224. if (output == NULL)
  225. {
  226. error("Cannot open specified output file");
  227. }
  228. printf("Writing to file...");
  229. while (current != NULL)
  230. {
  231. fprintf(output, "%s\n", current->word);
  232. current = current->next;
  233. }
  234. printf("done!\n");
  235.  
  236. }
  237.  
  238. //Punkt wejsciowy programu
  239. int main(int argc, const char *argv[]) {
  240.  
  241. //Deklaracja indeksow nazw plikow wejscia/wyjscia dla argv
  242. int in = 0, out = 0;
  243. //Deklaracja poczatku listy slow
  244. wordNode* head;
  245. //Interpretacja argumentow i ustawienie indeksow
  246. parseArgs(&in, &out, argc, argv);
  247.  
  248. printf("Input file: %s\nOutput file: %s\n", argv[in], argv[out]);
  249. //Przepisanie najdluzszych slow z pliku do listy
  250. head = parseFile(argv[in]);
  251. //Przepisanie slow z listy do pliku wyjsciowego
  252. writeToFile(head, argv[out]);
  253. //Zwalnianie pamieci
  254. clear(&head);
  255. printf("Program completed\n");
  256.  
  257.  
  258. return 0;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement