首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >整理字典,用C编程,Stephen,第10章,练习10

整理字典,用C编程,Stephen,第10章,练习10
EN

Stack Overflow用户
提问于 2015-04-14 15:06:25
回答 3查看 475关注 0票数 3

在Stephen的“C编程”一书中,我对第10章的练习10有一个问题。

问题是:

编写一个名为dictionarySort的函数,按字母顺序对字典进行排序,如程序10.9和10.10中定义的那样。

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

我的密码在下面。你可以忽略这个事实,我不是在写另一个函数。当我看到我的错误时,我会做一个函数。现在,为了保持简单,我在main中拥有了所需的一切。

代码语言:javascript
复制
#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;
}

我得到的输出是:

代码语言:javascript
复制
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"

如你所见,什么都没有发生,字典也没有整理好。我对此很困惑。我已经成功地实现了一个排序数组的程序,但是我不知道我在这里应该做什么。我不知道我哪里错了。如果有人能给我一个提示,它将极大地帮助我!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-04-14 15:14:05

使用

代码语言:javascript
复制
if ( dictionary[i].word > dictionary[j].word )

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

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

将其改为:

代码语言:javascript
复制
if (strcmp(dictionary[i].word, dictionary[j].word) > 0)

而且很可能会成功的。

票数 3
EN

Stack Overflow用户

发布于 2015-04-14 15:16:22

试着这样做:)

代码语言:javascript
复制
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

您编写的代码比较每个单词的指针!

看看你的代码,我建议你做两件事:

声明如下:

代码语言:javascript
复制
struct entry temp;

而不是:

代码语言:javascript
复制
struct entry temp[10];

守则变成:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2018-09-26 15:13:06

您可以重用同一章中使用的函数compareStrings来比较字典中的单词。

代码语言:javascript
复制
// 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;
}

然后你的常规变成:

代码语言:javascript
复制
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函数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29630904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档