- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我对 Stephen Kochan 编写的《C 语言编程》一书中第 10 章的练习 10 有疑问。
问题是:
Write a function called
dictionarySort
that sorts a dictionary, as defined in Programs 10.9 and 10.10, into alphabetical order.
虽然我认为我的排序算法没有问题,但我命名存储字符的临时变量 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"
如您所见,没有发生任何事情,字典也没有得到排序。我对此很困惑。我已经成功地实现了一个对数组进行排序的程序,但我不知道我应该在这里做什么。我不知道我哪里错了。如果有人能给我提示,那将对我有很大帮助!
最佳答案
与
if ( dictionary[i].word > dictionary[j].word )
您正在比较地址(dictionary[i].word
是字符串第一个字符的地址),而不是词法值。
从这个角度来看,字典已经完美排序(因为元素位于升序内存地址)。
将其更改为:
if (strcmp(dictionary[i].word, dictionary[j].word) > 0)
它可能会起作用。
关于c - 字典排序,C 语言编程,Stephen Kochan,第 10 章,练习 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29630904/
K&R 前。 4.2 要求您修改给定的(非标准)atof 函数,该函数缺少处理指数的指数处理机制(如 123e6 或 456e-7)。我添加了一个最小的更改来处理正确输入的、无空格的个位数指数。为了检
我正在学习计算机科学入门类(class)的考试,我对“常规”算法和递归算法中的复杂性主题有疑问(通常我们将这些问题写成 C 代码). 我想知道 Internet 和/或书籍中是否有涵盖该主题的基础级别
console.log( ‘blah’.repeatMe( 3 ) ); 使用 Javascript 编写代码,使前面的函数打印: 输出:blahblahblah 最佳答案 噢,放弃函数式解决方案太有
我正在准备 Java SE 7 认证考试,并且正在做一些关于继承和访问修饰符的无聊练习。 但是现在我在应用继承时遇到了意外的行为。在我的基础包 com.testpkg 中,我有一个抽象类: packa
我刚刚开始了 C 语言队列的第一课,我得到了创建队列、向队列添加元素和删除元素的练习。但是,我在检查队列是满还是空时遇到了麻烦。 #include typedef struct FloatQueue
请问我从昨天开始就被困在下面这个问题中了。下面是问题: Write a program that uses console.log to print all the numbers from 1 to
我最近尝试了一些 Java,希望对我的风格进行一些评论。如果你喜欢看这个放在图像中的练习,并告诉我我的风格是否足够好?或者是做的还不够好,可以告诉我应该在哪方面多下工夫,帮我改进一下? exercis
我对手动编写 SQL 查询还很陌生,而且我有一个我似乎无法解决的练习。 我了解解决此问题所需的工具,但我就是想不出解决方案。 你能帮助我理解如何以一种能让我在未来解决类似练习的方式解决这个问题吗? 我
好吧,这就是练习: Define a class named student, containing three grades of students. The class will have a f
我是一个 JS 菜鸟,试图制作这个“你好,先生/小姐 你的名字!”干净的。我看不到在 if/else 中重构警报的方法,因为那样我就失去了 var b 的值。 JS: "use strict
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
反转二维数组的值,可以扩展 n 次。 [1, [2, [3, ... [n, null]]]] 给定: 所有数组的长度始终为 2 列表中的最后一个数组将包含一个 null 索引 1 示例: [1, [
我试图通过 Jason Hickey 笔记自学 OCaml,下面的练习让我难住了。 问题:编写一个函数 sum 给定两个整数边界 m,n 和函数 f 计算求和。 我正在尝试这个: let r
这是一个生成斐波那契数列的程序,这里是引用:http://sicp.org.ua/sicp/Exercise1-19 据说我们可以将程序视为“a <- bq + aq + ap and b <- bp
所以,我正在努力通过 SICP。 第 4 章的第一个练习是: Exercise 4.1. Notice that we cannot tell whether the metacircular eva
这个问题已经有答案了: Count the number of occurrences of a character in a string in Javascript (39 个回答) 已关闭 6
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我目前正在学习 JS,并且正在尝试进行编码练习。到目前为止,我已经成功地使用离线和在线部分代码的大量资源拼凑了以下代码。我已经非常接近了 - 只是结果中的数字无法正确。 一些背景:在函数中输入一个对象
我需要创建一个回收器 View 练习,这是一个带有简单的单个回收器的应用程序加载大小为 20 的页面,并且可以容纳无限数量的项目。 现在我不想做出重新加载越来越多的项目的幼稚解决方案,而是一个优雅的解
下面的实现正确吗? 输入:Oldrecords(GameRecord 对象数组)和 newRecords (GameRecord) 我将检查 oldRecords 数组中的 newRecord 值。如
我是一名优秀的程序员,十分优秀!