c++ primer plus 习题答案(7)

p427.4

  1 //头文件:
  2 #include<iostream>
  3 #ifndef STACK_H_
  4 #define STACK_H_
  5 typedef unsigned long Item;
  6 
  7 class Stack{
  8 private:
  9     enum{MAX=10};
 10     Item *pitems;
 11     int size;
 12     int top;
 13 public:
 14     Stack(int n = 7);
 15     Stack(const Stack &st);
 16     ~Stack();
 17     bool isempty()const;
 18     bool isfull()const;
 19     bool push(const Item & item);
 20     bool pop();
 21     Stack & operator=(const Stack & st);
 22     friend std::ostream & operator<<(std::ostream & os, const Stack &st);
 23 };
 24 
 25 #endif
 26 
 27 //方法:
 28 #include<iostream>
 29 #include<cstring>
 30 #include"Stack.h"
 31 using std::cout;
 32 using std::endl;
 33 
 34 Stack::Stack(int n){
 35     size = n;
 36     top = n;
 37     pitems = new Item[n];
 38     for (int i = 0; i < size; i++)
 39         pitems[i] = 0;
 40 }
 41 
 42 Stack::Stack(const Stack &st){
 43     size = st.size;
 44     top = st.top;
 45     pitems = new Item[size];
 46     for (int i = 0; i < size; i++)
 47         pitems[i] = st.pitems[i];
 48 }
 49 
 50 Stack::~Stack(){
 51     delete[]pitems;
 52 }
 53 
 54 bool Stack::isempty()const{
 55     if (top == 0)
 56         return true;
 57     else return false;
 58 }
 59 
 60 bool Stack::isfull()const{
 61     if (top == MAX)
 62         return true;
 63     else return false;
 64 }
 65 
 66 bool Stack::push(const Item & item){
 67     if (isfull()){
 68         cout << "the stack is already full" << endl;
 69         return false;
 70     }
 71     else{
 72         Item *temp;
 73         temp = new Item[size + 1];
 74         for (int i = 0; i < size; i++)
 75             *(temp + i) = *(pitems + i);
 76         *(temp + size) = item;
 77         delete[]pitems;
 78         pitems = temp;
 79         size++;
 80         top++;
 81         return true;
 82     }
 83 }
 84 
 85 bool Stack::pop(){
 86     if (isempty()){
 87         cout << "the stack is already empty" << endl;
 88         return false;
 89     }
 90     else{
 91         Item *temp;
 92         temp = new Item[size - 1];
 93         for (int i = 0; i < (size - 1); i++)
 94             temp[i] = pitems[i];
 95         delete[]pitems;
 96         pitems = temp;
 97         size--;
 98         top--;
 99         return true;
100     }
101 }
102 
103 Stack & Stack::operator=(const Stack & st){
104     if (this == &st)
105         return *this;
106     size = st.size;
107     top = st.top;
108     delete[]pitems;
109     pitems = new Item[st.size];
110     for (int i = 0; i < size; i++)
111         pitems[i] = st.pitems[i];
112     return *this;
113 }
114 
115 std::ostream & operator<<(std::ostream & os, const Stack &st){
116     os << "size: " << st.size << endl
117         << "top: " << st.top << endl;
118     for (int i = 0; i < st.size; i++)
119         os << st.pitems[i] << " ";
120     return os;        
121 }
122 
123 //驱动:
124 #include<iostream>
125 #include<cstdlib>
126 #include"Stack.h"
127 using namespace std;
128 
129 int main(){
130     Stack ct1;
131     Stack ct2(5);
132     cout << "ct1 "<< ct1 <<endl;
133     cout << "ct2 "<< ct2 <<endl;
134     Stack ct3 = ct2;
135     cout << "ct3 "<< ct3 << endl;
136     ct1 = ct2;
137     cout << "ct1 " << ct1 << endl;
138     ct2.push(4);
139     cout << "ct2 " << ct2 << endl;
140     ct1.pop();
141     cout << "ct1 "<< ct1 << endl;
142 
143     system("pause");
144     return 0;
145 }

p474.1

  1 //头文件:
  2 class Cd{
  3 private:
  4     char performers[50];
  5     char label[20];
  6     int selections;
  7     double playtime;
  8 public:
  9     Cd(char *s1, char *s2, int n, double x);
 10     Cd(const Cd & st);
 11     Cd();
 12     virtual ~Cd();
 13     virtual void Report()const;
 14     Cd & operator = (const Cd & st);
 15 };
 16 
 17 class Classic : public Cd
 18 {
 19 private:
 20     char production[50];
 21 public:
 22     Classic(char *s1 = "nullbody1", char *s2 = "nullbody2", char *s3 = "nullbody3", int n = 0, double x = 0);
 23     Classic(const Classic & st);
 24     virtual void Report()const;
 25     Classic & operator=(const Classic & st);
 26 };
 27 
 28 //方法:
 29 #include<iostream>
 30 #include<cstring>
 31 #include"classic.h"
 32 
 33 using std::cout;
 34 using std::endl;
 35 
 36 Cd::Cd(char *s1, char *s2, int n, double x){
 37     strncpy(performers, s1, 50);
 38     performers[49] = \0;
 39     strncpy(label, s2, 20);
 40     label[19] = \0;
 41     selections = n;
 42     playtime = x;
 43 }
 44 
 45 Cd::Cd(const Cd & st){
 46     strcpy(performers, st.performers);
 47     strcpy(label, st.label);
 48     selections = st.selections;
 49     playtime = st.playtime;
 50 }
 51 
 52 Cd::Cd(){
 53     selections = 0;
 54     playtime = 0;
 55 }
 56 
 57 void Cd::Report()const{
 58     cout << "performers: " << performers << endl
 59         << "label: " << label << endl
 60         << "selections: " << selections << endl
 61         << "playtime: " << playtime << endl;
 62 }
 63 
 64 Cd & Cd::operator = (const Cd & st){
 65     if (this == &st)
 66         return *this;
 67     strcpy(performers, st.performers);
 68     strcpy(label, st.label);
 69     selections = st.selections;
 70     playtime = st.playtime;
 71     return *this;
 72 }
 73 
 74 Classic::Classic(char *s1, char *s2, char *s3, int n, double x): Cd(s2, s3, n, x){
 75     strncpy(production, s1, 50);
 76     production[49] = \0;    
 77 }
 78 
 79 Classic::Classic(const Classic & st): Cd(st){
 80     strcpy(production, st.production);
 81 }
 82 
 83 Cd::~Cd(){
 84 
 85 }
 86 
 87 void Classic::Report()const{
 88     cout << "production: " << production << endl;
 89     Cd::Report();
 90 }
 91 
 92 Classic & Classic::operator=(const Classic & st){
 93     if (this == &st)
 94         return *this;
 95     Cd::operator=(st);
 96     strcpy(production, st.production);
 97     return *this;
 98 }
 99 
100 //驱动:
101 #include<iostream>
102 #include<cstdlib>
103 using namespace std;
104 #include"classic.h"
105 void Bravo(const Cd & disk);
106 
107 int main(){
108     Cd c1("Beatles", "Capitol", 14, 35.5);
109     Classic c2 = Classic("piano sonata in B flat", "Alfred Brendel", "Philips", 2, 57.17);
110     Cd *pcd = &c1;
111 
112     cout << "using object directly\n";
113     c1.Report();
114     c2.Report();
115 
116     cout << "using type cd *pointer to objects:\n";
117     pcd->Report();
118     pcd = &c2;
119     pcd->Report();
120 
121     cout << "calling a function with a Cd reference argument:\n";
122     Bravo(c1);
123     Bravo(c2);
124     cout << "testing assignment: ";
125     Classic copy;
126     copy = c2;
127     copy.Report();
128 
129     system("pause");
130     return 0;
131 }
132 
133 void Bravo(const Cd & disk){
134     disk.Report();
135 }

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