【发布时间】:2020-04-04 13:30:49
【问题描述】:
我有 2 个函数:smalltalk 和 do_smalltalk。 smalltalk 将检查用户输入(意图)是否与数组中给定的单词匹配。如果匹配,程序将转到do_smalltalk。我的问题是,如何将其更改为哈希表或哈希映射数据结构,以便优化对某个单词的搜索? smalltalk word 是 key,response 是 value。
smalltalk
int smalltalk(const char *intent)
{
// An array to store 9 smalltalk words
char *smalltalk[]= {"hi","hello","it","it's","that","that's","this","bye","goodbye"};
// Loop through the smalltalk array. Each index is a word.
for (int word = 0; word < 9; word++)
{
// If user input matches a small talk word, return 1.
if (strcmp(intent, smalltalk[word]) == 0)
{
return 1;
}
}
return 0;
}
}
do_smalltalk
int do_smalltalk(int argc, char *argv[], char *response, int n)
{
if (strcmp("hi",argv[0])==0 || strcmp("hello", argv[0]) == 0)
{
snprintf(response,n,"Hello");
}
else if (strcmp("it's",argv[0])==0 || strcmp("it",argv[0])==0)
{
snprintf(response,n,"Indeed it is.");
}
else if (strcmp("that's",argv[0])==0 || strcmp("that",argv[0])==0 || strcmp("this",argv[0])==0)
{
snprintf(response,n,"Yes, you're right.");
}
else if (strcmp("bye",argv[0])==0 || strcmp("goodbye",argv[0])==0)
{
snprintf(response,n,"Bye");
return 1;
}
return 0;
}
【问题讨论】:
-
了解散列并实现它。这里不方便解释。
-
所以.. [hashmap strings c] 似乎是研究的合理术语(包括)。
标签: c arrays hashmap hashtable