2

入力された文字列を (コマンド ライン引数として) 受け取り、文字列に含まれる動詞を出力するプログラムを作成しようとしています。動詞のリストは、別のヘッダー ファイルの配列にあります。プログラムは現在動詞を見つけていますが、同じ単語を複数回出力しています。これが私のコードです:

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

何か案は?

4

1 に答える 1

3

さて、あなたが投稿したコードには、あらゆる種類の間違ったものがあります。

  1. 内側の for ループは必要ありません。実際、argv[1] から取得した各部分文字列について検索を行うのは間違っているため、内側のループは非常に間違っています。入力が 1 つの文字列であり、その部分文字列である動詞を見つけることが仕事であると想定していることに注意してください。argv のすべてをチェックする必要がある場合は、for ループを while の外に置きます。いずれにせよ、あなたのループ構成は正しくありません。
  2. strtok の区切り文字として空白を使用するのはなぜですか? 空白による文字列区切り文字は argv[1] にはありません!! したがって、余白は区切り記号であってはなりません。.- などのようなものが必要です
  3. strtok の入力区切り文字を変更しています。そうしないでください。
  4. strcmp を正しく使用していません ( http://www.cplusplus.com/reference/cstring/strcmp/ )
  5. 基本ケースの関数から戻ります。

とにかく、私はあなたの質問に基づいてあなたのプログラムを修正しました。

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

お役に立てれば。

于 2013-05-28T06:30:50.343 に答える