0

我一直在独自完成 Kochan 的“Programming in C”第 3 版,为明年的研究生院做准备,但我第一次被困住了。我离指针还有一章,但是最近关于字符串的这一章末尾的练习有一个问题,我自己的研究似乎表明只能通过使用指针来解决。

问题与数据结构有关entry

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

我们创建一个条目数组:

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

提示内容为:“编写一个名为的函数dictionary_sort,将字典按照[上面]的定义按字母顺序排序。”

我知道与函数有关的结构和数组有一些微妙之处,以及函数如何将它们作为参数或将它们作为返回值返回。对我来说似乎有意义的唯一方法是返回一个结构,或者特别是一个结构数组,但我认为我在这里没有正确应用它:

struct entry dictionary_sort(struct entry dictionary)

总的来说,我当前的程序版本如下:

#include <stdio.h>
#include <stdbool.h>

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

// Function to compare two character strings

int compare_strings(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;
}

// Function to sort a dictionary structure

struct entry dictionary_sort(struct entry dictionary[])
{
    int dictionary_length = sizeof(dictionary) / sizeof(dictionary[0]);
    int i, j, minimum;
    struct entry temp;

    for (i = 0; i < dictionary_length; i++) {
        minimum = i;
        for (j = i + 1; j < dictionary_length; j++) {
            if (compare_strings(dictionary[j].word, 
                                dictionary[minimum].definition) == -1)
                minimum = j;
        }
        temp = dictionary[minimum];
        dictionary[minimum] = dictionary[i];
        dictionary[i] = dictionary[minimum];
    }

    return dictionary;
}

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

    dictionary = dictionary_sort(dictionary);

    for (i = 0; i < dictionary_length; i++)
        printf("%s - %s.\n", dictionary[i].word, dictionary[i].definition);

    return 0;
}

字符串比较函数的行为与预期一样,因为它只返回一个整数。我真的不知道如何在不了解指针的情况下拥有所需的功能。有足够多的带有指针的示例可供查找,但我很好奇我在这里缺少什么基本原则,因为我觉得书中的其他所有内容对我来说都非常自然。

先感谢您!

4

4 回答 4

4

你根本不需要返回任何东西,你甚至不需要显式指针,这些都不需要。就地排序,不要重新发明轮子:使用strcmp()qsort()现场演示):

struct entry dictionary[] = {
    { "def", "second entry" },
    { "abc", "first entry" },
    { "ghi", "third entry" },
    { "mno", "fifth entry" },
    { "jkl", "fourth entry" }
};

int compare_entry(const void *l, const void *r)
{
    const struct entry *ll = l;
    const struct entry *rr = r;
    return strcmp(ll->word, rr->word);
}

#define COUNT(x) (sizeof(x) / sizeof(x[0]))

qsort(dictionary, COUNT(dictionary), sizeof(dictionary[0]), compare_entry);
于 2013-04-22T21:06:12.800 回答
1

虽然并不完美并且仍然需要明确定义指针,但这个答案在问题和本书的范围内,而不仅仅是调用库。

#include <stdio.h>
#include <stdbool.h>

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

// Function to compare two character strings

int compare_strings(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;
}

// Function to sort a dictionary structure

void dictionary_sort(struct entry *dictionary, int dictionary_length)
{
    int i, j, minimum;
    struct entry temp;

    for (i = 0; i < dictionary_length - 1; i++) {
        minimum = i;
        for (j = i + 1; j < dictionary_length; j++) {
            if (compare_strings(dictionary[j].word, 
                                dictionary[minimum].word) == -1)
                minimum = j;
        }
        temp = dictionary[minimum];
        dictionary[minimum] = dictionary[i];
        dictionary[i] = temp;
    }
}

// Prints the dictionary in its current state

void print_dictionary(struct entry *dictionary, int dictionary_length)
{
    int i;

    for (i = 0; i < dictionary_length; i++) {
        printf("%s - %s.\n", dictionary[i].word, dictionary[i].definition);
    }
}

// Demostrates the dictionary_sort function

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

    int i, dictionary_length = sizeof(dictionary) / sizeof(dictionary[0]);

    print_dictionary(&dictionary, dictionary_length);
    printf("\nSorting...\n\n");
    dictionary_sort(&dictionary, dictionary_length);
    print_dictionary(&dictionary, dictionary_length);
    printf("\n");

    return 0;
}
于 2013-04-24T19:11:09.940 回答
0

当你用 声明一个函数参数时[],你已经使用了指针。函数定义

int compare_strings(const char s1[], const char s2[])

在功能上与

int compare_strings(const char *s1, const char *s2)

与 相同struct entry dictionary_sort(struct entry dictionary[])

由于您获得了指向struct entry参数的指针dictionary,因此也return dictionary返回了指向的指针struct entry

而且您所做的所有更改dictionary都已经在外部可见,因为您修改了数组本身而不是某个本地数组。

于 2013-04-22T21:04:39.747 回答
0

像这样的语句"Returning an array of structs without pointers in C"在 C 中是一个悖论,因为在 C 中传递或返回数组的唯一方法是通过指针。即使看起来像char *foo(char arr_demo[]){}在传递数组之类的东西,本质上它是一个被传递的指针,因为它减少到char *foo(char *arr_demo){}.

于 2013-04-22T21:11:19.903 回答