c - 使用 C 中的二分查找函数在数组中查找单词?

标签 c binary-search

我正在尝试制作一个程序,它接受输入的字符串(作为命令行参数),然后打印出字符串中的动词。动词列表位于单独头文件中的数组中。该程序当前正在查找动词,但多次打印同一个单词。这是我的代码:

#include "verbs.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void binary_search(char *list_of_words, int size, char *target){
    int bottom= 0;
    int mid;
    int top = size - 1;

    while(bottom <= top){
        mid = (bottom + top)/2;
        if (strcmp(&list_of_words[mid], target) == 0){
            printf("%s found at location %d.\n", target, mid+1);
            break;
        }
        if (strcmp(&list_of_words[mid], target) == 1){
            top= mid - 1;
        }
        if (strcmp(&list_of_words[mid], target) == -1){
            bottom= mid + 1;
        }
    }
}

int main(int argc, char* argv[]){

    char *input;
    int i = 0;

    input = strtok (argv[1], " \"\n");
    while (input != NULL){
        for (i = 0; i < VERBS; i++){ //VERBS is defined in verbs.h as 637
            binary_search(verbs[i], VERBS, input);
        }
        input = strtok (NULL, " ");
    }

    return 0;
}

有什么想法吗?

最佳答案

好吧,您发布的代码有很多不正确的地方。

  1. 您不需要内部 for 循环。事实上,对于从 argv[1] 获得的每个子字符串,您将进行搜索是错误的,因此内部循环是非常错误的。请注意,我假设您的输入只是一个字符串,您的工作是查找作为该字符串子串的动词。如果您需要检查 argv 中的所有内容,请在 while 之外放置一个 for 循环。无论哪种方式,您的循环配置都是不正确的。
  2. 为什么使用空格作为 strtok 的分隔符?任何由空格组成的字符串分隔符都不会出现在 argv[1] 中!所以空白不应该是那里的分隔符。你应该有诸如.-等之类的东西
  3. 您正在更改 strtok 的输入分隔符。不要这样做。
  4. 您错误地使用了 strcmp ( http://www.cplusplus.com/reference/cstring/strcmp/ )
  5. 从基本情况下的函数返回。

无论如何,我根据您的问题及其措辞修复了您的程序(有点不清楚您是否只期望 1 个输入字符串或多个输入字符串,我假设是 1)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DELIM ",.-+=*"

void binary_search(char *list_of_words[], int size, char *target){
    int bottom= 0;
    int mid;
    int top = size - 1;

    while(bottom <= top){
        mid = (bottom + top)/2;
        if (strcmp(list_of_words[mid], target) == 0){
            printf("%s found at location %d.\n", target, mid+1);
            return;
        } else if (strcmp(list_of_words[mid], target) > 0){
            top    = mid - 1;
        } else if (strcmp(list_of_words[mid], target) < 0){
            bottom = mid + 1;
        }
    }
}

int main(int argc, char* argv[]){
    int i = 1;
    char *input = strtok(argv[1], DELIM);

    char *verbs[5] = { "do", "make", "shit", "talk", "walk" };

    while (input != NULL) {   
        printf("looking at %s\n", input);
        binary_search(verbs, 5, input);
        input = strtok(NULL, DELIM);
    }

    return 0;
}

如果您希望通过 argv 更改多个字符串:

    while (input != NULL) {   
        printf("looking at %s\n", input);
        binary_search(verbs, 5, input);
        input = strtok(NULL, DELIM);
    }

    for (i = 1; i < argc - 1; i++) {
        input = strtok(argv[i], DELIM);
        while (input != NULL) {   
            printf("looking at %s\n", input);
            binary_search(verbs, 5, input);
            input = strtok(NULL, DELIM);
        }
    }

希望这有帮助。

关于c - 使用 C 中的二分查找函数在数组中查找单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16784352/

相关文章:

algorithm - Raku 中的简洁(一行?)二分搜索

c - linux内核启动时间

c++ - 在 gmock 中将自定义值设置为 void 指针

c++ - C/C++ : Calling function with no arguments with function which returns nothing

arrays - 数组中的峰元素

java - 如何对字节数组集合进行 Collections.binarySearch ?

c - 使用C打印服务器IP地址

c - 这段代码我哪里出错了

c++ - 从数组中查找 MIN MAX 对

python - 这种递归二分搜索算法可以更有效吗?