gpt4 book ai didi

c - 字典排序,C 语言编程,Stephen Kochan,第 10 章,练习 10

转载 作者:太空狗 更新时间:2023-10-29 16:11:50 27 4
gpt4 key购买 nike

我对 Stephen Kochan 编写的《C 语言编程》一书中第 10 章的练习 10 有疑问。

问题是:

Write a function called dictionarySort that sorts a dictionary, as defined in Programs 10.9 and 10.10, into alphabetical order.

虽然我认为我的排序算法没有问题,但我命名存储字符的临时变量 temp 的方式肯定有问题。我不知道我哪里错了,我已经尝试了很多次,现在我很困惑。

我的代码如下。你可以忽略这个事实,我没有写入另一个函数。当我看到我的错误时,我会做一个功能。现在,为了简单起见,我在 main 中拥有我需要的一切。

#include <stdio.h>

struct entry {
char word[15];
char definition[64];
};

int main (void) {
// void dicionarySort (struct entry *dictionary[], const int entries);

struct entry dictionary[100] = {
{ "aardvark", "a burrowing African mammal" },
{ "ahoy", "a nautical call of greeting" },
{ "affix", "to append; attach" },
{ "addle", "to become confused" },
{ "agar", "a jelly made from seaweed" },
{ "aerie", "a high nest" },
{ "acumen", "mentally sharp; keen" },
{ "aigrette", "an ornamental cluster of feathers" },
{ "abyss", "a bottomless pit" },
{ "ajar", "partially opened" }
};

int i, j, entries = 10;
struct entry temp[10];

printf("Dictionary before sorting:\n");
for (i = 0; i < entries; ++i)
printf("Word: \"%s\" \t Definition: \"%s\"\n",
dictionary[i].word, dictionary[i].definition);

//dictionary[100] = dicionarySort(&dictionary, 10);

for (i = 0; i < entries - 1; ++i)
for (j = i + 1; j < entries; ++j)
if (dictionary[i].word > dictionary[j].word) { // if previous word is higher than next word..
temp[i] = dictionary[i];
dictionary[i] = dictionary[j];
dictionary[j] = temp[i];
} // ..exchange their positions in the dictionary

printf("\nDictionary after sorting:\n");
for (i = 0; i < entries; ++i)
printf("Word: \"%s\" \t Definition: \"%s\"\n",
dictionary[i].word, dictionary[i].definition );

printf("\n");

return 0;
}

我得到的输出是:

Dictionary before sorting:
Word: "aardvark" Definition: "a burrowing African mammal"
Word: "ahoy" Definition: "a nautical call of greeting"
Word: "affix" Definition: "to append; attach"
Word: "addle" Definition: "to become confused"
Word: "agar" Definition: "a jelly made from seaweed"
Word: "aerie" Definition: "a high nest"
Word: "acumen" Definition: "mentally sharp; keen"
Word: "aigrette" Definition: "an ornamental cluster of feathers"
Word: "abyss" Definition: "a bottomless pit"
Word: "ajar" Definition: "partially opened"

Dictionary after sorting:
Word: "aardvark" Definition: "a burrowing African mammal"
Word: "ahoy" Definition: "a nautical call of greeting"
Word: "affix" Definition: "to append; attach"
Word: "addle" Definition: "to become confused"
Word: "agar" Definition: "a jelly made from seaweed"
Word: "aerie" Definition: "a high nest"
Word: "acumen" Definition: "mentally sharp; keen"
Word: "aigrette" Definition: "an ornamental cluster of feathers"
Word: "abyss" Definition: "a bottomless pit"
Word: "ajar" Definition: "partially opened"

如您所见,没有发生任何事情,字典也没有得到排序。我对此很困惑。我已经成功地实现了一个对数组进行排序的程序,但我不知道我应该在这里做什么。我不知道我哪里错了。如果有人能给我提示,那将对我有很大帮助!

最佳答案

if ( dictionary[i].word > dictionary[j].word )

您正在比较地址(dictionary[i].word 是字符串第一个字符的地址),而不是词法值。

从这个角度来看,字典已经完美排序(因为元素位于升序内存地址)。

将其更改为:

if (strcmp(dictionary[i].word, dictionary[j].word) > 0)

它可能会起作用。

关于c - 字典排序,C 语言编程,Stephen Kochan,第 10 章,练习 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29630904/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com