// wordcount.c // Řešení IJC-DU2, příklad b), 22.4.2024 // Autor: Roman Nečas, FIT // Přeloženo: gcc 11.4.0 #include #include "htab.h" #include "io.h" #include "htab_struct.h" #define MAX_WORD_LEN 255 #define HTAB_SIZE 130003 //program som testoval napriklad na prikaze seq 100000 200000|shuf, teda 100 0000 slov, velkost by mala byt 1.3 //nasobok a zaroven prvocislo, teda najblizsie cislo je 130003 static void print_pair(htab_pair_t *pair) { printf("%s\t%d\n", pair->key, pair->value); } int main() { htab_t *t = htab_init(HTAB_SIZE); if (t == NULL) { fprintf(stderr, "Chyba: Nedostatok pamati pre tabulku\n"); return 1; } char word[MAX_WORD_LEN]; int ret; while ((ret = read_word(word, sizeof(word), stdin)) != EOF) { if (ret == MAX_WORD_LEN) { fprintf(stderr, "Varovanie: Niektore slova boli skratene na 255 znakov.\n"); } htab_pair_t *pair = htab_lookup_add(t, word); if (pair == NULL) { fprintf(stderr, "Chyba: Nedostatok pamati pre novy zaznam\n"); htab_free(t); return 1; } pair->value++; } htab_for_each(t, print_pair); #ifdef STATISTICS htab_statistics(t); #endif htab_free(t); return 0; }