首页 > 代码库 > C++程序设计原理与实践 第十九章部分答案

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

 1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5  6 template<class T> void f(vector<T>&v1,vector<T>&v2)   //v2不设为const因为可能v2=v1    习题1 7 { 8     if(v1.capacity()<=v2.size()) 9     {10         v1.resize(v2.size());11     }12     int i;13     for(i=0;i<v1.size();i++)14         v1[i]+=v2[i];15     for(;i<v2.size();i++)16         v1.push_back(v2[i]);17 }18 19 template<class T,class U> void f1(vector<T>&v1,vector<U>&v2,vector<U>&v3)      //习题220 {21     int i=v1.size()<v2.size()?v1.size():v2.size();22     for(int j=0;j<i;j++)23         v3.push_back(v1[j]*v2[j]);24 }25 26 template<class T> class Variable{           //习题327 public:28     T name;29     double value;30     Variable(T n,double v):name(n),value(v){}31 };32 33 vector<Variable<string>> v;34 35 template<class T>double get_value(T s,vector<Variable<T>>&v)36 {37     for(int i=0;i<v.size();i++)38         if(v[i].name==s)39             return v[i].value;40 //    cerr>>"get,undefined variable"<<endl;41 }42 43 template<class T>void set_value(T s,double d,vector<Variable<T>>&v)44 {45     for(int i=0;i<v.size();i++)46         if(v[i].name==s){47             v[i].value=http://www.mamicode.com/d;48             return ;49         }50     //    cerr>>"set.undefined variable"<<endl;51 }
习题1 2 3
  1 #include <iostream>  2 #include <vector>  3 #include <string>  4 using namespace std;  5   6 template<class T>class Link{  7 public:  8     T value;  9      10     Link(const T&v,Link*p=0,Link*s=0) 11         :value(v),prev(p),succ(s){} 12     Link* insert(Link*n); 13     Link* add(Link*n); 14     Link* erase(); 15     Link* find(const T&s); 16     const Link* find(const T&s)const; 17     Link* advance(int n)const; 18  19     Link* next()const{return succ;} 20     Link* previous()const{return succ;} 21  22  23 private: 24     Link* prev; 25     Link* succ; 26 }; 27  28 template<class T> Link<T>* Link<T>::insert(Link* n) 29 { 30     if(n==0) 31         return this; 32     if(this==0) 33         return n; 34     n->succ=this; 35     if(prev) 36         prev->succ=n; 37     n->prev=prev; 38     prev=n; 39     return n; 40 } 41  42 template<class T>Link<T>* Link<T>::add(Link* n) 43 { 44     if(n==0) 45         return this; 46     if(this==0) 47         return n; 48     n->prev=this; 49     if(succ) 50         succ->prev=n; 51     n->succ=succ; 52     succ=n; 53     return this; 54 } 55  56 template<class T> Link<T>* Link<T>::erase() 57 { 58     if(this==0) 59         return 0; 60     if(this->succ) 61         this->succ->prev=this->prev; 62     if(this->prev) 63         this->prev->succ=this->succ; 64     return this->succ; 65 } 66  67 template<class T>Link<T>* Link<T>::find(const T&s) 68 { 69     Link* p=this; 70     while(p) 71     { 72         if(p->value=http://www.mamicode.com/=s) 73             return p; 74         p=p->succ; 75     } 76     return 0; 77 } 78  79 template<class T>const Link<T>* Link<T>::find(const T&s)const 80 { 81     const Link* p=this; 82     while(p) 83     { 84         if(p->value=http://www.mamicode.com/=s) 85             return p; 86         p=p->succ; 87     } 88     return 0; 89 } 90  91 template<class T>Link<T>* Link<T>::advance(int n)const 92 { 93     const Link* p=this; 94     if(this==0) 95         return 0; 96     if(0<n) 97     { 98         while(n--) 99         {100             if(p->succ==0)101                 return 0;102             p=p->succ;103         }104     }105     if(n<0)106     {107         while(n++)108         {109             if(p->prev==0)110                 return 0;111             p=p->prev;112         }113     }114     return (Link*)p;115 }116 117 template<class T>void print_all(Link<T>*p)118 {119     cout<<"{";120     while(p)121     {122         cout<<p->value;123         if(p=p->next())124             cout<<",";125     }126     cout<<"}";127 }128 129 int main()130 {131     Link<string>* norse_gods=new Link<string>("Thor");132     norse_gods=norse_gods->insert(new Link<string>("Odin"));133     norse_gods=norse_gods->insert(new Link<string>("Zeus"));134     norse_gods=norse_gods->insert(new Link<string>("Freia"));135 136     Link<string>* greek_gods=new Link<string>("Hera");137     greek_gods=greek_gods->insert(new Link<string>("Athena"));138     greek_gods=greek_gods->insert(new Link<string>("Mars"));139     greek_gods=greek_gods->insert(new Link<string>("Poseidon"));140 141     Link<string>*p=greek_gods->find("Mars");142     if(p)143         p->value=http://www.mamicode.com/"Ares";144 145     Link<string>*p2=norse_gods->find("Zeus");146     if(p2)147     {148         if(p2==norse_gods)149             norse_gods=p2->next();150         p2->erase();151         greek_gods=greek_gods->insert(p2);152     }153 154     print_all(norse_gods);155     cout<<endl;156     print_all(greek_gods);157     cout<<endl;158     while(1);159     return 0;160     161 }
习题4
 1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5  6 template<class T> class Number 7 { 8 public: 9     Number(T i1=0):ii(i1){}10     Number(Number<T>&i):ii(i.ii){}11     Number(const Number<T>&i):ii(i.ii){}12 13     Number<T>&operator=(const Number<T>&i);14     Number<T> operator+(const Number<T>&i);15     Number<T> operator-(const Number<T>&i);16     Number<T> operator*(const Number<T>&i);17     Number<T> operator/(const Number<T>&i);18     Number<T> operator%(const Number<T>&i);19     T re_int(){return ii;}20     friend istream& operator>>(istream&is,Number<T>&i){return is>>i.ii;}21     22 private:23     T ii;24 };25 26 template<class T> Number<T>& Number<T>::operator=(const Number<T>&i)27 {28     ii=i.ii;29     return *this;30 }31 32 template<class T> Number<T> Number<T>::operator+(const Number<T>&i)33 {34     Number<T> i1;35     i1.ii=ii+i.ii;36     return i1;37 }38 39 template<class T> Number<T> Number<T>::operator-(const Number<T>&i)40 {41     Number<T> i1;42     i1.ii=ii-i.ii;43     return i1;44 }45 46 template<class T> Number<T> Number<T>::operator*(const Number<T>&i)47 {48     Number<T> i1;49     i1.ii=ii*i.ii;50     return i1;51 }52 template<class T> Number<T> Number<T>::operator/(const Number<T>&i)53 {54     Number<T> i1;55     i1.ii=ii/i.ii;56     return i1;57 }58 59 template<class T> Number<T> Number<T>::operator%(const Number<T>&i)60 {61     Number<T> i1;62     i1.ii=ii%i.ii;63     return i1;64 }65 66 template<class T> ostream& operator<<(ostream& os,Number<T>&i){return os<<i.re_int();}67 68 69 int main()70 {71     Number<int> i;72     cout<<i<<endl;73     cin>>i;74     //Number<double> d=2.6;75     cout<<i<<endl;76     int i1=3;77     cout<<i%i1<<endl;78     //cout<<d<<endl;79     while(1);80     return 0;81 }
习题5 6
1 //把ii改成public2 //加入以下函数3 template<class T1,class T2> Number<T2> operator*(Number<T1>&i1,Number<T2>&i2)4 {5     Number<T2> i3;6     i3.ii=i1.ii*i2.ii;7     return i3;8 }
习题7
 1 template<class T> class allocator1{ 2 public: 3     T* allocate(int n); 4     void dellocate(T*p,int n); 5     void construct(T*p,const T&v); 6     void destory(T*p); 7 }; 8  9 template<class T> T* allocator1<T>::allocate(int n)10 {11     if(n<=0)12         return 0;13     T* t=(T*)malloc(n*sizeof(T));14     return t;15 16     /*if (n<= 0)17     {18         return 0 ;19     }20    21     void* pMem = nullptr ;22     if (max_size() < n || (pMem = malloc(n * sizeof(value_type))) == NULL)23     {24         throw std::bad_alloc(0);25     }26   27     return static_cast <pointer>(pMem);*/28 29 }30 31 template<class T> void allocator1<T>::dellocate(T*p,int n)32 {33     free(p);34     p=NULL;35 }36 37 template<class T> void allocator1<T>:: construct(T*p,const T&v)38 {39     ::new ((void *)p) T(v);        //////////////////////////////////////注意40 //    *p=v;41 }42 43  template<class T> void allocator1<T>::destory(T*p)44  {45      p->~T();46  }
习题8
 1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5  6 template<class T> 7 class auto_ptr 8 { 9 private:10     T*ap;11 12 13 public:14 //constructor&destructor-----------------------------------(1)15     explicit auto_ptr(T*ptr=0)throw():ap(ptr){}16 17     ~auto_ptr()18         throw()19     {20         delete ap;21     }22     //Copy&assignment--------------------------------------------(2)23     auto_ptr(auto_ptr&rhs)throw():ap(rhs.release()){}24 25     template<class Y>26     auto_ptr(auto_ptr<Y>&rhs)throw():ap(rhs.release()){}27 28     auto_ptr&operator=(auto_ptr&rhs)throw()29     {30         reset(rhs.release());31         return *this;32     }33 34     template<class Y>35     auto_ptr&operator=(auto_ptr<Y>&rhs)throw()36     {37         reset(rhs.release());38         return*this;39     }40     //Dereference----------------------------------------------------(3)41     T&operator*()const throw()42     {43         return*ap;44     }45     T*operator->()const throw()46     {47         return ap;48     }49     //Helperfunctions------------------------------------------------(4)50     //valueaccess51     T*get()const throw()52     {53         return ap;54     }55     //releaseownership56     T*release()throw()57     {58         T* tmp(ap);59         ap=0;60         return tmp;61     }62     //resetvalue63     void reset(T*ptr=0)throw()64     {65         if(ap!=ptr)66         {67             delete ap;68             ap=ptr;69         }70     }71     //Special conversions-----------------------------------------------(5)72     template<class Y>73     struct auto_ptr_ref74     {75         Y*yp;76         auto_ptr_ref(Y*rhs):yp(rhs){}77     };78     auto_ptr(auto_ptr_ref<T>rhs)throw():ap(rhs.yp){}79 80     auto_ptr&operator=(auto_ptr_ref<T>rhs)throw()81     {82         reset(rhs.yp);83         return*this;84     }85 86     template<class Y>87     operator auto_ptr_ref<Y>()throw()88     {89         return auto_ptr_ref<Y>(release());90     }91     template<class Y>92     operator auto_ptr<Y>()throw()93     {94         return auto_ptr<Y>(release());95     }96 };
习题 10
 1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5  6 template<class T> 7 class counted_ptr{ 8 private: 9     T* t;10     int* n;11 12     void dispose() {13            if (--*n== 0) {14                 delete n;15                 delete t;16            }17        }18 public:19     counted_ptr(T*p=0):t(p),n(new int(1)){}20     ~counted_ptr()21     {22         dispose();23     }24 25     counted_ptr (const counted_ptr<T>& p) throw()26         : t(p.t), n(p.n) {27            ++*n;28        }29 30      counted_ptr<T>& operator= (const counted_ptr<T>& p) throw() {31            if (this != &p) {32                dispose();33                t = p.t;34                n= p.n;35                ++*n;36            }37            return *this;38        }39 40     T&operator*()const throw()41     {42         return *t;43     }44     T*operator->()const throw()45     {46         return t;47     }48 49     int* re_int(){return n;}50 51 };52 53 int main()54 {55     string s1="hello";56     counted_ptr<string> c1(&s1);57     counted_ptr<string> c2;58     counted_ptr<string> c3;59     c2=c1;60     c3=c2;61     cout<<*c1.re_int()<<endl;62     while(1);63     return 0;64 }
习题11
 1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5  6  7  8 template<class T> 9 class vector1{10 public:11     vector<T>* t;12     vector1(){t=new vector<T>;}13     void push_back(T t1)14     {15         (*t).push_back(t1);16     }17 18     T& operator[](int n){return (*t)[n];}19 };20 21 22 23 int main()24 {25     vector1<int> i;26     i.push_back(1);27     i.push_back(7);28     i.push_back(2);29     cout<<sizeof(i)<<endl;30     cout<<i[1]<<endl;31     while(1);32     return 0;33 }
习题16

 

 

习题14 15  因为用到了GUI= =放弃了

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