C++ Primer Plus编程练习答案——第14章
Posted 开心果壳好硬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ Primer Plus编程练习答案——第14章相关的知识,希望对你有一定的参考价值。
1 // chapter14_1_wine.h 2 3 #ifndef LEARN_CPP_CHAPTER14_1_WINE_H 4 #define LEARN_CPP_CHAPTER14_1_WINE_H 5 6 #include <valarray> 7 #include <string> 8 9 typedef std::valarray<int> ArrayInt; 10 typedef std::pair<ArrayInt, ArrayInt> PairArray; 11 12 class Wine { 13 private: 14 std::string label; 15 int numofyears; 16 PairArray year_bots; 17 public: 18 Wine(); 19 Wine(const char * l, int y, const int ye[], const int bot[]); 20 Wine(const char * l, int y); 21 ~Wine(); 22 void GetBottles(); 23 std::string & Label(); 24 int sum() const; 25 void show() const; 26 }; 27 28 #endif //LEARN_CPP_CHAPTER14_1_WINE_H 29 30 31 // chapter14_1_wine.cpp 32 33 #include "chapter14_1_wine.h" 34 #include <iostream> 35 36 Wine::Wine() { 37 label = "none"; 38 numofyears = 0; 39 year_bots.first = ArrayInt(numofyears); 40 year_bots.second = ArrayInt(numofyears); 41 } 42 43 Wine::Wine(const char *l, int y, const int *ye, const int *bot) { 44 label = l; 45 numofyears = y; 46 year_bots.first = ArrayInt(numofyears); 47 year_bots.second = ArrayInt(numofyears); 48 for (int i = 0; i < numofyears; ++ i) { 49 year_bots.first[i] = ye[i]; 50 year_bots.second[i] = bot[i]; 51 } 52 } 53 54 Wine::Wine(const char *l, int y) { 55 label = l; 56 numofyears = y; 57 year_bots.first = ArrayInt(numofyears); 58 year_bots.second = ArrayInt(numofyears); 59 } 60 61 Wine::~Wine() { 62 63 } 64 65 void Wine::GetBottles() { 66 using namespace std; 67 for (int i = 0; i < numofyears; ++ i) { 68 cout << "#" << i << " year: "; 69 cin >> year_bots.first[i]; 70 cout << "#" << i << " bottles: "; 71 cin >> year_bots.second[i]; 72 } 73 cout << "finished" << endl; 74 } 75 76 std::string &Wine::Label() { 77 return label; 78 } 79 80 int Wine::sum() const { 81 return year_bots.second.sum(); 82 } 83 84 void Wine::show() const { 85 using namespace std; 86 cout << "year\\tbottles" << endl; 87 for (int i = 0; i < numofyears; ++ i) 88 cout << year_bots.first[i] << "\\t" << year_bots.second[i] << endl; 89 } 90 91 92 // run 93 94 void ch14_1() { 95 using namespace std; 96 cout << "enter name of wine: "; 97 char lab[50]; 98 cin.getline(lab, 50); 99 cout << "enter number of years: "; 100 int yrs; 101 cin >> yrs; 102 103 Wine holding(lab, yrs); 104 holding.GetBottles(); 105 holding.show(); 106 107 const int YRS = 3; 108 int y[YRS] = {1993, 1995, 1998}; 109 int b[YRS] = {48, 60, 72}; 110 Wine more("Gushing Grape Red", YRS, y, b); 111 more.show(); 112 cout << "total bottles for " << more.Label() << ": " << more.sum() << endl; 113 cout << "bye" << endl; 114 return; 115 }
1 // chatper14_2_wine2.h 2 3 #ifndef LEARN_CPP_CHAPTER14_2_WINE2_H 4 #define LEARN_CPP_CHAPTER14_2_WINE2_H 5 6 #include <valarray> 7 #include <string> 8 9 typedef std::valarray<int> ArrayInt; 10 typedef std::pair<ArrayInt, ArrayInt> PairArray; 11 12 class Wine2 : private std::string, private PairArray { 13 private: 14 int numofyears; 15 public: 16 Wine2(); 17 Wine2(const char * l, int y, const int ye[], const int bot[]); 18 Wine2(const char * l, int y); 19 ~Wine2(); 20 void GetBottles(); 21 std::string & Label(); 22 int sum() const; 23 void show() const; 24 }; 25 26 #endif //LEARN_CPP_CHAPTER14_2_WINE2_H 27 28 29 // chapter14_2_wine2.cpp 30 31 #include "chapter14_2_wine2.h" 32 #include <iostream> 33 34 Wine2::Wine2() : std::string("none"), PairArray(ArrayInt(0),ArrayInt(0)){ 35 numofyears = 0; 36 } 37 38 Wine2::Wine2(const char *l, int y, const int *ye, const int *bot) 39 : std::string(l), PairArray(ArrayInt(y),ArrayInt(y)){ 40 numofyears = y; 41 for (int i = 0; i < numofyears; ++ i) { 42 PairArray::first[i] = ye[i]; 43 PairArray::second[i] = bot[i]; 44 } 45 } 46 47 Wine2::Wine2(const char *l, int y) 48 : std::string(l), PairArray(ArrayInt(y),ArrayInt(y)){ 49 numofyears = y; 50 } 51 52 Wine2::~Wine2() { 53 54 } 55 56 void Wine2::GetBottles() { 57 using namespace std; 58 for (int i = 0; i < numofyears; ++ i) { 59 cout << "#" << i << " year: "; 60 cin >> PairArray::first[i]; 61 cout << "#" << i << " bottles: "; 62 cin >> PairArray::second[i]; 63 } 64 cout << "finished" << endl; 65 } 66 67 std::string &Wine2::Label() { 68 return (std::string &)*this; 69 } 70 71 int Wine2::sum() const { 72 return PairArray::second.sum(); 73 } 74 75 void Wine2::show() const { 76 using namespace std; 77 cout << "year\\tbottles" << endl; 78 for (int i = 0; i < numofyears; ++ i) 79 cout << PairArray::first[i] << "\\t" << PairArray::second[i] << endl; 80 } 81 82 83 // run 84 85 void ch14_2() { 86 using namespace std; 87 cout << "enter name of wine: "; 88 char lab[50]; 89 cin.getline(lab, 50); 90 cout << "enter number of years: "; 91 int yrs; 92 cin >> yrs; 93 94 Wine2 holding(lab, yrs); 95 holding.GetBottles(); 96 holding.show(); 97 98 const int YRS = 3; 99 int y[YRS] = {1993, 1995, 1998}; 100 int b[YRS] = {48, 60, 72}; 101 Wine2 more("Gushing Grape Red", YRS, y, b); 102 more.show(); 103 cout << "total bottles for " << more.Label() << ": " << more.sum() << endl; 104 cout << "bye" << endl; 105 return; 106 }
1 //chatper14_3_queuetp.h 2 3 #ifndef LEARN_CPP_CHAPTER14_3_QUEUETP_H 4 #define LEARN_CPP_CHAPTER14_3_QUEUETP_H 5 6 #include <string> 7 8 class Worker { 9 private: 10 std::string fullname; 11 long id; 12 public: 13 Worker(); 14 Worker(const std::string & s, long n); 15 ~Worker(); 16 Worker & operator=(const Worker & w); 17 void show() const; 18 }; 19 20 template <typename Type> 21 class QueueTp { 22 private: 23 enum {MAX = 100}; 24 Type items[MAX]; 25 int front; 26 int rear; 27 public: 28 QueueTp(); 29 ~QueueTp(); 30 bool isEmpty(); 31 bool isFull(); 32 bool enQueue(const Type &t); 33 bool deQueue(Type &t); 34 int num() const {return (rear - front + MAX) % MAX;} 35 }; 36 37 template<typename Type> 38 QueueTp<Type>::QueueTp() { 39 front = rear = 0; 40 } 41 42 template<typename Type> 43 QueueTp<Type>::~QueueTp() { 44 45 } 46 47 template<typename Type> 48 bool QueueTp<Type>::isEmpty() { 49 if (front == rear && front == 0) 50 return true; 51 return false; 52 } 53 54 template<typename Type> 55 bool QueueTp<Type>::isFull() { 56 if ((rear + 1) % MAX == front) 57 return true; 58 return false; 59 } 60 61 template<typename Type> 62 bool QueueTp<Type>::enQueue(const Type &t) { 63 if (isFull()) 64 return false; 65 items[rear] = t; 66 rear = (rear + 1) % MAX; 67 return true; 68 } 69 70 template<typename Type> 71 bool QueueTp<Type>::deQueue(Type &t) { 72 if (isEmpty()) 73 return false; 74 t = items[front]; 75 front = (front + 1) % MAX; 76 return true; 77 } 78 79 #endif //LEARN_CPP_CHAPTER14_3_QUEUETP_H 80 81 82 83 // chapter14_3_queuetp.cpp 84 85 #include "chapter14_3_queuetp.h" 86 #include <iostream> 87 88 Worker::Worker() 89 : fullname("none"){ 90 id = 0; 91 } 92 93 Worker::Worker(const std::string &s, long n) 94 : fullname(s){ 95 id = n; 96 } 97 98 Worker::~Worker() { 99 100 } 101 102 Worker & Worker::operator=(const Worker & w) { 103 fullname = w.fullname; 104 id = w.id; 105 return *this; 106 } 107 108 void Worker::show() const { 109 using namespace std; 110 cout << "name: " << fullname << endl; 111 cout << "id: " << id << endl; 112 } 113 114 115 // run 116 117 void ch14_3() { 118 using namespace std; 119 Worker * lolas[5]; 120 Worker * t; 121 for (int i = 0; i < 5; ++ i) 122 lolas[i] = new Worker("worker" + i, i); 123 QueueTp<Worker *> q; 124 q.enQueue(lolas[0]); 125 q.enQueue(lolas[1]); 126 q.enQueue(lolas[2]); 127 cout << q.num() << "items in q" << endl; 128 q.enQueue(lolas[3]); 129 q.enQueue(lolas[4]); 130 cout << q.num() << "items in q" << endl; 131 q.deQueue(t); 132 cout << q.num() << "items in q" << endl; 133 }
1 // chapter14_4_person.h 2 3 #ifndef LEARN_CPP_CHAPTER14_4_PERSON_H 4 #define LEARN_CPP_CHAPTER14_4_PERSON_H 5 6 #include <iostream> 7 8 class Personn { 9 private: 10 std::string firstname; 11 std::string lastname; 12 public: 13 Personn(); 14 Personn(std::string f, std::string l); 15 ~Personn(); 16 virtual void show() const; 17 }; 18 19 class Gunslmger : virtual public Personn { 20 private: 21 int num; 22 public: 23 Gunslmger(); 24 Gunslmger(std::string f, std::string l, int n); 25 ~Gunslmger(); 26 double draw() const; 27 virtual void show() const; 28 }; 29 30 class PokerPlayer : virtual public Personn { 31 private: 32 int poke; 33 public: 34 PokerPlayer(); 35 PokerPlayer(std::string f, std::string l, int p); 36 ~PokerPlayer(); 37 int draw() const; 38 virtual void show() const; 39 }; 40 41 class BadDude : public PokerPlayer, public Gunslmger { 42 public: 43 BadDude(); 44 BadDude(std::string f, std::string l, int n, int p); 45 ~BadDude(); 46 double Gdraw() const; 47 int Cdraw() const; 48 virtual void show() const; 49 }; 50 51 52 53 #endif //LEARN_CPP_CHAPTER14_4_PERSON_H 54 55 // chapter14_4_person.cpp 56 57 #include "chapter14_4_person.h" 58 #include <iostream> 59 60 Personn::Personn() 61 : firstname("1 // chapter09_golf.h 2 3 #ifndef LEARN_CPP_CHAPTER09_GOLF_H 4 #define LEARN_CPP_CHAPTER09_GOLF_H 5 6 #include <cstring> 7 #include <iostream> 8 9 const int Len = 40; 10 struct golf { 11 char fullname[Len]; 12 int handicap; 13 }; 14 15 void setgolf(golf &, const char *, int); 16 int setgolf(golf &); 17 void handicap(golf &, int); 18 void showgolf(const golf &); 19 20 21 22 #endif //LEARN_CPP_CHAPTER09_GOLF_H 23 24 25 26 27 28 // chapter09_golf.cpp 29 30 #include "chapter09_golf.h" 31 32 void setgolf(golf & g, const char * name, int hc) { 33 strcpy(g.fullname, name); 34 g.handicap = hc; 35 } 36 37 int setgolf(golf & g) { 38 using namespace std; 39 cout << "enter name: "; 40 cin.getline(g.fullname, Len); 41 cout << "enter hc: "; 42 cin >> g.handicap;cin.get(); 43 if (strcmp("\\0", g.fullname) == 0) 44 return 0; 45 return 1; 46 } 47 48 void handicap(golf & g, int hc) { 49 g.handicap = hc; 50 } 51 52 void showgolf(const golf & g) { 53 using namespace std; 54 cout << "name: " << g.fullname << endl; 55 cout << "hc: " << g.handicap << endl; 56 } 57 58 59 60 61 62 // run 63 64 void ch9_1() { 65 using namespace std; 66 golf arr_golf[3]; 67 cout << "enter golf: "; 68 int i; 69 for (i = 0; i < 3; ++ i) { 70 if (setgolf(arr_golf[i]) == 0) 71 break; 72 } 73 cout << "enter done" << endl; 74 for (int j = 0; j < i; ++ j) 75 showgolf(arr_golf[j]); 76 }
1 void ch9_2_strcount(std::string & str) { 2 using namespace std; 3 static int total = 0; 4 int count = 0; 5 cout << "\\"" << str << "\\" contains "; 6 count = str.length(); 7 total += count; 8 cout << count << " chars" << endl; 9 cout << total << " chars total" << endl; 10 } 11 12 void ch9_2() { 13 using namespace std; 14 string str; 15 while (true) { 16 cout << "enter a line: " << endl; 17 getline(cin, str); 18 if (str == "") 19 break; 20 ch9_2_strcount(str); 21 } 22 cout << "Bye" << endl; 23 }
1 void ch9_3() { 2 using namespace std; 3 int buffer[1000]; 4 chaff * arr_chaff = new (buffer)chaff[2]; 5 strcpy(arr_chaff[0].dross, "#1"); arr_chaff[0].slag = 123; 6 strcpy(arr_chaff[1].dross, "#2"); arr_chaff[1].slag = 321; 7 for (int i = 0; i < 2; ++ i) 8 cout << "dross: " << arr_chaff[i].dross << " slag: " << arr_chaff[i].slag << endl; 9 }
1 // chapter 09_sales.h 2 3 #ifndef LEARN_CPP_CHAPTER09_SALES_H 4 #define LEARN_CPP_CHAPTER09_SALES_H 5 6 #include <iostream> 7 8 namespace SALES 9 { 10 const int QUARTERS = 4; 11 struct Sales { 12 double sales[QUARTERS]; 13 double average; 14 double max; 15 double min; 16 }; 17 void setSales(Sales &, const double *, int); 18 void setSales(Sales &); 19 void showSales(const Sales &); 20 } 21 22 23 24 #endif //LEARN_CPP_CHAPTER09_SALES_H 25 26 27 28 29 30 31 32 // chapter09_sales.cpp 33 34 #include "chapter09_sales.h" 35 36 void SALES::setSales(SALES::Sales & s, const double * ar, int n) { 37 double sum = 0; 38 int p_max = 0, p_min = 0; 39 for (int i = 0; i < n; ++ i) { 40 s.sales[i] = ar[i]; 41 sum += ar[i]; 42 if (ar[i] > ar[p_max]) 43 p_max = i; 44 if (ar[i] < ar[p_min]) 45 p_min = i; 46 } 47 s.average = sum / n; 48 s.max = ar[p_max]; 49 s.min = ar[p_min]; 50 } 51 52 void SALES::setSales(SALES::Sales & s) { 53 using namespace std; 54 double sum = 0; 55 int p_max = 0, p_min = 0, n = 0; 56 for (int i = 0; i < SALES::QUARTERS; ++ i) { 57 if (!(cin >> s.sales[i])) 58 break; 59 cin.get(); 60 ++ n; 61 sum += s.sales[i]; 62 if (s.sales[i] > s.sales[p_max]) 63 p_max = i; 64 if (s.sales[i] < s.sales[p_min]) 65 p_min = i; 66 } 67 s.average = sum / n; 68 s.max = s.sales[p_max]; 69 s.min = s.sales[p_min]; 70 } 71 72 void SALES::showSales(const SALES::Sales & s) { 73 using namespace std; 74 cout << "sales: "; 75 for (int i = 0; i < SALES::QUARTERS; ++ i) 76 if (s.sales[i]) 77 cout << s.sales[i]; 78 cout << endl; 79 cout << "average: " << s.average << endl; 80 cout << "max: " << s.max << endl; 81 cout << "min: " << s.min << endl; 82 } 83 84 85 86 87 // run 88 89 void ch9_4() { 90 using namespace std; 91 SALES::Sales a; 92 cout << "set a: " << endl; 93 SALES::setSales(a); 94 double s[4]{1.1,2.2,3.3,4.4}; 95 SALES::Sales b; 96 SALES::setSales(b,s,4); 97 cout << "a: " << endl; 98 SALES::showSales(a); 99 cout << "b: " << endl; 100 SALES::showSales(b); 101 }
以上是关于C++ Primer Plus编程练习答案——第14章的主要内容,如果未能解决你的问题,请参考以下文章