gpt4 book ai didi

c - 对没有任何内容的字典进行排序

转载 作者:行者123 更新时间:2023-11-30 19:16:50 25 4
gpt4 key购买 nike

我是一名 C 语言学生,正在使用 Kochans C 语言进行编程。我的挑战是为之前编写的程序创建一个字典排序函数。我感到困惑的是我没有工具可以做到这一点,这意味着......

没有指针没有字符串头几乎没有命令,也没有操作字符串的命令(例如 strcpy)。

我一直在尝试使用简单的冒泡排序(字典只有十个条目...),但由于条目是按结构组织的,我得到了只读位置,并且无法分配值(或我做错了,这总是有可能的)。代码在下面,尽管问题出在顶部的函数中。我收到的错误是“无法从只读位置分配值”。

如何在有限制的情况下完成此任务?

另外,我知道基本冒泡排序的代码不完整,但我试图解决的问题此时已经出现,我被难住了。任何意见都会很棒。

#include <stdio.h>

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

int dictionarySort(const struct entry dictionary[],int entries)
{
int x, y;
char tempWord;
char tempDefinition[81];

for (y = 0; y <= entries; ++y) {
for (x = 0; x < 15; ++x) {
if (dictionary[y].word[x] > dictionary[y + 1].word[x])
tempWord = dictionary[y].word[x];
dictionary[y].word[x] = dictionary[y + 1].word[x];
dictionary[y + 1].word[x] = tempWord;
printf("%c", tempWord);
}
}

return 0;
}

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

int entries = 10;
int dictionarySort(const struct entry dictionary[], int entries);

dictionarySort(dictionary, entries);

return 0;
}

最佳答案

字典条目是const,因为您告诉编译器对您的参数强制执行此约束:const struct Entrydictionary[] 并且您也以这种方式创建了数组: const struct 条目字典 [100] = { ... }

顺便说一句,由于问题的陈述,您无法使用字符串函数,但这并不妨碍您编写等效的函数。这对于学习者来说是一个很好的练习,当然也是您受到此限制的原因:)

关于c - 对没有任何内容的字典进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29348567/

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