码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-05-25 00:54:21      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

p333.3

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<string>
 5 using namespace std;
 6 
 7 class Golf{
 8 private:
 9     static const int Len = 40;
10     char fullname[Len];
11     int handicap;
12 public:
13     Golf(char name[Len], int bt);
14     void showgolf() const;
15 };
16 
17 void setgolf(char *name, int extent, int &bt){
18     cout << "enter a name\n";
19     cin.get(name, extent);
20     while (cin.get() != \n)
21         continue;
22     cout << "enter a number\n";
23     cin >> bt;
24 }
25 
26 Golf::Golf(char name[Len], int bt){
27     strncpy(fullname, name, Len);
28     handicap = bt;
29 }
30 
31 void Golf::showgolf()const{
32     cout << "fullname is " << fullname << endl
33         << "handicap is " << handicap;
34 }
35 
36 int main(){
37     Golf one = Golf("asxc", 5);
38     int ct;
39     char name[40];
40     setgolf(name, 40, ct);
41     Golf two = Golf(name, ct);
42     one.showgolf();
43     cout << endl;
44     two.showgolf();
45 
46     system("pause");
47     return 0;
48 }

p333.5

  1 头文件
  2 #ifndef _STACK_H_
  3 #define _STACK_H_ 
  4 
  5 struct customer{
  6     char fullname[35];
  7     double payment;
  8 };
  9 
 10 typedef customer Item;
 11 
 12 class Stack{
 13 private:
 14     enum{max=10};
 15     Item items[max];
 16     int top;
 17 public:
 18     Stack();
 19     bool isfull()const;
 20     bool isempty()const;
 21     void push(const Item &);
 22     bool pop(const Item &);
 23 };
 24 
 25 #endif
 26 
 27 方法
 28 #include<iostream>
 29 #include"stack.h"
 30 using namespace std;
 31 
 32 Stack::Stack(){
 33     top = 0;
 34 }
 35 
 36 bool Stack::isfull()const{
 37     if (top == max)
 38         return true;
 39     else return false;
 40 }
 41 
 42 bool Stack::isempty()const{
 43     if (top == 0)
 44         return true;
 45     else return false;
 46 }
 47 
 48 void Stack::push(const Item &temp){
 49     if (isfull())
 50         cout << "the stack has already full\n";
 51     else items[top++] = temp;
 52 }
 53 
 54 bool Stack::pop(const Item &temp){
 55     if (isempty()){
 56         cout << "the stack has already empty\n";
 57         return false;
 58     }
 59     else {
 60         items[--top] = temp;
 61         return true;
 62     }
 63 }
 64 
 65 驱动
 66 #include<iostream>
 67 #include"stack.h"
 68 using namespace std;
 69 void get_customer(Item &);
 70 
 71 int main(){
 72     Item temp;
 73     Stack test;
 74     double payments=0;
 75     cout << "enter A to push, enter P to pop or"
 76         <<"enter Q to quit\n";
 77     char ch;
 78     while (cin >> ch && (ch != Q && ch != q)){
 79         while (cin.get() != \n)
 80             continue;
 81         switch (ch){
 82         case A:
 83         case a:
 84             get_customer(temp);
 85             test.push(temp);
 86             break;
 87         case P:
 88         case p:
 89             if (test.pop(temp))
 90                 payments += temp.payment;
 91             cout << "now total payments is " <<
 92                 payments << endl;
 93         default:
 94             cout << "please enter A/P or Q\n";
 95         }
 96         cout << "enter A to push, enter P to pop or"
 97             << "enter Q to quit\n";
 98     }
 99     system("pause");
100     return 0;    
101 }
102 
103 void get_customer(Item &temp){
104     cout << "enter a line\n";
105     cin.getline(temp.fullname, 35);
106     cout << "enter a number\n";
107     cin >> temp.payment;
108     cin.get();
109 }

p333.7

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 using namespace std;
 5 
 6 class Plorg{
 7 private:
 8     char fullname[19];
 9     int CI;
10 public:
11     Plorg(char *ar = "Plorga", int ct = 50);
12     void index();
13     void show()const;
14 };
15 
16 Plorg::Plorg(char *ar, int ct){
17     strcpy(fullname, ar);
18     CI = ct;
19 }
20 
21 void Plorg::index(){
22     cout << "enter a number of CI\n";
23     cin >> CI;
24 }
25 
26 void Plorg::show()const{
27     cout << "fullname is " << this->fullname <<
28         " CI is " << (*this).CI << endl;
29 }
30 
31 int main(){
32     Plorg test1;
33     cout << "enter a line\n";
34     char ar[19];
35     int ct;
36     cin.getline(ar, 19);
37     cout << "enter a number\n";
38     cin >> ct;
39     cin.get();
40     Plorg test2(ar, ct);
41     test1.show();
42     cout << endl;
43     test2.show();
44     test1.index();
45     cout << endl;
46     test1.show();
47 
48     system("pause");
49     return 0;
50 }

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

标签:

原文地址:http://www.cnblogs.com/coding-time/p/4526782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!