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 iid;
 14     double value;
 15     Item(string s,int i,double d):name(s),iid(i),value(d){}
 16     Item(){}
 17     Item(string s):name(s){}
 18 };
 19
 20 ostream& operator<<(ostream&os,Item&i)
 21 {
 22     os<<i.name<<‘\t‘<<‘\t‘<<i.iid<<‘\t‘<<i.value;
 23     return os;
 24 }
 25
 26 istream& operator>>(istream&is,Item&i)
 27 {
 28     is>>i.name>>i.iid>>i.value;
 29     return is;
 30 }
 31
 32 struct sort_name
 33 {
 34     bool operator()(const Item&i1,const Item&i2) const
 35     {
 36         if(i1.name<=i2.name)
 37             return true;
 38         else
 39         {
 40             return false;
 41         }
 42     }
 43 };
 44
 45 struct find_name
 46 {
 47     string s2;
 48     find_name(string s1):s2(s1){}
 49     bool operator()(const Item&i) const
 50     {
 51         if(i.name==s2)
 52             return true;
 53         else
 54         {
 55             return false;
 56         }
 57     }
 58 };
 59
 60 void find_earse(vector<Item>&vi,const string &n)
 61 {
 62     vector<Item>::iterator p=vi.begin();
 63     while(p!=vi.end())
 64     {
 65         if(p->name==n)
 66             break;
 67         p++;
 68     }
 69     vi.erase(p);
 70 }
 71
 72 int main()
 73 {
 74     vector<Item> vi;
 75     Item item;
 76     int i;
 77     string a;
 78     cin>>a;
 79
 80     ifstream ifs(a.c_str());
 81     if(!ifs)
 82         cerr<<"can not open input file1";
 83     for(i=0;i<10;i++)
 84     {
 85         ifs>>item;
 86         vi.push_back(item);
 87     }
 88
 89     sort(vi.begin(),vi.end(),sort_name());
 90
 91     item=Item("hourse shoe",99,12.34);
 92     vi.insert(vi.begin(),item);
 93     item=Item("Canon s400",9899,499.14);
 94     vi.insert(vi.begin(),item);
 95
 96     find_earse(vi,"kj");
 97     find_earse(vi,"oj");
 98
 99     for(i=0;i<10;i++)
100     {
101         cout<<vi[i]<<endl;
102     }
103
104     vector<Item>::iterator p=find_if(vi.begin(),vi.end(),find_name("dio"));
105     cout<<*p<<endl;
106     while(1);
107     return 0;
108
109 }

简单练习

 1 int count1(In first,In last,const T&val)
 2 {
 3     int i=0;
 4     while(first!=last){
 5         if(*first==val)
 6             i++;
 7         first++;
 8     }
 9     return i;
10 }
11
12 template<class In,class Pred>
13 int count_if1(In first,In last,Pred pred)
14 {
15     int i=0;
16     while(first!=last){
17         if(pred(*first))
18             i++;
19         first++;
20     }
21     return i;
22 }

习题3 4

 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 using namespace std;
11
12 struct Fruit
13 {
14     string name;
15     int count;
16     Fruit(string s,int i):name(s),count(i){}
17 };
18
19 struct Fruit_order
20 {
21     bool operator()(const Fruit*a,const Fruit*b)const
22     {
23         return a->name<b->name;
24     }
25 };
26
27 ostream& operator<<(ostream&os,const Fruit* f)
28 {
29     os<<f->name<<"  "<<f->count;
30     return os;
31 }
32
33 int main()
34 {
35
36     set<Fruit*,Fruit_order> in;
37     in.insert(new Fruit("fa",33));
38     in.insert(new Fruit("as",83));
39     typedef set<Fruit*>::iterator s1;
40     for(s1 p=in.begin();p!=in.end();p++)
41         cout<<*p<<endl;
42     while(1);
43     return 0;
44
45 }

习题6

  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 using namespace std;
 11
 12 struct Purchase
 13 {
 14     string name;
 15     double unit_price;
 16     int count;
 17
 18 };
 19
 20 struct Order{
 21     string name;
 22     string add;
 23     double data;
 24     vector<Purchase> vp;
 25     bool operator < (const Order&o) const
 26     {
 27     //return cig_quantity>b1.cig_quantity;
 28     //按照名字的大小顺序进行排序
 29      return name>o.name;
 30     }
 31 };
 32
 33 istream& operator>>(istream&is,Purchase&p)
 34 {
 35     is>>p.name>>p.unit_price>>p.count;
 36     return is;
 37 }
 38
 39 istream& operator>>(istream&is,Order&o)
 40 {
 41     is>>o.name>>o.add>>o.data;
 42     char c;
 43     is>>c;
 44     Purchase p;
 45     while(c==‘(‘)
 46     {
 47         is>>p;
 48         o.vp.push_back(p);
 49         is>>c;
 50         is>>c;
 51     }
 52     if(c!=‘(‘)
 53         is.putback(c);
 54     return is;
 55
 56 }
 57
 58 ostream& operator<<(ostream&os,Purchase&p)
 59 {
 60     os<<p.name<<‘ ‘<<p.unit_price<<‘ ‘<<p.count;
 61     return os;
 62 }
 63
 64 ostream& operator<<(ostream&os,Order&o)
 65 {
 66     os<<o.name<<‘ ‘<<o.add<<‘ ‘<<o.data<<‘ ‘;
 67     for(int i=0;i<o.vp.size();i++)
 68         os<<‘(‘<<o.vp[i].name<<‘ ‘<<o.vp[i].unit_price<<‘ ‘<<o.vp[i].count<<‘)‘;
 69     return os;
 70
 71 }
 72
 73 struct sort_name
 74 {
 75     bool operator()(const Order&o1,const Order&o2) const
 76     {
 77         if(o1.name<=o2.name)
 78             return true;
 79         else
 80         {
 81             return false;
 82         }
 83     }
 84 };
 85
 86 int main()
 87 {
 88     vector<Order> vo;
 89     list<Order>lo;
 90     string a;
 91     int i=0;
 92     //Order o;   //注意不要放在这里
 93
 94     cin>>a;
 95     ifstream ifs(a.c_str());
 96     if(!ifs)
 97         cerr<<"can not open input file1";
 98     for(i=0;i<10;i++)
 99     {
100         Order o;     //应该放在这
101         ifs>>o;
102         vo.push_back(o);
103     }
104     ifs.close();
105     sort(vo.begin(),vo.end(),sort_name());
106
107     cin>>a;
108     fstream ofs(a.c_str());
109     if(!ofs)
110         cerr<<"can not open input file1";
111     for(i=0;i<vo.size();i++)
112         ofs<<vo[i]<<endl;
113
114
115     cin>>a;
116     ifstream ifs1(a.c_str());
117     if(!ifs1)
118         cerr<<"can not open input file1";
119     for(i=0;i<10;i++)
120     {
121         Order o;     //应该放在这
122         ifs1>>o;
123         lo.push_back(o);
124     }
125     ifs1.close();
126     lo.sort();
127
128
129     cin>>a;
130     fstream ofs1(a.c_str());
131     if(!ofs1)
132         cerr<<"can not open input file1";
133     for(list<Order>::iterator p=lo.begin();p!=lo.end();p++)
134         ofs1<<*p<<endl;
135
136
137     vector<Order> vo1(vo.size()+lo.size());
138     merge(vo.begin(),vo.end(),lo.begin(),lo.end(),vo1.begin());
139     cin>>a;
140     fstream f(a.c_str());
141     if(!f)
142         cerr<<"can not open input file1";
143
144     while(1);
145     return 0;
146
147 }

习题9

习题8   先找出最大频率   然后再遍历输出

习题10  accumulate()

习题14  map储存  s[][0]==‘s‘   size()

时间: 2024-10-12 04:37:12

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/re

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 #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 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