需要帮助打印链接列表的内容

发布于 2024-11-04 03:58:35 字数 417 浏览 1 评论 0原文

我需要编写一个程序来读取文件,并将它们的所有单词以及它们出现的频率和单词总数放入“字典”中。例如,如果文件读取

Hello, my name is Robert! My name is cool...

输出将为

hello 1
my 2
name 2
is 2
robert 1
cool 1
The total number of words is 9.

但是,我的函数仅打印单词总数,而不打印单词本身。抱歉,如果这是一个微不足道的问题,但我是 C 的新手。

这是代码 -

http://pastebin.com/ Nb30q965

I need to make a program that reads a file, and puts all of their words in a "dictionary" as well as how often they appear and the total number of words. For example, if the file read

Hello, my name is Robert! My name is cool...

the output would be

hello 1
my 2
name 2
is 2
robert 1
cool 1
The total number of words is 9.

However, my function only prints the total number of words, and not the words themselves. Sorry if this is a trivial question, but I'm a newbie to C.

Here's the code -

http://pastebin.com/Nb30q965

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋心╮凉 2024-11-11 03:58:35

您的代码在 add_to_dictionary 方法中存在错误。

问题#1:第 80 行应更改为 if(stringcompare == 0) (缺少 =,这使其成为赋值。执行后 stringcompare 将为 0,而不是检查它是否为 0赋值的结果是被赋值的值,0 相当于 C) 中的 false。

问题#2:您的循环仅检查列表中的第一项。你需要删除其他的。

问题 #3:第 90 行和第 91 行不应该在循环中。第 92 行的右大括号关闭循环。

问题 #4:在 new_entry 中,您应该 memcpystrcpy stringone 的值,而不是分配它。分配一个指针使其指向新地址。您想要做的是使当前地址与其他字符串具有相同的内容。为此,您需要遍历旧内存并将元素逐个复制到新内存中。由于您经常这样做,有人编写了一个可以为您执行此操作的函数(前面提到过的 strcpy 或 memcpy )。

我没有太多检查其余代码,因此不能保证此列表是详尽的。

Your code is buggy in the add_to_dictionary method.

Problem #1: Line 80 should be changed to if(stringcompare == 0) (you are missing an =, which makes it an assignment. stringcompare will be 0 after executing instead of checking if it is 0. The result of an assignment is the value assigned and 0 is the equivalent of false in C).

Problem #2: Your loop only checks against the first item in the list. You need to remove the else.

Problem #3: Line 90 and 91 should not be in the loop. The closing brace on line 92 closes the loop.

Problem #4: In new_entry, you should memcpy or strcpy the value of stringone, instead of assigning it. Assigning a pointer makes it point to the new address. What you want to do is for the current address to have the same content as the other string. To do that, you need to go through the old memory and copy element by element into the new memory. Since you do that a lot, someone wrote a function that will do it for you (strcpy or memcpy that where mentioned before).

I didn't check the rest of the code much, so no guarantees that this list is exhaustive.

晌融 2024-11-11 03:58:35

您的代码存在各种各样的问题。例如,在 new_entry() 函数中,您将一些新malloc内存的地址分配给 newentry->string ,但随后您立即将原始字符串的地址分配给它。这会导致内存泄漏(您永远无法回到您的malloced内存)。然后您正在执行 strcpy(temp->string, word),但它们现在指向相同的内存!所以你正在将字符串复制到其自身中。

还有其他问题(例如第 80 行错误地使用了 = 而不是 ==)。

There are all sorts of problems with your code. For instance, in your new_entry() function, you're assigning the address of some newly-malloced memory to newentry->string, but then you're immediately assigning the address of the original string to it. This causes a memory leak (you can never get back to your malloced memory). You're then doing strcpy(temp->string, word), but they're now pointing at the same memory! So you're copying the string into itself.

There are other issues as well (such as the incorrect use of = instead of == on line 80).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文