在Stephen的“C编程”一书中,我对第10章的练习10有一个问题。
问题是:
编写一个名为
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"如你所见,什么都没有发生,字典也没有整理好。我对此很困惑。我已经成功地实现了一个排序数组的程序,但是我不知道我在这里应该做什么。我不知道我哪里错了。如果有人能给我一个提示,它将极大地帮助我!
发布于 2015-04-14 15:14:05
使用
if ( dictionary[i].word > dictionary[j].word )您正在比较地址(dictionary[i].word是字符串的第一个字符的地址),而不是词法值。
从这个角度来看,字典已经被完美地排序了(因为元素位于升序内存地址)。
将其改为:
if (strcmp(dictionary[i].word, dictionary[j].word) > 0)而且很可能会成功的。
发布于 2015-04-14 15:16:22
试着这样做:)
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发布于 2018-09-26 15:13:06
您可以重用同一章中使用的函数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函数
https://stackoverflow.com/questions/29630904
复制相似问题