标签:
用c++练习下 系统常见io命令。
1)显示文档的文本
2)统计文本单词数字
3)列出目录所有文件 ,递归思路
4)查找第一个匹配的字符.
5)文本单词排序, 快速排序,其实还是递归思路
6)文本单词排序后去除重复.
除了3和6,可以练下手,其他没太大意义。
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 char); 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‘: { //统计字符。 } } 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; //测试发现第一个file的句饼一般是.,所以tmpfile不处理. 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])!=‘.‘)//好像有隐藏的.和..文件夹,代表上一个文件夹.这里要去掉. { printf("%s%s\n",space.c_str(),tmpfile.name); ls(_path+"\\"+tmpfile.name,l+1);//习惯了自增++,这里只是要把l+1后传给参数.本身不需要加1. } else if(tmpfile.attrib!=_A_SUBDIR)//没有文件属性.只能用非文件来替换.以免上面的,上一文件夹..到这里. { 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 #define UTILITY_H_INCLUDED #include <iostream> #include <fstream> using namespace std; namespace Utility{ ifstream& open_file(ifstream&,const string&); vector<string> sort_quick(const vector<string> _words); } ifstream& Utility::open_file(ifstream& _in,const string& _filename) { _in.close(); _in.clear(); _in.open(_filename.c_str()); return _in; } //快速排序,性能应该需要优化下.但思路是快速排序思路. vector<string> Utility::sort_quick(const vector<string> _words) { if(_words.size()>=2) { string compareStr=_words[0]; vector<string> left; vector<string> right; for(int i=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(int i=0;i!=right.size();++i) { left.push_back(right[i]); } return left; } else { return _words; } } #endif // UTILITY_H_INCLUDED
main.cpp
void mainCommand() { command cmd; string filename="text.txt"; //显示文本 cmd.cat(filename); //统计单词数字 int couta=cmd.wc(filename,‘w‘); cout<<"words:"<<couta<<endl; //列出目录所有文件 cmd.ls("E:\\db\\stl",0); //查找第一个匹配的字符. cout<<cmd.search_index("ittle",filename)<<endl; //文本单词排序 vector<string> Words=cmd.getwords(filename); vector<string> sort_words=cmd.sort_q(Words); for(int i=0;i!=sort_words.size();++i) { cout<<sort_words[i]<<endl; } //文本单词排序后去除重复. vector<string> unique_words=cmd.unique_w(sort_words); for(int i=0;i!=unique_words.size();++i) { cout<<unique_words[i]<<endl; } }
标签:
原文地址:http://www.cnblogs.com/lsfv/p/5843090.html