aquar
#pragma warning(disable:4786)

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <list>
#include <iomanip>
#include <algorithm>
#include <iterator>
using namespace std;

void get_word(istream& in_stream,string& w);
void insert_word(string word,list<string>&wordlist);
void display_list(ostream & out_stream,list<string>wordlist);

void main(){
ifstream in_stream;
ofstream out_stream;

string infile,outfile;//filenames
string word;          //string to hold current word

//Prompt for filenames and open files
cout <<"Enter the name of the input file:";
cin>> infile;
cout <<"Enter the name of the output file:";
cin>> outfile;

in_stream.open(infile.c_str());
if (in_stream.fail()) {
cout <<"fail to open this file" << infile <<endl;
}
else{
out_stream.open(outfile.c_str());
list<string> wordlist;
list<string>::iterator iter;
get_word(in_stream,word);    //get a word

//while non-empty word was returned.
while (word.size()) {
insert_word(word,wordlist);
get_word(in_stream,word); //get another word
}
wordlist.sort();
out_stream << "There are " << wordlist.size() << " distinct words.\n";
out_stream <<"\nHere is the ordered list of words\n";
display_list(out_stream,wordlist);
}//end esle

}

/*-----------------------------------------------------------*/

/*-----------------------------------------------------------*/
/*这个函数把一个单词不再单词表中则插入到单词列表中           */

void insert_word(string word,list<string>&wordlist){
list<string>::iterator iter;

iter=find(wordlist.begin(),wordlist.end(),word);
if(iter== wordlist.end()){
//Word is not in list. Insert word.
wordlist.insert(iter,word);
}
}

/*----------------------------------------------------------*/
/*得到输入流中的下个单词,忽视所有非字母的字符              */
void get_word(istream& in_stream,string& w){
char ch;
w = "";        //clear word

in_stream.get(ch);
while (!isalpha(ch)&&!in_stream.eof()) {//skip non-alpha
in_stream.get(ch);
}

while (isalpha(ch)&&!in_stream.eof()) {//read and store alpha
ch = tolower(ch);  //把大写字母改为小写
w +=ch;
in_stream.get(ch);
}
}

/*-----------------------------------------------------------*/
/*把列表中的单词输出到输出流中,每行三个                     */
void display_list(ostream& out_stream,list<string>wordlist){
int columns(3),counter(0);
list<string>::iterator iter;
out_stream<<setiosflags(ios::left);

iter = wordlist.begin();
while (iter!=wordlist.end()) { //循环访问list
out_stream<<setw(20)<<(*iter).c_str();
iter++;
counter++;
if (counter%columns == 0) { //定制列数
out_stream <<endl;
}
}
}

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
  • 2022-03-07
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-03-09
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案