C 二进制搜索中出现无限循环,为什么?
我接到一个任务,用C语言编写一个使用指针和内存分配的字典。 一切正常,但我实现的二进制搜索的行为方式很奇怪(我无法找到导致这种行为的特定模式): 有时,当用户在C 二进制搜索中出现无限循环,为什么?,c,string,binary-search,C,String,Binary Search,我接到一个任务,用C语言编写一个使用指针和内存分配的字典。 一切正常,但我实现的二进制搜索的行为方式很奇怪(我无法找到导致这种行为的特定模式): 有时,当用户在compare中输入字符串并将其复制到search(分配后)后,它只进行搜索而不进行查找,但从未退出循环,代码如下: 搜索使用的数组经过排序,不包含任何重复的值,数组的每个元素都包含一个字符串的地址,如果有任何帮助,我将不胜感激 void bin_search (char **word, char **definitions_1 ,cha
compare
中输入字符串并将其复制到search
(分配后)后,它只进行搜索而不进行查找,但从未退出循环,代码如下:
搜索使用的数组经过排序,不包含任何重复的值,数组的每个元素都包含一个字符串的地址,如果有任何帮助,我将不胜感激
void bin_search (char **word, char **definitions_1 ,char **definitions_2, int limit)
{
int first, //start value for binary search
last, //last value for binary search
middle; //middle value for binary search
char compare [81], //will get the word to search
*search, //will hold the string after memory allocation
ESC [5] = "Exit"; //this string holds exit value for comparison
do
{
first = 0;
last = limit-1;
middle = limit/2;
compare [81];
free(search); //memory allocated to search pointer is freed (allocation will take place upon next search)
printf("Please enter a word (exit to terminate):\n");
scanf("%s", compare); //gets desired word to search for
search = (char*) malloc (strlen (compare)+1);
if (search == NULL)
{
printf ("Memory allocation failed!\n");
exit (1);
}
strlwr (compare+1); //each of the letters (excluding the first one) will be chaneged to lower case letters
if ((int) *(compare) >= ASCII_a) //if the first [element] letter of a string [word] is a lower case letter
{
*compare = (char)(((int) *(compare)) - ASCII_CAPIAL_DIF); //the lower case letter will be changed to upper case one by subtracting the constat difference of 32
}
strcpy (search, compare);
while (first <= last && strcmp (search, ESC) != 0)
{
printf("\nsearching\n");
if (strcmp (word [middle], search) < 0)
{
first = middle+1;
}
else if (strcmp (word [middle], search) == 0)
{
if (definitions_2 [middle] == NULL) //if the word has only one definition, the second NULL one won't be diaplayed
{
printf("The word '%s' #%d has 1 definition:\n\n1.%s\n\n", search, middle, definitions_1 [middle]);
}
else //if the word has two defenitons - two of them will be disaplayed
{
printf("%s\n", definitions_2 [middle]);
printf("The word '%s' #%d has 2 definitions:\n\n1.%s\n\n2.%s\n\n", search, middle, definitions_1 [middle], definitions_2 [middle]);
}
break;
}
else
{
last = middle-1;
middle = ((first+last)/2);
}
}
if (first > last && strcmp (compare, ESC) != 0)
{
printf("'%s' is an unknown word!\n", search);
}
} while (strcmp (compare, ESC) != 0);
}
void bin\u搜索(字符**单词,字符**定义\u 1,字符**定义\u 2,整数限制)
{
int first,//二进制搜索的起始值
last,//二进制搜索的最后一个值
middle;//二进制搜索的中间值
char compare[81],//将获取要搜索的单词
*search,//将在内存分配后保留该字符串
ESC[5]=“Exit”;//此字符串保存用于比较的Exit值
做
{
第一个=0;
最后=极限-1;
中间=极限/2;
比较[81];
释放(搜索);//分配给搜索指针的内存被释放(分配将在下次搜索时进行)
printf(“请输入一个单词(退出以终止):\n”);
scanf(“%s”,compare);//获取要搜索的单词
search=(char*)malloc(strlen(compare)+1);
如果(搜索==NULL)
{
printf(“内存分配失败!\n”);
出口(1);
}
strlwr(compare+1);//每个字母(不包括第一个)都将转换为小写字母
if((int)*(compare)>=ASCII_a)//如果字符串[word]的第一个[element]字母是小写字母
{
*compare=(char)((int)*(compare))-ASCII_CAPIAL_DIF);//通过减去常量差32,小写字母将更改为大写字母
}
strcpy(搜索、比较);
然而(首先有时,在没有所有细节的情况下查看代码的结构会有所帮助
if (strcmp (word [middle], search) < 0)
{
first = middle+1;
}
else if (strcmp (word [middle], search) == 0)
{
break;
}
else
{
last = middle-1;
middle = ((first+last)/2);
}
if(strcmp(单词[中间],搜索)<0)
{
第一个=中间+1;
}
else if(strcmp(单词[中间],搜索)==0)
{
打破
}
其他的
{
last=middle-1;
中间=((第一个+最后一个)/2);
}
请显示变量声明。例如,什么是word
?什么是first
?last
?所涉及变量的值是什么?word是char**
变量-数组,它包含排序后没有重复值的所有单词。first
和last
are二进制搜索操作所需的变量。在代码中放入一些打印语句(或调试)查看变量是如何变化的,以及变量与预期变化的不同之处。变量已按预期变化。最好将二进制搜索提取到自己的方法中。这会使一切看起来更好。此外,避免为相同的参数调用两次strcmp,即使某些实现会缓存result.是的,你是对的……那么它(二进制搜索)实现得正确吗?@Medvednic乍一看,你的搜索似乎实现得正确,当然,一旦调试完成:)那么问题出在哪里呢?我怀疑它与用户输入有关。我是否必须将比较
复制到另一个字符串搜索
?将比较
复制到搜索
是不必要的,但无害的。我看到的一个问题是,在一个uninit上第一次通过循环调用free
序列化指针。您应该将search
初始化为NULL,或者干脆将其全部删除。