forked from rswinkle/spelling_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spelling_game.h
109 lines (85 loc) · 2.21 KB
/
spelling_game.h
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef SPELLING_GAME_H
#define SPELLING_GAME_H
#include <string.h>
#include <ctype.h>
#include "cvector.h"
int compare_strings(const void* a, const void* b)
{
return strcmp(*(const char**)a, *(const char**)b);
}
int compare_strings_1(const void* a, const void* b)
{
return strncmp(*(const char**)a, *(const char**)b, 1);
}
int compare_strings_2(const void* a, const void* b)
{
//printf("%s\t%s\n", *(const char**)a, *(const char**)b);
return strncmp(*(const char**)a, *(const char**)b, 2);
}
int compare_strings_3(const void* a, const void* b)
{
return strncmp(*(const char**)a, *(const char**)b, 3);
}
int compare_strings_4(const void* a, const void* b)
{
return strncmp(*(const char**)a, *(const char**)b, 4);
}
int (*compare_str_funcs[])(const void*, const void*) =
{
compare_strings_1, compare_strings_2, compare_strings_3, compare_strings_4
};
#define CLEAR_SCREEN "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" \
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" \
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
int is_good_word(char* word)
{
int seen_lower = 0;
for (char* c=word; *c; ++c) {
if (!isalpha(*c))
return 0;
if (islower(*c))
seen_lower = 1;
*c = tolower(*c);
}
if (!seen_lower) //it's an acronym
return 0;
return 1;
}
void* mybsearch(const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void *, const void *))
{
size_t min = 0, max = num-1;
size_t cursor;
while (min <= max) {
cursor = min + ((max - min) / 2);
int ret = compare(&key, (const char*)buf+cursor*size);
if (!ret) {
return (char*)buf + cursor*size;
} else if (ret < 0) {
max = cursor - 1; //overflow possibilities here and below
} else {
min = cursor + 1;
}
}
return NULL;
}
void find_match_range(vector_str* wordlist, int n_chars, char* prefix, int n, int* first, int* last)
{
int initial = n;
while (n >= 0) {
if (compare_str_funcs[n_chars-1](&prefix, &wordlist->a[n])) {
break;
}
--n;
}
*first = ++n;
n = initial;
while (n < wordlist->size) {
if (compare_str_funcs[n_chars-1](&prefix, &wordlist->a[n])) {
break;
}
++n;
}
*last = --n;
return;
}
#endif