首页 > 代码库 > C++程序设计原理与实践 第二十三章部分答案
C++程序设计原理与实践 第二十三章部分答案
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <list> 5 #include<fstream> 6 #include <set> 7 #include<algorithm> 8 #include<stdexcept> 9 #include <map> 10 #include<boost/regex.hpp> 11 using namespace std; 12 13 typedef vector<string>::const_iterator Line_iter; 14 15 16 class Message{ 17 Line_iter first; 18 Line_iter last; 19 public: 20 Message(Line_iter p1,Line_iter p2):first(p1),last(p2){} 21 Line_iter begin()const{return first;} 22 Line_iter end()const{return last;} 23 }; 24 25 typedef vector<Message>::const_iterator Mess_iter; 26 27 28 struct Mail_file{ 29 string name; 30 vector<string> lines; 31 vector<Message> m; 32 Mail_file(const string&n); 33 Mess_iter begin()const{return m.begin();} 34 Mess_iter end()const {return m.end();} 35 }; 36 37 Mail_file::Mail_file(const string&n) 38 { 39 ifstream in(n.c_str()); 40 if(!in){ 41 cerr<<"no file"<<endl; 42 exit(1); 43 } 44 string s; 45 while(getline(in,s)) 46 lines.push_back(s); 47 Line_iter first =lines.begin(); 48 for(Line_iter p=lines.begin();p!=lines.end();++p) 49 { 50 if(*p=="----") 51 { 52 m.push_back(Message(first,p)); 53 first=p+1; 54 } 55 } 56 } 57 58 int is_prefix(const string&s,const string& p) 59 { 60 int n=p.size(); 61 if(string(s,0,n)==p) 62 return n; 63 return 0; 64 } 65 66 bool find_from_addr(const Message*m,string&s) 67 { 68 for(Line_iter p=m->begin();p!=m->end();++p) 69 if(int n=is_prefix(*p,"From:")) 70 { 71 s=string(*p,n); 72 return true; 73 } 74 return false; 75 } 76 77 bool find_from_sub(const Message*m,string&s) 78 { 79 for(Line_iter p=m->begin();p!=m->end();++p) 80 if(int n=is_prefix(*p,"subject:")) 81 { 82 s=string(*p,n); 83 return true; 84 } 85 return false; 86 } 87 string find_subject(const Message* m) 88 { 89 for(Line_iter p=m->begin();p!=m->end();++p) 90 if(int n=is_prefix(*p,"To:")) 91 return string(*p,n); 92 return ""; 93 } 94 95 int main() 96 { 97 Mail_file mfile("a.txt"); 98 multimap<string,const Message*>sender; 99 multimap<string,const Message*>sub1;100 101 for(Mess_iter p=mfile.begin();p!=mfile.end();++p)102 {103 const Message&m=*p;104 string s;105 if(find_from_addr(&m,s))106 sender.insert(make_pair(s,&m));107 }108 109 for(Mess_iter p=mfile.begin();p!=mfile.end();++p)110 {111 const Message&m=*p;112 string s;113 if(find_from_sub(&m,s))114 sub1.insert(make_pair(s,&m));115 }116 117 typedef multimap<string,const Message*>::const_iterator MCI;118 119 pair<MCI,MCI> pp=sender.equal_range("John Doe");120 121 for(MCI p1=pp.first;p1!=pp.second;++p1)122 cout<<find_subject(p1->second)<<endl;123 124 string str="";125 cin>>str;126 pair<MCI,MCI> pp1=sub1.equal_range(str);127 128 for(MCI p1=pp1.first;p1!=pp1.second;++p1){129 for(Line_iter l=p1->second->begin();l!=p1->second->end();l++)130 cout<<*l<<endl;131 cout<<endl<<endl;132 }133 134 135 136 while(1);137 return 0;138 139 }
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <list> 5 #include<fstream> 6 #include <set> 7 #include<algorithm> 8 #include<stdexcept> 9 #include <map> 10 #include<boost/regex.hpp> 11 using namespace std; 12 13 typedef vector<string>::const_iterator Line_iter; 14 15 16 class Message{ 17 Line_iter first; 18 Line_iter last; 19 public: 20 Message(Line_iter p1,Line_iter p2):first(p1),last(p2){} 21 Line_iter begin()const{return first;} 22 Line_iter end()const{return last;} 23 }; 24 25 typedef vector<Message>::const_iterator Mess_iter; 26 27 28 struct Mail_file{ 29 string name; 30 vector<string> lines; 31 vector<Message> m; 32 Mail_file(const string&n); 33 Mess_iter begin()const{return m.begin();} 34 Mess_iter end()const {return m.end();} 35 }; 36 37 Mail_file::Mail_file(const string&n) 38 { 39 ifstream in(n.c_str()); 40 if(!in){ 41 cerr<<"no file"<<endl; 42 exit(1); 43 } 44 string line; 45 boost::regex pat("^(\\s)*-+(\\s)*$"); 46 while(getline(in,line)) 47 lines.push_back(line); 48 Line_iter first =lines.begin(); 49 for(Line_iter p=lines.begin();p!=lines.end();++p) 50 { 51 boost::smatch matches; 52 if(boost::regex_search(*p,matches,pat)) 53 { 54 m.push_back(Message(first,p)); 55 first=p+1; 56 } 57 } 58 } 59 60 int is_prefix(const string&s,const string& p) 61 { 62 int n=p.size(); 63 if(string(s,0,n)==p) 64 return n; 65 return 0; 66 } 67 68 bool find_from_addr(const Message*m,string&s) 69 { 70 boost::regex pat("^From:([\\w\\s]*)?$"); 71 for(Line_iter p=m->begin();p!=m->end();++p) 72 { 73 boost::smatch matches; 74 if(boost::regex_search(*p,matches,pat)) 75 { 76 s=matches[1]; 77 return true; 78 } 79 80 } 81 return false; 82 } 83 84 bool find_from_sub(const Message*m,string&s) 85 { 86 boost::regex pat("^subject:([\\w\\s]*)?$"); 87 for(Line_iter p=m->begin();p!=m->end();++p) 88 { 89 boost::smatch matches; 90 if(boost::regex_search(*p,matches,pat)) 91 { 92 s=matches[1]; 93 return true; 94 } 95 } 96 return false; 97 } 98 99 string find_subject(const Message* m)100 {101 boost::regex pat("^To:([\\w\\s]*)?$");102 for(Line_iter p=m->begin();p!=m->end();++p)103 {104 boost::smatch matches;105 if(boost::regex_search(*p,matches,pat))106 {107 return matches[1];108 }109 }110 return "";111 }112 113 int main()114 {115 Mail_file mfile("a.txt");116 multimap<string,const Message*>sender;117 multimap<string,const Message*>sub1;118 119 for(Mess_iter p=mfile.begin();p!=mfile.end();++p)120 {121 const Message&m=*p;122 string s;123 if(find_from_addr(&m,s))124 sender.insert(make_pair(s,&m));125 }126 127 for(Mess_iter p=mfile.begin();p!=mfile.end();++p)128 {129 const Message&m=*p;130 string s;131 if(find_from_sub(&m,s))132 sub1.insert(make_pair(s,&m));133 }134 135 typedef multimap<string,const Message*>::const_iterator MCI;136 137 pair<MCI,MCI> pp=sender.equal_range("John Doe");138 139 for(MCI p1=pp.first;p1!=pp.second;++p1)140 cout<<find_subject(p1->second)<<endl;141 142 cout<<endl<<endl;143 string str="";144 cin>>str;145 pair<MCI,MCI> pp1=sub1.equal_range(str);146 147 for(MCI p1=pp1.first;p1!=pp1.second;++p1){148 for(Line_iter l=p1->second->begin();l!=p1->second->end();l++)149 cout<<*l<<endl;150 cout<<endl<<endl;151 }152 153 154 155 while(1);156 return 0;157 158 }
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <list> 5 #include<fstream> 6 #include <set> 7 #include<algorithm> 8 #include<stdexcept> 9 #include <map>10 #include<boost/regex.hpp>11 using namespace std;12 13 int main()14 {15 ifstream in("a.txt");16 if(!in)17 {18 cerr<<"no file"<<endl;19 exit(1);20 }21 22 boost::regex pat("(\\d{1,2}/\\d{1,2}/\\d{4})|(\\d{4}/\\d{1,2}/\\d{1,2})");23 string line;24 int i=0;25 boost::smatch matches;26 while(getline(in,line))27 {28 i++;29 if(boost::regex_search(line,matches,pat))30 cout<<i<<": "<<line<<endl;31 }32 33 34 while(1);35 return 0;36 37 }
1 // \w 不需要输入 \\w 2 3 #include <iostream> 4 #include <vector> 5 #include <string> 6 #include <list> 7 #include<fstream> 8 #include <set> 9 #include<algorithm>10 #include<stdexcept>11 #include <map>12 #include<boost/regex.hpp>13 using namespace std;14 using namespace boost;15 int main()16 {17 string a;18 getline(cin,a);19 ifstream in(a.c_str());20 if(!in)21 {22 cerr<<"no file"<<endl;23 exit(1);24 }25 26 regex pat;27 string p;28 getline(cin,p);29 try30 {31 pat=p;32 cout<<"pat: "<<pat<<endl;33 }34 catch(bad_exception)35 {36 cout<<"pat error"<<endl;37 exit(1);38 }39 string line;40 int i=0;41 smatch matches;42 while(getline(in,line))43 {44 i++;45 if(regex_search(line,matches,pat))46 cout<<i<<": "<<line<<endl;47 }48 49 50 while(1);51 return 0;52 53 }
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <list> 5 #include<fstream> 6 #include <set> 7 #include<algorithm> 8 #include<stdexcept> 9 #include <map>10 #include<boost/regex.hpp>11 using namespace std;12 13 int main()14 {15 ifstream in("a.txt");16 if(!in)17 {18 cerr<<"no file a"<<endl;19 exit(1);20 }21 ofstream out("b.txt");22 if(!out)23 {24 cerr<<"no file b"<<endl;25 exit(1);26 }27 28 boost::regex pat("^(.*\\D)?(\\d{1,2})/(\\d{1,2})/(\\d{4})(\\D.*)?$");29 boost::regex pat1("^(.*\\D)?(\\d{4})/(\\d{1,2})/(\\d{1,2})(\\D.*)?$");30 string line;31 int i=0;32 boost::smatch matches;33 while(getline(in,line))34 {35 i++;36 if(boost::regex_search(line,matches,pat)){37 out<<matches[1]<<matches[4]<<"/"<<matches[3]<<"/"<<matches[2]<<matches[5]<<endl;38 }39 else if(boost::regex_search(line,matches,pat1)){40 out<<line<<endl;41 }42 else43 out<<line<<endl;44 }45 46 47 while(1);48 return 0;49 50 }
习题9 触发eof()需要到尾了再输入才能触发
习题10 11 用一个数组使其增加 数组易于区分 数组下标 (\d{1,2})(\w...)...
习题13 可以
PS这一章的boost包装的有点久= =我都开通宵电脑让它装的 不过安装操作不难
C++程序设计原理与实践 第二十三章部分答案
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。