将文本文件中的单词读入struct的有关问题
将文本文件中的单词读入struct的问题
这是一个很长的程序的一部分,最基础的一部分,但是因为我对struct的掌握不熟练,所以卡住了。希望大家能够帮我看一下。
现在我有一个叫做text.txt的文本文件,内容是:
roses are red
violets are blue
i am schizophrenic
and so am i
希望有一段代码可以统计每个词有多少个,按字母顺序输出为:
am 2
and 1
are 2
blue 1
i 2
red 1
roses 1
schizophrenic 1
so 1
violets 1
希望实现的思路是用一个struct数组,每个struct元素有单词和出现次数两部分(必须用这个结构是因为以后这个东西还有其他用处。)因为初学C,没用过stuct,我就想,我先把文件里每一个词读入struct数组每一个元素的单词这部分搞定,算是一切的开始。没想到这一步就卡住了,程序编译了运行不出东西,所以希望大家能帮我看看。我知道这个可能花费不了高手多少时间,我对指针的概念也不是很熟悉,所以在struct读入解决后可能排序上也有困难,100分求一个能够按要求运行的程序。如果完成整段有困难的话,希望能和我在帖子里讨论帮我解决我的部分困惑。十分感谢!
------解决方案--------------------
这是一个很长的程序的一部分,最基础的一部分,但是因为我对struct的掌握不熟练,所以卡住了。希望大家能够帮我看一下。
现在我有一个叫做text.txt的文本文件,内容是:
roses are red
violets are blue
i am schizophrenic
and so am i
希望有一段代码可以统计每个词有多少个,按字母顺序输出为:
am 2
and 1
are 2
blue 1
i 2
red 1
roses 1
schizophrenic 1
so 1
violets 1
希望实现的思路是用一个struct数组,每个struct元素有单词和出现次数两部分(必须用这个结构是因为以后这个东西还有其他用处。)因为初学C,没用过stuct,我就想,我先把文件里每一个词读入struct数组每一个元素的单词这部分搞定,算是一切的开始。没想到这一步就卡住了,程序编译了运行不出东西,所以希望大家能帮我看看。我知道这个可能花费不了高手多少时间,我对指针的概念也不是很熟悉,所以在struct读入解决后可能排序上也有困难,100分求一个能够按要求运行的程序。如果完成整段有困难的话,希望能和我在帖子里讨论帮我解决我的部分困惑。十分感谢!
------解决方案--------------------
- C/C++ code
while(!feof(firstFile)) { fscanf(firstFile,"%s",temp.word); struct wordCounter counter[i]; strcpy(counter[i].word,temp.word); counter[i].count = 1; printf("%s\n",counter[i].word); i++; }
------解决方案--------------------
- C/C++ code
#include <stdio.h> #include <string.h> #define MAX_LENGTH 40 #define MAX_WORD 50 void ReadText(FILE *firstFile); void insertList(char *); void output(); struct wordCounter { char word[MAX_LENGTH]; unsigned int count; }; struct wordCounter counter[MAX_WORD]; int counterSize=0; int main(void) { // Declaration FILE *text; //FILE *remove; //Body text = fopen("E:\\c++\\ListViewTest01\\C语言试验用项目\\C语言试验用项目\\text.txt", "r"); // 文件目录需要自己调整 ReadText(text); output(); // End fclose(text); } void ReadText(FILE *firstFile) { char str[40]; while(!feof(firstFile)) { fscanf(firstFile,"%s",str); insertList(str); } } void insertList(char *word) // 此函数将读入的word插入数组中,若数组中已有该word,则增量计数 { int i,j,flag=1,cmp; for (i=0;i<counterSize;++i) { cmp=strcmp(counter[i].word,word); // 比较数组中与当前字符串 if (cmp==0) // 若想等则增量计数 { counter[i].count++; flag=0; break; } if (cmp>0) // 若已大于当前字符串,需要插入word到此位置 { for (j=counterSize;j>=i;--j) // 先移开后面的元素 counter[j+1]=counter[j]; strcpy(counter[i].word,word); // 再插入当前word counter[i].count=1; // 置计数为1 ++counterSize; // 累加word总数 flag=0; // 复位标志 break; } } if (flag) // 若未找到同样word且未发生插入操作,则将word插到尾部 { strcpy(counter[counterSize].word,word); counter[counterSize++].count=1; } } void output() { int i; for (i=0;i<counterSize;++i) printf("%s %d\n",counter[i].word,counter[i].count); }
------解决方案--------------------
这个问题应该用哈希表啊,不过那是Perl语言里的,希望对你能有帮助。
------解决方案--------------------
赞一下5楼吧,5楼的处理方法是每次读1行,如何处理其中的单词,放到struct数组中。
你自己的问题还是很多,对比一下吧。
------解决方案--------------------
看看下面这个代码,我感觉非常好:
- C/C++ code
#include<stdlib.h> #include<stdio.h> #include<string.h> #define NHASH 29989//比总数 大一些的 质数,这个例子单词的总数为 299131 #define MULT 31 // 单个单词可以选择的数目大一些的质数。 这个例子为英语字母26 typedef struct node *nodeptr; typedef struct node{ char *word; int count; nodeptr next; }hashnode; nodeptr bin[NHASH]; unsigned int hash(const char *p) { unsigned int h=0; for(;*p;++p) { h=MULT*h+*p; } return h%NHASH; } void incword(const char * buf) { unsigned int h=hash(buf); nodeptr p; for(p=bin[h];p!=NULL;p=p->next) { if(strcmp(buf,p->word)==0 ) ++(p->count); return ; } p=(nodeptr)malloc(sizeof(hashnode)); p->word=(char *)malloc(strlen(buf)+1); p->count=1; strcpy(p->word,buf); p->next=bin[h]; bin[h]=p; } int main(void) { int i=0,sum=0; char buf[128]; nodeptr p; freopen("deep_cpp.txt","r",stdin); for(i=0;i<NHASH;++i) { bin[i]=NULL; } while(scanf("%s",buf)!=EOF) { incword(buf); ++sum; } for(i=0;i<NHASH;++i) { for(p=bin[i];p!=NULL;p=p->next) { printf("%s\t%d\n",p->word,p->count); } } printf("total %d words\n",sum); return 0; }