-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordcount.c
66 lines (52 loc) · 1.3 KB
/
wordcount.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Název: wordcount.h
* Řešení: IJC-DU2, příklad b), 29.4.2014
* Autor: Vojtech Mašek, FIT 1BIB xmasek15
* Přeloženo: gcc 4.8.2
*/
#include "htable.h"
#include "io.h"
/**Velkost hash tabulky.
* Bola zvolena najvhodnejsia hodnota, prvocislo, tak aby sme dosahovali
* co najnizsiu chybovost. Z tabulky uverejnenej na odkaze nizsie mi ako
* nalepsie riesenie v pomere rozsahu tabulky a chyb prvocislo 12289.
* http://planetmath.org/GoodHashTablePrimes
*/
#define HTAB_SIZE 12289U
/**
* Implementacny limit pre orezavanie slov.
*/
#define WORD_LIMIT 127
/**
* Funkcia ktora bude spustana nad jednotlivimi polozkami tabulky.
*/
void print_items(const char *key, unsigned int data)
{
printf("%s\t%d\n", key, data);
}
/**
* Prepis programu wordcount z C++ do C zo zadania.
*/
int main(void)
{
htab_t *t=htab_init(HTAB_SIZE);
if(!t)
{
fprintf(stderr, "[wordcount]\t-htable was not allocated\n");
return EXIT_FAILURE;
}
char word[WORD_LIMIT+1]={0};
while(fgetw(word, WORD_LIMIT, stdin)!=EOF)
{
if(!htab_lookup(t, word))
{
fprintf(stderr, "[wordcount]\t-[htab_lookup] allocation fail\n");
htab_free(t);
return EXIT_FAILURE;
}
}
htab_foreach(t, &print_items);
//htab_statistics(t); /// vypis statistiky udajov tabulky
htab_free(t);
return EXIT_SUCCESS;
}