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()

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。