Use C + + to practice the system common IO commands.
1) Display the text of the document
2) Statistical text word number
3) List all files in the directory, recursive thinking
4) Find the first matching character.
5) Text word sort, quick sort, in fact or recursive thinking
6) Text word sorting after removing duplicates.
In addition to 3 and 6, you can practice, the other does not make much sense.
Command.h
#ifndef command_h_included#define command_h_included#include <iostream> #include <fstream> #include " Utility.h "#include" io.h "#include" stdio.h "using namespace Std;class command{public:int WC (const string&,const CHA R); void Cat (const string&); void LS (const string& _path,int); int Search_index (const string&,const string&); void LS (const string&); Vector<string> getwords (const string&); Vector<string> sort_q (const vector<string>&); Vector<string> unique_w (const vector<string>&);}; Vector<string> command::unique_w (const vector<string>& _ws) {vector<string> temp; String Comparestr= ""; for (int i=0;i!=_ws.size (); ++i) {if (_ws[i]!=comparestr) {temp.push_back (_ws[i]); Comparestr=_ws[i]; }} return temp;} Vector<string> command::getwords (const string& _filename) {vector<string> Ret; IfstreAM in; Utility::open_file (In,_filename); String line; while (Getline (in,line)) {int frontp=0; for (int i=0;i!=line.size (); ++i) {if (line[i]== ' | | | line[i]== ', ' | | I==line.size ()-1) {Ret.push_back (Line.substr (FRONTP,I-FRONTP)); frontp=i+1; }}} return Ret;} Vector<string> command::sort_q (const vector<string>& _WS) {return Utility::sort_quick (_WS);} int COMMAND::WC (const string& _filename,const char _option) {int rstcount=0; Switch (_option) {case ' W ': {ifstream in; Utility::open_file (In,_filename); String line; while (Getline (In,line)) {for (int i=0;i!=line.size (); ++i) { if (line[i]== ' | | | line[i]== ', ') {++rstcount; } } ++rstcount; }} case ' C ': {//statistical characters. }} return rstcount;} void Command::cat (const string& _filename) {ifstream in; Utility::open_file (In,_filename); String line; while (Getline (In,line)) {cout<<line<<endl; }}struct Dir{public:int Isfolder; string name;}; void Command::ls (const string& _path,int l) {string dir=_path+ "\\*.*"; vector<_finddata_t> files; _finddata_t tmpfile; The test found that the first file's sentence cake is usually., so tmpfile does not handle it. Long lfdir= _findfirst (Dir.c_str (), &tmpfile); if (lfdir!=-1l) {while (_findnext (lfdir,&tmpfile) ==0) {string space; for (int i=0;i!=l;++i) {space+= ""; } if (tmpfile.attrib==_a_subdir&& (tmpfile.name[0])! = '. ') There seems to be something hidden. and. folder, which represents the previous folder. Here to get rid of. {printf ("%s%s\n", Space.c_str (), tmpfile.name); LS (_path+ "\ \" +tmpfile.name,l+1);//used to self-increment + +, here just to pass the l+1 to the parameters. It does not need to add 1. The else if (tmpfile.attrib!=_a_subdir)//does not have a file attribute. Can only be replaced with non-files. To avoid the above, the previous folder: to here. {printf ("%s%s\n", Space.c_str (), tmpfile.name); }}} _findclose (Lfdir);} int Command::search_index (const string& _word,const string& _filename) {ifstream in; Utility::open_file (In,_filename); String line; string lines; while (Getline (In,line)) {lines+=line; } int position=0; BOOL Pitch=false; For (Position=0;position!=lines.size (); ++position) {int index=0; Pitch=true; For (Index=0;index!=_word.size (); ++index) {if (Lines[position+index]!=_word[index]) { Pitch=false; Break }} if (pitch) {break; }} if (pitch) {return position; } else {return-1; }} #endif//Command_h_included
Utility.h
#ifndef utility_h_included#defineutility_h_included#include<iostream>#include<fstream>using namespacestd;namespaceUtility{ifstream& Open_file (Ifstream&,Const string&); Vector<string> Sort_quick (Constvector<string>_words);} Ifstream& Utility::open_file (ifstream& _in,Const string&_filename) {_in.close (); _in.clear (); _in.open (_filename.c_str ()); return_in;}//fast sequencing, performance should need to be optimized under. But the idea is to sort ideas quickly.vector<string> Utility::sort_quick (Constvector<string>_words) { if(_words.size () >=2) { stringcomparestr=_words[0]; Vector<string>Left ; Vector<string>Right ; for(intI=1; I!=_words.size (); + +i) {if(_words[i]>=comparestr) {Right.push_back (_words[i]); } Else{left.push_back (_words[i]); }} Left=Sort_quick (left); Right=Sort_quick (right); Left.push_back (COMPARESTR); for(intI=0; I!=right.size (); + +i) {left.push_back (right[i]); } returnLeft ; } Else { return_words; }}#endif //utility_h_included
Main.cpp
voidMaincommand () {command cmd; stringFilename="Text.txt"; //display textcmd.cat (filename); //statistical Word numbers intCOUTA=CMD.WC (filename,'W'); cout<<"words:"<<couta<<Endl; //list all files in directoryCmd.ls ("E:\\db\\stl",0); //finds the first match of a character.Cout<<cmd.search_index ("Ittle", filename) <<Endl; //Text Word sortvector<string> words=cmd.getwords (filename); Vector<string> sort_words=cmd.sort_q (Words); for(intI=0; I!=sort_words.size (); + +i) {cout<<sort_words[i]<<Endl; } //text Word sorting after removing duplicates.vector<string> unique_words=Cmd.unique_w (sort_words); for(intI=0; I!=unique_words.size (); + +i) {cout<<unique_words[i]<<Endl; }}
C + + operation IO Common commands