c++ 从文件中提取英文单词并排序的程序

本人初学c++,遇到难题,请高手帮帮忙,给个完整的程序.功能:从一段英文文章(保存在words.txt中)中把单词提取出来,按字典顺序排序,然后把排序后的单词输出到一个文... 本人初学c++,遇到难题,请高手帮帮忙,给个完整的程序.

功能:从一段英文文章(保存在words.txt中)中把单词提取出来,按字典顺序排序,然后把排序后的单词输出到一个文件(output.txt)中.

如文本文件内容为:
"The 28-year-old decided to quit after being omitted from England's 16-man squad for the upcoming triangular series with Australia and New Zealand, the last tournament before next year's World Cup in the West Indies.

Harmison, who made a disastrous start to the Ashes series and has struggled to regain form, said he decided to abandon the abbreviated form of the game and concentrate on tests. "

把单词"The" ,"year","old" ,"decided",..., "on", "tests"按ABCD....的顺序排序,结果保存到output.txt
展开
 我来答
百度网友8a130d3
2006-12-29
知道答主
回答量:1
采纳率:0%
帮助的人:0
展开全部
#include <fstream.h>
#include <ctype.h>
#include <string.h>
void main()
{
void SortData(int *ip,int n);

char file[]="word.txt";
char ch;
char word[100][20];
char d[20];
char ip[100];
int flag;
int i=0,j=0,n;
fstream infile,outfile;

infile.open(file,ios::in);
infile.unsetf(ios::skipws);
outfile.open("output.txt",ios::out|ios::trunc);
if(!infile|!outfile)
{
cerr<<"Con't open file "<<file<<endl;
}

while(infile>>ch)
{
if(isalpha(ch))
{
cout<<ch;
word[i][j]=ch;
j++;
}
else
{
if(j!=0)
{word[i][j]='\0';i++;j=0;}
}
}
cout<<endl;
n=i;

for(j=0;j<i;j++) cout<<word[j]<<'\t';
cout <<endl<<endl;

for(i=0;i<n-1;i++)
{
flag=n-1;
for(j=n-1;j>i;j--)

{ if(strcmp(word[j],word[j-1])<0)

{
strcpy(d,word[j]);
strcpy(word[j],word[j-1]);
strcpy(word[j-1],d);
flag=j;
}
}
if(flag==n-1)
break;
}

cout <<endl<<endl;
for(i=0;i<n;i++)
{cout<<word[i]<<'\t';
outfile.write(word[i],strlen(word[i]));
outfile<<'\n';
}
infile.close();
outfile.close();
}

vc++6.0 在win2000下运行通过
TableDI
2024-07-18 广告
仅需3步!不写公式自动完成Excel vlookup表格匹配!Excel在线免,vlookup工具,点击96步自动完成表格匹配,无需手写公式,免费使用!... 点击进入详情页
本回答由TableDI提供
戢璐桑寄柔
2019-12-12 · TA获得超过3755个赞
知道小有建树答主
回答量:3136
采纳率:25%
帮助的人:165万
展开全部
#include
<fstream.h>
#include
<ctype.h>
#include
<string.h>
void
main()
{
void
SortData(int
*ip,int
n);
char
file[]="word.txt";
char
ch;
char
word[100][20];
char
d[20];
char
ip[100];
int
flag;
int
i=0,j=0,n;
fstream
infile,outfile;
infile.open(file,ios::in);
infile.unsetf(ios::skipws);
outfile.open("output.txt",ios::out|ios::trunc);
if(!infile|!outfile)
{
cerr<<"Con't
open
file
"<<file<<endl;
}
while(infile>>ch)
{
if(isalpha(ch))
{
cout<<ch;
word[i][j]=ch;
j++;
}
else
{
if(j!=0)
{word[i][j]='\0';i++;j=0;}
}
}
cout<<endl;
n=i;
for(j=0;j<i;j++)
cout<<word[j]<<'\t';
cout
<<endl<<endl;
for(i=0;i<n-1;i++)
{
flag=n-1;
for(j=n-1;j>i;j--)
{
if(strcmp(word[j],word[j-1])<0)
{
strcpy(d,word[j]);
strcpy(word[j],word[j-1]);
strcpy(word[j-1],d);
flag=j;
}
}
if(flag==n-1)
break;
}
cout
<<endl<<endl;
for(i=0;i<n;i++)
{cout<<word[i]<<'\t';
outfile.write(word[i],strlen(word[i]));
outfile<<'\n';
}
infile.close();
outfile.close();
}
vc++6.0
在win2000下运行通过
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式