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 展开
功能:从一段英文文章(保存在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 展开
展开全部
#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下运行通过
#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 广告
2024-07-18 广告
仅需3步!不写公式自动完成Excel vlookup表格匹配!Excel在线免,vlookup工具,点击96步自动完成表格匹配,无需手写公式,免费使用!...
点击进入详情页
本回答由TableDI提供
展开全部
#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下运行通过
<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下运行通过
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |