Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- //Struktura elementu listy slow
- typedef struct ListWordNode {
- struct ListWordNode* next;
- char* word;
- } wordNode;
- //Deklaracje wybranych funkcji
- void error(char* error);
- void clear(wordNode** head);
- //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.
- //Zwrocona wartosc odpowiada odleglosci znakow w tablicy ascii
- int compareNoCase(char const *a, char const *b)
- {
- int i = 0;
- int d = 0;
- do
- {
- d = tolower(a[i]) - tolower(b[i]);
- ++i;
- } while (a[i] != '0' && d == 0); //Wartosc 0 odpowiada "\0" - koniec lancucha
- return d;
- }
- //Zwalnia pamieci elementu listy
- void deleteNode(wordNode* node)
- {
- free(node->word);
- free(node);
- }
- //Zamienia pola danych zadanych elementow listy
- void swap(wordNode* node1, wordNode* node2)
- {
- char* temp = node1->word;
- node1->word = node2->word;
- node2->word = temp;
- }
- //Sortuje alfabetycznie elementy listy
- void bubblesort(wordNode* head)
- {
- wordNode *prev = head;
- wordNode *current;
- if (head == NULL)
- {
- return;
- }
- current = head->next;
- while (current != NULL)
- {
- if (compareNoCase(prev->word, current->word) > 0)
- {
- swap(current, prev);
- }
- prev = current;
- current = current->next;
- }
- }
- //Usuwa znaki specjalne z slowa
- void parseword(char* word)
- {
- 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";
- int startIndex = 0;
- int endIndex = strlen(word);
- while (!isalpha(word[startIndex]))
- {
- ++startIndex;
- }
- while (!isalpha(word[endIndex]))
- {
- --endIndex;
- }
- memcpy(buf, &word[startIndex], 1 + endIndex - startIndex);
- strcpy(word, buf);
- }
- //Dodaje element na poczatku listy
- void push(wordNode** head, char* word, int wordLength)
- {
- wordNode* newNode = (wordNode*)malloc(sizeof(*newNode));
- if (newNode == NULL)
- {
- clear(head);
- error("Malloc error");
- }
- newNode->word = (char*)malloc((sizeof(char)*wordLength) + 1);
- if (newNode->word == NULL)
- {
- free(newNode);
- clear(head);
- error("Malloc error");
- }
- strcpy(newNode->word, word);
- newNode->next = *head;
- *head = newNode;
- }
- //Czysci cala liste
- void clear(wordNode** head)
- {
- wordNode* current = *head;
- wordNode* next = NULL;
- while (current != NULL)
- {
- next = current->next;
- deleteNode(current);
- current = next;
- }
- *head = NULL;
- }
- //Konczy program w przypadku bledu
- void error(char* error)
- {
- perror(error);
- exit(1);
- }
- //Wyswietla liste w konsoli
- void printList(wordNode* const head)
- {
- wordNode* current = head;
- while (current != NULL)
- {
- printf("%s\n", current->word);
- current = current->next;
- }
- }
- //Wyswietla pomoc w konsoli
- void printHelp()
- {
- printf("program used to find the longest word in .txt files\n");
- printf("-i [foo.txt] - desired input filename used to read data\n");
- printf("-o [bar.txt] - desired output filename used to save processed result\n");
- printf("Example: najwiekszy.exe -i foo.txt -o bar.txt\n");
- exit(0);
- }
- //Interpretuje argumenty programu (indeksy nazw plikow wejscia i wyjscia ustawiane sa poprzez wskazniki w liscie parametrow)
- void parseArgs(int *Input, int *Output, int argc, char const *argv[])
- {
- if (argc == 2)
- {
- if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-H") == 0)
- {
- printHelp();
- }
- }
- if (argc == 5)
- {
- if (strcmp(argv[1], "-i") == 0 && strcmp(argv[3], "-o") == 0) {
- *Input = 2;
- *Output = 4;
- return;
- }
- else if (strcmp(argv[3], "-i") == 0 && strcmp(argv[1], "-o") == 0) {
- *Input = 4;
- *Output = 2;
- return;
- }
- }
- error("Invalid arguments. Run with -h for help.");
- }
- //Przetlumacza plik na liste najdluzszych slow z tego pliku
- wordNode* parseFile(char const* inputFileName)
- {
- wordNode* head = NULL;
- int currentLength = 0;
- int maxLength = 0;
- char buf[64];
- printf("Opening file %s...\n", inputFileName);
- FILE* input = fopen(inputFileName, "r");
- if (input == NULL)
- {
- error("Cannot open specified input file");
- }
- printf("Reading from input file...");
- while (fscanf(input, " %64s", buf) == 1)
- {
- parseword(buf);
- currentLength = strlen(buf);
- if (currentLength >= maxLength)
- {
- if (currentLength != maxLength)
- {
- clear(&head);
- maxLength = currentLength;
- }
- push(&head, buf, currentLength);
- }
- }
- printf("Done!\n");
- fclose(input);
- return head;
- }
- //Sortuje i zapisuje do pliku slowa z listy
- void writeToFile(wordNode *head, const char* outputFileName)
- {
- bubblesort(head);
- wordNode* current = head;
- printf("Opening file %s...\n", outputFileName);
- FILE* output = fopen(outputFileName, "w");
- if (output == NULL)
- {
- error("Cannot open specified output file");
- }
- printf("Writing to file...");
- while (current != NULL)
- {
- fprintf(output, "%s\n", current->word);
- current = current->next;
- }
- printf("done!\n");
- }
- //Punkt wejsciowy programu
- int main(int argc, const char *argv[]) {
- //Deklaracja indeksow nazw plikow wejscia/wyjscia dla argv
- int in = 0, out = 0;
- //Deklaracja poczatku listy slow
- wordNode* head;
- //Interpretacja argumentow i ustawienie indeksow
- parseArgs(&in, &out, argc, argv);
- printf("Input file: %s\nOutput file: %s\n", argv[in], argv[out]);
- //Przepisanie najdluzszych slow z pliku do listy
- head = parseFile(argv[in]);
- //Przepisanie slow z listy do pliku wyjsciowego
- writeToFile(head, argv[out]);
- //Zwalnianie pamieci
- clear(&head);
- printf("Program completed\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement