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 2

  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 }

习题3

 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 }

习题6

 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     try
30     {
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 }

习题8

 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         else
43             out<<line<<endl;
44     }
45
46
47     while(1);
48     return 0;
49
50 }

习题12

习题9 触发eof()需要到尾了再输入才能触发

习题10  11 用一个数组使其增加  数组易于区分  数组下标   (\d{1,2})(\w...)...

习题13  可以

PS这一章的boost包装的有点久= =我都开通宵电脑让它装的     不过安装操作不难

时间: 2024-12-21 19:20:10

C++程序设计原理与实践 第二十三章部分答案的相关文章

C++程序设计原理与实践 第二十一章部分答案

1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <list> 5 #include<fstream> 6 #include<algorithm> 7 #include<stdexcept> 8 using namespace std; 9 10 struct Item 11 { 12 string name; 13 int i

C++程序设计原理与实践 第二十七章部分答案

1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<assert.h> 4 5 6 struct List 7 { 8 struct Link*first; 9 struct Link*last; 10 }; 11 12 struct Link 13 { 14 struct Link*pre; 15 struct Link*suc; 16 }; 17 18 void init(struct List*lst) 19 {

C++程序设计原理与实践 第五章部分答案

1 #include "../../st.h" 2 3 int main() 4 { 5 vector<double> nums; 6 double t; 7 int n; 8 cout<<"input how many nums: "; 9 cin>>n; 10 cin>>t; 11 nums.push_back(t); 12 int i=0; 13 while(cin>>t) 14 { 15 if(i&

c++程序设计原理与实践 第四章部分答案

1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a=1,b=100; 7 int f=1; 8 char c=0; 9 10 while(f<=7 && (b-a)>=1) 11 { 12 cout<<"你的数小等于"<<(a+b)/2<<"吗?(y/n)"; 13 cin>>c; 14

C++程序设计原理与实践 第十七章部分答案

1 #include <iostream> 2 using namespace std; 3 4 void to_lower(char* s) 5 { 6 while(*s!='\0') 7 { 8 if(*s>='A'&&*s<='Z') 9 *s+=32; 10 s++; 11 } 12 } 13 14 char* strdup1(const char*s) 15 { 16 char *p=new char[]; 17 cout<<sizeof(s)

C++程序设计原理与实践 第十一章部分答案

1 #include "../../st.h" 2 3 int main() 4 try{ 5 string s1="a.txt"; 6 string s2="z.txt"; 7 ifstream ifs(s1.c_str()); 8 if(!ifs) 9 error("can not open input file",s1); 10 ofstream ofs(s2.c_str()); 11 if(!ofs) 12 error

c++程序设计原理与实践 第二十四章部分答案

1 double f(double& d) 2 { 3 d*=2; 4 return d; 5 } 6 7 void f1(double&d) 8 { 9 d*=2; 10 } 11 12 double f2(double& d) 13 { 14 d*=2; 15 return d; 16 } 习题1 1 class f3{ 2 int i; 3 public: 4 f3(int i1):i(i1){} 5 //double operator()(double d){return

C++程序设计原理与实践 第二十六章部分答案

1 bool fn(vector<int>::iterator vi1,vector<int>::iterator vi2,int i) 2 { 3 vector<int>::iterator vi3=vi2-1; 4 if(vi1>vi3) 5 return 0; 6 else 7 { 8 vector<int>::iterator vi=vi1+(vi2-vi1)/2; 9 if(*vi==i) 10 return 1; 11 else if(*v

C++程序设计原理与实践 第二十章部分答案

1 //运行错误= = 2 3 #include <iostream> 4 #include <vector> 5 #include <list> 6 using namespace std; 7 8 typedef vector<char> Line; 9 10 11 12 struct Document 13 { 14 list<Line> line; 15 Document(){line.push_back(Line());} 16 Tex