/* 小词典 程序 说明: 此程序可用来 学习 C++ STL (容器部分)练习用 程序运行时需手动提供词典源 词典信息存储格式如下: $word1 %explanation item1 %explanation item2 ...... 其中首行必须是单词,并且不允许连续两行均为单词,允许一个单词多条释义 数据结构为: map<单词string 记录条数size_t> multimap<单词string 释义string> 程序各处均设置了出错标志 ,方便学习 程序一次性将词典读入内存,顺序执行,未使用特殊设计方法 */ #include <iostream> #include <fstream> #include <string> #include <vector> #include <map> using namespace std; int main(){ ifstream in_file; string file_name ,word , myline , operand; int pState = 0;// pState = 0表示下一次必须读进单词,1表示下一次必须读进释义,2表示下一次可以读进单词或释义 size_t itemsTotal = 0 ; map<string,size_t> briefScan ; multimap<string,string> wordsExplanation;//map<单词string 记录条数size_t> multimap<单词string 释义string> cout<<"Input the source file of your dictionary:"<<endl; getline ( cin , file_name ) ; in_file.open(file_name.c_str()); if(!in_file){ cout<<"Exception code: 1!!!"<<endl; system("pause"); } //读进容器 string curStr,lastStr;//curStr用来存储当前行有效内容;lastStr存储上一个单词,每次读入新单词时将重设该值 while(getline(in_file , myline)){//每次读一行 if(myline.size()<2) continue; curStr = string(myline.begin()+1,myline.end()); if(myline.at(0) == '$' ) {//如果该行以$开始,那么将此行认为是一个单词 if(pState!=0 && pState != 2){cout<<"Exception code: 2!!!"<<endl;system("pause");} if(briefScan.count(curStr) == 0){//如果单词不在容器中,则添加新的单词 briefScan.insert( make_pair ( curStr , 0 ) ); }else{//如果单词在容器中,则什么也不做 map<string,size_t>::iterator myiter = briefScan.find(curStr); if(myiter == briefScan.end()) {cout<<"Exception code: 3!!!"<<endl;system("pause");} } lastStr = curStr ; //更新当前单词 pState = 1 ; //每次读完单词,下次必须读进释义,禁止连续两次读进单词 }else if(myline.at(0) == '%'){ //如果该行以%开始 则认为是上一个单词的一条释义 if(pState!=1 && pState != 2){cout<<"Exception code: 4!!!"<<endl;system("pause");} if(lastStr == "") {cout<<"Exception code: 5!!!"<<endl;system("pause");} //cout<<"laststr:"<<lastStr<<"/tcurStr :"<<curStr<<endl; wordsExplanation.insert(make_pair(lastStr,curStr));//添加释义 //更新解释条数 map<string,size_t>::iterator myiter = briefScan.find(lastStr); if(myiter == briefScan.end()) {cout<<"Exception code: 6!!!"<<endl;system("pause");} ++ myiter->second ; pState = 2;//每次读完释义,下一行可以继续读释义,也可以读新单词 ,若继续读释义,则被认为该单词多条释义 } } in_file.close(); in_file.clear();//将文件流复位 //检测容器内数据是否正常 for(map<string,size_t>::iterator iterWords = briefScan.begin(); iterWords!= briefScan.end();++iterWords){ itemsTotal += iterWords->second ; if(wordsExplanation.count(iterWords->first) != iterWords->second){ cout<<"Exception code: 8!!!"<<endl;system("pause"); } } if(itemsTotal != wordsExplanation.size()){cout<<"Exception code: 7!!!"<<endl;system("pause");} START_SEARCH: //开始查找,下面给出了单词列表 cout<<"Total:/t"<<briefScan.size()<<" words ! "<< endl; cout<<"Words list belows:"<<endl; for(map<string,size_t>::iterator iterWords = briefScan.begin(); iterWords!= briefScan.end();++iterWords){ cout<< iterWords->first << ends ; } cout<<endl; //检索单词 cout<<"Input the words to search :"<<endl; cin>>word ; map<string,size_t>::iterator iterWords = briefScan.find(word); if(iterWords == briefScan.end()){ cout<<"Exception code: 9!!!"<<endl; goto START_SEARCH; }else{ cout<<"$"<<word<<" has "<< iterWords->second<<" explanations."<<endl; } for(multimap<string,string>::iterator iterExp = wordsExplanation.lower_bound(word); iterExp != wordsExplanation.upper_bound(word); ++ iterExp){ cout<<"%"<<iterExp->second<<endl; } cout<<" Exit(E) or Search(S) again?"<<endl; cin >> operand; if(operand =="E" || operand =="e"){ return 1; }else if(operand =="S" || operand =="s"){ goto START_SEARCH; } system("PAUSE"); return 1; }