请教c语言高手

如何设计一个程序,把下面文章作为文件内容,读取后,能够计数并输出文章中所有不同单词的个数。请教高手告诉下大体思路,能写出程序更好。谢谢Eitherthewellwasve... 如何设计一个程序,把下面文章作为文件内容,读取后,能够计数并输出文章中所有不同单词的个数。
请教高手告诉下大体思路,能写出程序更好。谢谢

Either the well was very deep, or she fell very slowly, for she had
plenty of time as she went down to look about her and to wonder what was
going to happen next. First, she tried to look down and make out what
she was coming to, but it was too dark to see anything; then she
looked at the sides of the well, and noticed that they were filled with
cupboards and book-shelves; here and there she saw maps and pictures
hung upon pegs. She took down a jar from one of the shelves as
she passed; it was labelled 'ORANGE MARMALADE', but to her great
disappointment it was empty: she did not like to drop the jar for fear
of killing somebody, so managed to put it into one of the cupboards as
she fell past it.
因为要计数的原文是一本书,所以有些细节要求
用“—”连接的分成两个词,I've这样词是一个词,含有大写单词和小写单词是一样的,不同时态的都算不同单词。其他标点符号全部忽略。
虽然有些麻烦,希望有高手帮忙。用c语言。
展开
 我来答
codyboyzj
2011-06-10 · TA获得超过592个赞
知道小有建树答主
回答量:271
采纳率:0%
帮助的人:599万
展开全部
#include <stdio.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#define M 100
#define N 150

char xx[M][N];
int maxline=0,cnt=0;

struct words
{
char word[20];
int number;
}word[1000]={"\0",0};

int ReadFile()
{
FILE *fp;
int i=0;
char *p;
if( (fp=fopen("data.txt","r"))==NULL )
return 1;
while(fgets(xx[i],N,fp)!=NULL)
{
p=strchr(xx[i],'\n');
if(p) xx[i][p-xx[i]]=0;
i++;
}
maxline=i;
fclose(fp);
return 0;
}

int Word_finder()
{
int i,j,n,t,k=0;
char ww[20];
for(i=0;i<maxline;i++)
{
n=0;
for(j=0;j<=strlen(xx[i]);j++)
if(isalpha(xx[i][j]))
ww[n++]=xx[i][j];
else
{
ww[n]='\0';
cnt++;
if( strcmp(ww,"\0")!=0 )
if(k==0)
{
strcpy(word[k].word ,ww);
word[k].number++;
k++;
}
else
{
for(t=0;t<k;t++)
if( strcmp(word[t].word ,ww)==0 )
{ word[t].number++;break;}
if(t==k)
{
strcpy(word[k].word ,ww);
word[k].number ++;
k++;
}
}
n=0;
}
}
return k;
}

void main()
{
int i,k;
if(ReadFile())
{
printf("数据文件打不开!\n");
return ;
}
k=Word_finder();
printf("%d words found !\n\n",cnt);
for(i=0;i<k;i++)
printf("%20s出现了%5d次!\n",word[i].word,word[i].number);
}
tcet030840zxp
2011-06-05 · TA获得超过736个赞
知道小有建树答主
回答量:299
采纳率:0%
帮助的人:118万
展开全部
#include<iostream>
#include<map>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
map<string,int>text;
string line,word;
ifstream in("text.txt");
if(!in)
{
cerr<<"Unable to open file!"<<endl;
return -1;
}
while(getline(in,line))
{
for(size_t i=0;i!=line.size();++i)
if(!isalpha(line[i]))
line[i]=' ';
stringstream ss(line);
while(ss>>word)
++text[word];
}
cout<<text.size()<<endl;
in.clear();
in.close();
system("pause");
return 0;
}
答案为85. 此程序还可以输出每个单词·出现的次数,因要求没写出
追问
不好意思,能不能用c来写,还有一些附加条件
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
maohuiabc
2011-06-05 · TA获得超过197个赞
知道小有建树答主
回答量:113
采纳率:0%
帮助的人:153万
展开全部
C++可以吗?

我刚写的

#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <strstream>
#include <stdlib.h>
#include <ctype.h>
using namespace std;

class Tree
{
private:
map<string,int> data;
map<string,int>::iterator iter;
public:
void insert(string &str)
{
iter=data.find(str);
if(iter!=data.end() )
{
iter->second++;
}else
{
data[str]=1;
}

}
void display()
{
cout<<"共"<<data.size()<<"个单词\n"<<endl;
cout<<"单 词 |\t数 目\n"<<endl;
for(iter=data.begin();iter!=data.end();iter++)
{
cout<<iter->first;
if(iter->first.size()<8)
cout <<"\t";
cout<<"\t"<<iter->second<<endl;
}
}
};

int main()
{
Tree TR;
fstream iFile("data1.txt",ios::in);
if(!iFile)
{
cout<<"data.txt error! \n"<<endl;
}
while(!iFile.eof ())
{

char c=0;
string buf;
strstream stream;
while( !iFile.eof ()&&( iFile.get(c),!isalpha(c)));
if(isalpha(c)){
do{
stream<<c;
}while( !iFile.eof ()&&( iFile.get(c),isalpha(c)));
stream>>buf;
TR.insert (buf);
}
}
TR.display ();
system("pause");
return 0;
}
更多追问追答
追问
不好意思,不能用c来写么?
追答
我做好了,给你发邮箱吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式