【发布时间】:2018-10-08 00:51:32
【问题描述】:
我必须制作一个单词的链接列表并分析出现在 txt 文件中的所有单词并打印出单词以及每个单词出现的次数。我有三个文件:WordList.h、WordList.c 和 TextAnalyzer.c
WordList.h:
#ifndef _WordList_H
#define _WordList_H
#include <stdbool.h>
typedef struct wordListNode *wordListNodePtr;
typedef struct wordListNode
{
char word[20];
int count;
wordListNodePtr next;
} WordListNode;
bool listIsEmpty();
int length();
wordListNodePtr addWords(const char *word);
wordListNodePtr findWord(const char *word);
int numericalSort();
int insertWord();
void sortedInsert();
int printList();
void printWord(wordListNodePtr word);
#endif
WordList.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "WordList.h"
wordListNodePtr head = {NULL};
bool listIsEmpty()
{
if (head == NULL)
{
return true;
}
else
{
return false;
}
}
int length()
{
wordListNodePtr current;
int count = 0;
for (current = head; current != NULL; current = current->next)
{
count++;
}
return count;
}
wordListNodePtr addWords(const char *word)
{
wordListNodePtr newWord = (wordListNodePtr)malloc(sizeof(WordListNode));
strcpy(newWord->word, word);
//adding to head
if (listIsEmpty()) {
head = newWord;
head->next = NULL;
}
else {
wordListNodePtr current = head;
if (current->word == word) {
current->count += 1;
}
else {
insertWord(newWord);
}
}
return newWord;
}
wordListNodePtr findWord (const char *word)
{
wordListNodePtr current = head;
wordListNodePtr foundNode;
while (current!=NULL) {
if (strcmp(word, current->word)==0)
{
foundNode = current;
}
else
{
current = current->next;
}
}
return foundNode;
}
int insertWord(const wordListNodePtr node)
{
wordListNodePtr current = head;
// if only head exists
if (current->next == NULL) {
if (strcmp(current->word, node->word) > 0) {
node->next = current;
head = node;
}
else
{
head->next = current;
}
}
while (current->next != NULL) {
if (strcmp(current->next->word, node->word) > 0)
{
node->next = current->next;
current->next = node;
}
current = current->next;
}
return 0;
}
int numericalSort()
{
wordListNodePtr sorted = NULL;
wordListNodePtr current = head;
while (current != NULL)
{
sortedInsert(&sorted, current);
current = current->next;
}
head = sorted;
return 0;
}
void sortedInsert (wordListNodePtr head, wordListNodePtr newNode)
{
wordListNodePtr current;
if (head==NULL || head->count >= newNode->count)
{
newNode->next = head;
head = newNode;
}
else
{
current = head;
while (current != NULL && current->next->count < newNode->count)
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
int printList()
{
wordListNodePtr current = head;
if (listIsEmpty())
{
printf("List is empty\n");
}
while (current!=NULL) {
printf("%s || %d\n", current->word, current->count);
}
return 0;
}
void printWord(wordListNodePtr word)
{
if (word == NULL)
{
printf("Word does not exist\n");
}
else
{
printf("Word: %s\n", word->word);
printf("Count: %i\n", word->count);
}
}
文本分析器.c
#include "WordList.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//call with TextAnalyzer filename
int main(int argc, char const *argv[])
{
FILE *ptr_file;
char buf[100];
ptr_file = fopen("Haiku.txt", "r");
if (!ptr_file)
{
printf("Error opening file!\n");
return 1;
}
//read words into linked list
while(fscanf(ptr_file, "%s", buf)!=EOF) {
addWords(buf);
}
if (listIsEmpty())
{
printf("List is empty.\n");
}
else
{
//print list
printList();
}
return 0;
}
输出:
Undefined symbols for architecture x86_64:
"_addWords", referenced from:
_main in TextAnalyzer-34dd21.o
"_listIsEmpty", referenced from:
_main in TextAnalyzer-34dd21.o
"_printList", referenced from:
_main in TextAnalyzer-34dd21.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我不知道如何修复输出。代码还没有完成,我只想知道如何修复输出中的这些错误。
-verbose
Vinhs-Macbook-Pro:project-1-vnguyen56 vinhnguyen$ gcc -v analyzer
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.13.0 -o a.out analyzer -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.1.0/lib/darwin/libclang_rt.osx.a
ld: can't link with a main executable file 'analyzer' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我正在使用 Microsoft VS 代码。
【问题讨论】:
-
你输入了什么来编译和链接它?您已经向我们展示了其中的输出,但没有向我们展示输入(可能以
gcc开头)。你记得告诉编译器WordList.c,而不仅仅是TextAnalyzer.c吗? -
我已将我的输入放在下面的 cmets 中
-
RE:
#ifndef _WordList_H:请注意,以下划线开头然后是另一个下划线或大写字母的标识符在 C (this includes the preprocessor) 中的任何地方都保留。你应该避免使用它们,即使是包含守卫。永远不要太早学习规则 :-) 特别是因为这可能是您在某处看到然后复制它的约定。如果是这样,请告诉给你看的人。
标签: c linked-list