C语言 如何将输出更正为预期的输出?

ddrv8njm  于 2023-02-21  发布在  其他
关注(0)|答案(1)|浏览(129)

我有一个名为unscramble的代码,它接受两个文件Jumbled.txtdictionary.txt,并查找两个文件中是否有包含相同字符的单词,例如,下面是

Jumbled.txt:                                  
Hello
Wassup
Rigga
Boyka
Popeye

dictionary.txt:
olleH
Yello
elloH
lloeH
aggiR
ggiRa
giRag
yokaB
Bakoy
kaBoy
eyePop
poePye

上述代码的预期输出为:

Hello: olleH elloH lloeH
Wassup: NO MATCHES
Rigga: aggiR ggiRa giRag
Boyka: yokaB Bakoy kaBoy
Popeye: eyePop poePye

下面是我的代码,试图解决这个问题:

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

#define MAX_WORD_LEN 50
#define MAX_DICT_WORDS 500000
#define MAX_JUMBLES 10000

char dict[MAX_DICT_WORDS][MAX_WORD_LEN + 1];
int dict_size;

char jumbles[MAX_JUMBLES][MAX_WORD_LEN + 1];
int jumbles_size;

int compare_chars(const void *a, const void *b) {
    return *(char *)a - *(char *)b;
}

void sort_chars(char *s) {
    qsort(s, strlen(s), sizeof(char), compare_chars);
}

int compare_words(const void *a, const void *b) {
    return strcmp((char *)a, (char *)b);
}

void load_dict(const char *filename) {
    FILE *fp = fopen(filename, "r");
    if (fp == NULL) {
        perror("Error opening dictionary file");
        exit(1);
    }
    dict_size = 0;
    char word[MAX_WORD_LEN + 1];
    while (fgets(word, MAX_WORD_LEN + 1, fp) != NULL) {
        int len = strlen(word);
        if (len > 0 && word[len - 1] == '\n') {
            word[len - 1] = '\0';  // remove newline
        }
        if (len > 1 && len <= MAX_WORD_LEN) {
            strcpy(dict[dict_size++], word);
        }
    }
    fclose(fp);
    qsort(dict, dict_size, sizeof(dict[0]), compare_words);
}

void load_jumbles(const char *filename) {
    FILE *fp = fopen(filename, "r");
    if (fp == NULL) {
        perror("Error opening jumbles file");
        exit(1);
    }
    jumbles_size = 0;
    char word[MAX_WORD_LEN + 1];
    while (fgets(word, MAX_WORD_LEN + 1, fp) != NULL) {
        int len = strlen(word);
        if (len > 0 && word[len - 1] == '\n') {
            word[len - 1] = '\0';  // remove newline
        }
        if (len > 1 && len <= MAX_WORD_LEN) {
            strcpy(jumbles[jumbles_size++], word);
        }
    }
    fclose(fp);
}

void unscramble() {
    char sorted[MAX_WORD_LEN + 1];
    for (int i = 0; i < jumbles_size; i++) {
        strcpy(sorted, jumbles[i]);
        sort_chars(sorted);
        printf("%s: ", jumbles[i]);
        int count = 0;
        for (int j = 0; j < dict_size && count < 10; j++) {
            if (strcmp(sorted, dict[j]) == 0) {
                printf("%s ", dict[j]);
                count++;
            }
        }
        if (count == 0) {
            printf("NO MATCHES");
        }
        printf("\n");
    }
}

int main(int argc, char *argv[]) {
    if (argc != 3) {
        printf("Usage: %s <dictionary> <jumbles>\n", argv[0]);
        return 1;
    }
    load_dict(argv[1]);
    load_jumbles(argv[2]);
    unscramble();
    return 0;
}

但是,当我执行./unscramble dictionary.txt Jumbled.txt时,得到的结果如下:

xxxxxxxxx@LAPTOPxxxxxxxx:~$ ./unscramble dictionary.txt Jumbled.txt
 lloeHH
: NO MATCHES
 giRagR
 kaBoyB
 poePyep

我一直在尝试调试这个问题,并改变了我的代码,但没有任何工作,这里的问题是什么?

yacmzcpb

yacmzcpb1#

至少以下错误:

启用所有编译器警告

修复所有警告。

排序字典

词典的单词集已排序。如果需要原始文件顺序,则不对词典排序。

// Do not expect
Hello: olleH elloH lloeH
// Expect
Hello: elloH lloeH olleH

给定OP的预期输出, 不需要 * 对单词进行排序*
未排序的单词

代码依赖于每个单词的字母排序,而字典单词中的字母不排序。在传递给strcmp(sorted, dict[j])之前,形成一个排序的字符串。

代码不应对字母或单词进行多次排序

只做一次并保存结果。

高级:char值为负值时排序错误。

字符串应该按unsigned char排序,这就是strcmp()的作用。

// return *(char*) a - *(char*) b;
return *(unsigned char*) a - *(unsigned char*) b;

建议以下测试代码

FILE *outf = fopen("Jumbled.txt", "w");
fputs("Hello\n", outf);
fputs("Wassup\n", outf);
fputs("Rigga\n", outf);
fputs("Boyka\n", outf);
fputs("Popeye\n", outf);
fclose(outf);

outf = fopen("dictionary.txt", "w");
fputs("olleH\n", outf);
fputs("Yello\n", outf);
fputs("elloH\n", outf);
fputs("lloeH\n", outf);
fputs("aggiR\n", outf);
fputs("ggiRa\n", outf);
fputs("giRag\n", outf);
fputs("yokaB\n", outf);
fputs("Bakoy\n", outf);
fputs("kaBoy\n", outf);
fputs("eyePop\n", outf);
fputs("poePye\n", outf);
fclose(outf);

相关问题