【问题标题】:Finding words in an array using a binary search function in C?使用C中的二进制搜索函数在数组中查找单词?
【发布时间】:2013-05-28 14:30:50
【问题描述】:

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

#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;
}

有什么想法吗?

【问题讨论】:

标签: c binary-search


【解决方案1】:

好吧,你发布的代码有各种不正确的地方。

  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);
        }
    }

希望这会有所帮助。

【讨论】:

  • 感谢您的帮助!但是,我仍然无法使用二进制搜索功能打印出动词。例如。 “walk and talk”应该打印“walk talk”。我的想法是返回这些动词的位置,但我不确定如何实现它。
  • 我将 binary_search 更改为一个 int 函数,如果找到动词,我会尝试返回 mid,但它不太有效。
猜你喜欢
  • 1970-01-01
  • 2014-01-08
  • 2017-04-10
  • 2019-11-17
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
  • 2019-03-11
  • 2013-01-31
相关资源
最近更新 更多