我对第10章的练习10有一个问题,在Stephen Cochan编写的C编程一书中。
问题是:
编写一个名为
dictionarySort
的函数,用于对字典进行排序,如中所定义 程序10.9和10.10,按字母顺序排列。
虽然我认为排序算法没有问题,但是我命名存储字符的临时变量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"
你可以看到没有发生任何事情,字典也没有排序。 我对此非常困惑。我已经成功实现了一个对数组进行排序的程序,但我不知道我应该在这做什么。我不知道我哪里错了。如果有人能给我一个提示,那将对我有所帮助!
答案 0 :(得分:3)
与
if ( dictionary[i].word > dictionary[j].word )
你正在比较地址(dictionary[i].word
是字符串第一个字符的地址),而不是词汇值。
从这个角度来看,字典已经完全排序(因为元素存在于升序的内存地址中)。
将其更改为:
if (strcmp(dictionary[i].word, dictionary[j].word) > 0)
它可能会起作用。
答案 1 :(得分:2)
尝试这种方式:)
for ( i = 0; i < entries - 1; ++i )
for ( j = i + 1; j < entries; ++j )
if ( strcmp(dictionary[i].word, dictionary[j].word)>0 ){ // 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
你写的代码比较了每个单词的指针!
更好地查看代码,我建议你做两件事:
声明:
struct entry temp;
而不是:
struct entry temp[10];
代码变为:
for ( i = 0; i < entries - 1; ++i )
for ( j = i + 1; j < entries; ++j )
if ( strcmp(dictionary[i].word, dictionary[j].word)>0 ){ // if previous word is higher than next word..
temp = dictionary[i];
dictionary[i] = dictionary[j];
dictionary[j] = temp;
} // ..exchange their positions in the dictiona
答案 2 :(得分:0)
您可以重用同一章中使用的compareStrings函数来比较字典中的单词
// Function to compare two character strings
int compareStrings (const char s1[], const char s2[])
{
int i = 0, answer;
while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0' )
++i;
if ( s1[i] < s2[i] )
answer = -1; /* s1 < s2 */
else if ( s1[i] == s2[i] )
answer = 0; /* s1 == s2 */
else
answer = 1; /* s1 > s2 */
return answer;
}
然后您的例程将变为:
for ( i = 0; i < entries - 1; ++i )
for ( j = i + 1; j < entries; ++j )
if ( compareStrings (dictionary[i].word, dictionary[j].word) == 1 ) {
temp[i] = dictionary[i];
dictionary[i] = dictionary[j];
dictionary[j] = temp[i];
}
从那里您可以构建dictionarySort函数