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

时间:2022-11-18 00:16:16

p425.1

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 using namespace std;
 5 
 6 class Cow{
 7     char name[20];
 8     char *hobby;
 9     double weight;
10 public:
11     Cow();
12     Cow(const char *nm, const char *ho, double wt);
13     Cow(const Cow &c);
14     ~Cow();
15     Cow &operator=(const Cow &c);
16     void ShowCow()const;
17 };
18 
19 Cow::Cow(){
20     strcpy(name, "no body");
21     hobby = "nothing";
22     weight = 0.0;
23 }
24 
25 Cow::Cow(const char *nm, const char *ho, double wt){
26     std::strncpy(name, nm, 20);
27     hobby = new char[strlen(ho) + 1];
28     strcpy(hobby, ho);
29     weight = wt;
30 }
31 
32 Cow::Cow(const Cow &c){
33     hobby = new char[strlen(c.hobby) + 1];
34     strcpy(hobby, c.hobby);
35     strcpy(name, c.name);
36     weight = c.weight;
37 }
38 
39 Cow::~Cow(){
40     delete[]hobby;
41 }
42 
43 Cow & Cow::operator=(const Cow &c){
44     if (this == &c)
45         return *this;
46     delete[]hobby;
47     hobby = new char[strlen(c.hobby) + 1];
48     strcpy(hobby, c.hobby);
49     strcpy(name, c.name);
50     weight = c.weight;
51     cout << "hobby " << hobby << endl
52         << "name " << name << endl
53         << "weight " << weight << endl;
54     return *this;
55 }
56 
57 void Cow::ShowCow()const{
58     std::cout << "name is " << name << std::endl
59         << "hobby is " << hobby << std::endl
60         << "weight is " << weight << std::endl;
61 }
62 
63 int main(){
64     Cow test1, test2("Max", "soccer", 6.7);
65     test1.ShowCow();
66     test2.ShowCow();
67     Cow test3("Stack", "vollyball", 3.45);
68     cin.get();
69     test1 = test3;
70     test1.ShowCow();
71 
72     system("pause");
73     return 0;
74 }

p426.3

  1 //头文件:
  2 #include<iostream>
  3 #include<string>
  4 using std::istream;
  5 using std::ostream;
  6 
  7 #ifndef STRING2_H_
  8 #define STRING2_H_
  9 class String{
 10 private:
 11     char *str;
 12     int len;
 13     static int num_strings;
 14     static const int CINIM = 80;
 15 public:
 16     String(const char *s);
 17     String();
 18     String(const String &);
 19     ~String();
 20     int length()const { return len; }
 21     String & operator=(const String &);
 22     String &operator=(const char*);
 23     char&operator[](int i);
 24     const char &operator[](int i)const;
 25     String & Stringlow();
 26     char * Stringup();
 27     int has(char);
 28     friend char * operator+(const String &st1, const String &st2);
 29     friend bool operator<(const String &st1, const String &st2);
 30     friend bool operator==(const String &st1, const String &st2);
 31     friend ostream &operator<<(ostream &os, const String &st);
 32     friend istream &operator>>(istream &is, String &st);
 33     static int howmany();
 34 };
 35 
 36 #endif
 37 
 38 //方法:
 39 #include<iostream>
 40 #include<cctype>
 41 #include<cstring>
 42 #include<string>
 43 #include"String2.h"
 44 
 45 using std::cin;
 46 using std::cout;
 47 using std::endl;
 48 
 49 int String::num_strings = 0;
 50 
 51 String::String(const char *s){
 52     num_strings++;
 53     len = strlen(s);
 54     str = new char[len+1];
 55     strcpy(str, s);
 56     cout << "num_strings " << num_strings << endl;
 57 }
 58 
 59 String::String(){
 60     num_strings++;
 61     len = 0;
 62     str = NULL;
 63     cout << "num_strings " << num_strings << endl;
 64 }
 65 
 66 String::String(const String &st){
 67     num_strings++;
 68     len = st.len;
 69     str = new char[len + 1];
 70     strcpy(str, st.str);
 71     cout << "num_strings " << num_strings << endl;
 72 }
 73 
 74 String::~String(){
 75     num_strings--;
 76     delete[]str;
 77     cout << "num_strings " << num_strings << endl;
 78 }
 79 
 80 String & String::operator=(const String &st){
 81     if (&st == this)
 82         return *this;
 83     delete[]str;
 84     len = st.len;
 85     str = new char[len + 1];
 86     strcpy(str, st.str);
 87     return *this;
 88 }
 89 
 90 String & String::operator=(const char*s){
 91     delete[]str;
 92     len = strlen(s);
 93     str = new char[len + 1];
 94     strcpy(str, s);
 95     return *this;
 96 }
 97 
 98 char & String::operator[](int i){
 99     return str[i];
100 }
101 
102 const char & String::operator[](int i)const{
103     return str[i];
104 }
105 
106 String & String::Stringlow(){
107     for (int i = 0; i < len; i++)
108         str[i] = tolower(str[i]);
109     return *this;
110 }
111 
112 char * String::Stringup(){
113     for (int i = 0; i < len; i++)
114         str[i] = toupper(str[i]);
115     return str;
116 }
117 
118 int String::has(char ch){
119     int count = 0;
120     for (int i = 0; i < len; i++)
121         if (str[i] == ch)
122             count++;
123     return count;
124 }
125 
126 char * operator+(const String &st1, const String &st2){
127     char *st3 = new char[st1.len + st2.len+2];
128     for (int i = 0; i < st1.len; i++)
129         st3[i] = st1[i];
130     for (int j = 0; j < st2.len; j++)
131         st3[st1.len +1+ j] = st2[j];
132     st3[st1.len] = ' ';
133     st3[st1.len + st2.len + 1] = '\0';
134     return st3;
135 }
136 
137 bool operator<(const String &st1, const String &st2){
138     if (strcmp(st1.str, st2.str))
139         return false;
140     else return true;
141 }
142 
143 bool operator==(const String &st1, const String &st2){
144     if (strcmp(st1.str, st2.str) == 0)
145         return true;
146     else return false;
147 }
148 
149 ostream &operator<<(ostream &os, const String &st){
150     os << "str: " << st.str << endl;
151     return os;
152 }
153 
154 istream &operator>>(istream &is, String &st){
155     char temp[String::CINIM];
156     is.get(temp, String::CINIM);
157     if (is)
158         st = temp;
159     while (is&&is.get() != '\n')
160         continue;
161     return is;
162 }
163 
164 int String::howmany(){
165     return num_strings;
166 }
167 
168 //驱动:
169 #include<iostream>
170 #include<cstdlib>
171 using namespace std;
172 #include "string2.h"
173 
174 int main(){
175     String s1(" and i am a C++ student. ");
176     String s2 = "please enter your name: ";
177     String s3;
178     cout << s2;
179     cin >> s3;
180     s2 = "my name is " + s3;
181     cout << s2 << ".\n";
182     s2 = s2 + s1;
183     s2.Stringup();
184     cout << "the string\n" << s2 << "\ncontains " <<
185         s2.has('A') << "'A' characters in it.\n";
186     s1 = "red";
187     String rgb[3] = { String(s1), String(" green"), String("blue") };
188     cout << "enter the name of a primary color for mixing light: ";
189     String ans;
190     bool success = false;
191     while (cin >> ans){
192         ans.Stringlow();
193         for (int i = 0; i < 3; i++){
194             if (ans == rgb[i]){
195                 cout << "that's right!\n";
196                 success = true;
197                 break;
198             }
199         }
200         if (success)
201             break;
202         else
203             cout << "try again\n";
204     }
205     cout << "bye\n";
206     system("pause");
207     return 0;
208 }