C++ primer plus 第六版 第十章 编程练习答案

第十章 编程练习答案

1.

#ifndef ACCOUNT_H_
#define ACCOUNT_H_
//头文件
class BankAccount
{
private:
    std::string fullname;
    std::string account;
    double money;
public:
    BankAccount(const std::string & client, const std::string & acc, double cash = 0.0);
    void save(double cash);
    void withdraw(double cash);
    void show() const;  
};

#endif
#include 
#include //streng类 
#include "account.h"
//类的具体实现
BankAccount::BankAccount(const std::string & client, const std::string & ps, double cash)
{
    fullname = client;//string直接赋值 
    account = ps;
    money = cash;
}

void BankAccount::save(double cash)
{
    if(cash < 0)
        std::cout << "Number of cash can't be negative.\n";
    else
        money += cash;  
}

void BankAccount::withdraw(double cash)
{
    if(cash < 0)
        std::cout << "Number of cash can't be negative.\n";
    else if (cash > money)
        std::cout << "You can't get more than you have!\n";
    else
        money -= cash;  
}

void BankAccount::show() const
{
    using std::cout;

    cout << "Fullname: " << fullname << "  Account: " << account << '\n';
    cout << "  money: $" << money << '\n'; 
}
#include 
#include 
#include "account.h"
//使用类,主程序变得简单
int main()
{
    {
        using std::cout;
        cout << "Bank Account information:\n";
        BankAccount b1("Nano Smart", "ASD", 12334.34);
        b1.show();
        BankAccount b2("Boffo Objects", "ZXC", 5834.983);
        b2.show();      

        cout << "\nAfter a month:\n";
        b1.save(1231.23);
        b1.show();      
        b1.withdraw(5353.34);
        b1.show();
        b2.withdraw(555.2322);
        b2.show();
        b2.save(2354.4);
        b2.show();
    }
    return 0;
}

2.

#ifndef PERSON_H_
#define PERSON_H_
//头文件
class Person{
private:
    static const int LIMIT = 25;
    std::string lname;
    char fname[LIMIT];
public:
    Person() {lname = " "; fname[0] = '\0'; };
    Person(const std::string & ln, const char * fn = "Heyyou");
    void Show() const;
    void FormalShow() const;
};

#endif
#include 
#include //string类 
#include //字符串使用strcpy 
#include "person.h"
//类的实现
Person::Person(const std::string & ln, const char * fn)
{
    lname = ln;
    strcpy(fname, fn);
}

void Person::Show() const
{
    std::cout << "Full Name:" << std::endl;
    std::cout << fname << " " << lname << std::endl;
}

void Person::FormalShow() const
{
    std::cout << "Formal Name:" << std::endl;
    std::cout << lname << " " << fname << std::endl;
}
#include 
#include //streng类 
#include //字符串使用strcpy 
#include "person.h" 
//主程序
int main()
{
    {
        using std::cout;
        using std::endl;
        Person one;
        one.Show();
        cout << endl;
        one.FormalShow();
        cout << endl;

        Person two("Smythecraft");
        two.Show();
        cout << endl;
        two.FormalShow();
        cout << endl;

        Person three("Dimwdiddy", "Sam");
        three.Show();
        cout << endl;
        three.FormalShow();
        cout << endl;       
    }
    return 0;
}

3.

#ifndef GOLF10_H_
#define GOLF10_H_
#include 
//头文件
class Golf
{
private:
    std::string fullname;   
    int handicap;
public:
    Golf (const std::string name, int hc = 0);  
    int setgolf();
    void sethandicap(const int hc);
    void showgolf()const;   
};

#endif
#include 
#include 
#include "U10p3golf.h"
//类的实现
Golf::Golf(const std::string name, int hc)
{
    fullname = name;
    handicap = hc;  
}

int Golf::setgolf()
{
    using namespace std;
    int handicap = 0;   
    int temp = 1;

    cout << "The fullname is: ";    
    getline(cin, fullname);//获取字符串 
    cout << "The handicap is: ";
    cin >> handicap;

    Golf g(fullname, handicap);
    *this = g;
    return fullname == "" ? 0 : 1;      
    //判断输入行是否为空 
}

void Golf::sethandicap(const int hc)
{
    handicap = hc;
}

void Golf::showgolf() const
{
    using std::cout;
    using std::endl;
    cout << "The fullname is: " << fullname << endl;
    cout << "The handicap is: " << handicap << endl;        
}
#include 
#include "U10p3golf.h"
//主程序
int main()
{
    Golf g("Alex Tony", 10);
    g.showgolf();
    std::cout << std::endl;
    g.setgolf();
    std::cout << std::endl;
    g.showgolf();
    std::cout << std::endl;
    g.sethandicap(99);
    g.showgolf();
    return 0;
}

4.

#ifndef SALES_H_
#define SALES_H_
//头文件
namespace SALES//名称空间
{   
    class Sales
    {
    private:
        static const int QUARTERS = 4;
        double sales[QUARTERS];
        double average;
        double max;
        double min;
    public:
        Sales();
        Sales(const double *ar, const int n);   
        void setsales();    
        void showSales() const;
    };
}

#endif
#include 
#include "U10p4sales.h"
//类的实现
SALES::Sales::Sales()//注意这是在名称空间里的类的函数,后面也是
{
    for(int i = 0; i < QUARTERS; i++)
        sales[i] = 0;
    average = 0;
    max = 0;
    min = 0;
}

SALES::Sales::Sales(const double * ar,const int n)
{
    //计算和存储数组
    double a, b, c;
    a = b = ar[0];
    c = 0.0;
    for(int i = 0; i < n; i++)
    {
        sales[i] = ar[i];
        c += ar[i];
        if(ar[i] < a)
            a = ar[i];
        if(ar[i] > b)
            b = ar[i];              
    }
    average = c / n;
    max = b;
    min = a;            
}

void SALES::Sales::setsales()
{

    double ar[QUARTERS]={0,0,0,0};
    std::cout << "Please enter 4 number:" << std::endl;
    int n = 0;
    do{
        std::cin >> ar[n];
        n++;
    }while((n < QUARTERS) );


    double a, b, c;
    a = b = ar[0];
    c = 0.0;
    for(int i = 0; i < n; i++)
    {
        sales[i] = ar[i];
        c += ar[i];
        if(ar[i] < a)
            a = ar[i];
        if(ar[i] > b)
            b = ar[i];              
    }
    average = c / n;
    max = b;
    min = a;            
}

void SALES::Sales::showSales() const
{
    //显示结构数据
    for(int i = 0; i < QUARTERS; i++)
        std::cout << sales[i] << " ";           
    std::cout << "\nThe average is: "<< average << "  The max is: "
        << max << "  The min is: " << min << std::endl;
}   
#include 
#include "U10p4sales.h"
//主程序
int main()
{   
    int n;      
    std::cout << "Please enter n items:";
    std::cin >> n;//输入数据
    double a[n];
    std::cout << "Enter numbers: " << std::endl;
    for(int i = 0; i < n; i++)
        std::cin >> a[i];
    SALES::Sales s(a, n);
    s.showSales();
    s.setsales();
    s.showSales();  

    return 0;   
}

5.

#ifndef STACK_H_
#define STACK_H_
//头文件
struct customer {//声明结构
    char fullname[35];
    double payment;
};

typedef customer Item;//声明类型别名

class Stack
{
private:
    enum {MAX = 10};
    Item items[MAX];
    int top;
public:
    Stack();
    bool isempty() const;
    bool isfull() const;
    bool push(const Item & item);
    bool pop(Item & item);  
};

#endif
#include "U10p5stack.h"
//类的实现
Stack::Stack()
{
    top = 0;
}

bool Stack::isempty() const
{
    return top == 0;
}

bool Stack::isfull() const
{
    return top == MAX;
}

bool Stack::push(const Item & item)
{
    if (top < MAX)
    {
        items[top++] = item;
        return true;
    }
    else
        return false;
}

bool Stack::pop(Item & item)
{
    if (top > 0)
    {
        item = items[--top];
        return true;
    }
    else
        return false;   
}
#include 
#include 
#include "U10p5stack.h"
//主程序
int main()
{
    using namespace std;
    double payment;
    Stack st;
    char ch;
    Item po;
    cout << "Please enter A to add a purchase order,\n"
        << "P to process a po, or Q to quit.\n";
    while(cin >> ch && toupper(ch) != 'Q')//输入不为Q(q)时
    {
        while(cin.get() != '\n')
            continue;
        if (!isalpha(ch))
        {
            cout << '\a';
            continue;
        }
        switch(ch)
        {
            case'A':
            case'a':cout << "Enter a PO fullname: ";
                    cin.get(po.fullname, 35);
                    cout << "Enter a PO number to add: ";
                    cin >> po.payment;
                    cin.get();
                    if(st.isfull())
                        cout << "stack already full\n";
                    else
                        st.push(po);
                    break;      
            case'P':
            case'p':if (st.isempty())
                        cout << "stack already empty\n";
                    else {
                        st.pop(po);
                        cout << "PO: " << po.fullname << "  ayment: " << po.payment << " popped\n";
                        payment += po.payment;
                        cout << "The total of payment: " << payment << endl;
                    }
                    break;
        }           
        cout << "Please enter A to add a purchase order,\n"
             << "P to process a PO, or Q to quit.\n";   
    }
    cout << "Bye\n";
    return 0;
}

6.

#ifndef MOVE_H_
#define MOVE_H_
//头文件
class Move
{
private:
    double x;   
    double y;
public:
    Move (double a = 0, double b = 0);
    void showmove() const;
    Move add(const Move & m) const;
    void reset(double a = 0, double b = 0); 
};

#endif
#include 
#include "U10p6move.h"
//类的实现
Move::Move (double a, double b)
{
    x = a;
    y = b;
}

void Move::showmove() const
{
    std::cout << "X: "<< x << " Y: " << y << std::endl;
    std::cout << std::endl;     
}

    Move Move::add(const Move & m) const
{
    return Move(x + m.x, y + m.y);  
}

void Move::reset(double a, double b)
{
    x = a;
    y = b;
}
#include 
#include "U10p6move.h"
//主程序
int main()
{
    std::cout << "The first place:\n";
    Move m1(1.1, 3.3);
    Move m2(4.2, 2.0);
    Move m3(-2.2, -4.0);    
    m1.showmove();

    std::cout << "Move m2:\n";
    m1.add(m2).showmove();
    std::cout << "Then move m3:\n";
    m1.add(m2).add(m3).showmove();

    std::cout << "Place reset:\n";  
    m1.reset();
    m1.showmove();
    return 0;
}

7.

#ifndef PLORG_H
#define PLORG_H
//头文件
class Plorg
{
    private:
        char name[20];
        int CI;
    public:
        Plorg (char name[20] = "Plorga", int n = 50);//默认参数
        void setPlorg(int n);
        void showPlorg();
};

#endif
#include 
#include 
#include "Plorg.h"
//类的实现
Plorg::Plorg (char * str, int n)
{
    strcpy(name, str);//字符串的赋值
    CI = n;
}

void Plorg::setPlorg(int n)
{   
    CI = n; 
}

void Plorg::showPlorg()
{
    std::cout << "The plorg name: " << name << std::endl;
    std::cout << "The plorg CI: " << CI << std::endl;
    std::cout << std::endl;
}
#include 
#include 
#include "Plorg.h"
//主程序
int main()
{
    using namespace std;
    Plorg p1;
    p1.showPlorg();

    char s[20];
    int n;
    cout << "Please enter the plorg name: ";
    cin.getline(s, 20);
    cout << "Please enter the CI: ";
    cin >> n;
    Plorg p2(s, n);
    p2.showPlorg();

    int count;
    cout << "Please enter the CI: ";
    cin >> count;
    p1.setPlorg(count);
    p1.showPlorg();

    return 0;
}

8.

#ifndef LIST_H
#define LIST_H
//头文件,注意格式
typedef int Item;

class List
{
private:
    static const int MAX = 100;
    Item items[MAX];
    int top;
public:
    List(){ top = 0; }  
    bool add(const Item item);  
    bool isempty() const;
    bool isfull() const;
    void visit(void(*pf)(Item & item));
};

#endif
#include 
#include "List.h"//实现文件
//前三个函数可参考书中例子10-11
bool List::isempty() const
{
    return top == 0;
}

bool List::isfull() const
{
    return top == MAX;
}

bool List::add(const Item item)
{
    if (top < MAX)
    {
        items[top++] = item;
        return true;
    }
    else
        return false;
}
//比较难理解的函数,我的理解是它把函数指针作为一个参数
//而指针指向的具体函数是可以改变的,这里就是简单的显示每个item
void List::visit(void(*pf)(Item & item))
{
    std::cout << "\nDisplay the item: \n";  
    for (int i = 0; i < top; ++i)
        (*pf)(items[i]);
}
#include 
#include 
#include 
#include "List.h"
//主程序
void func(Item & item);//函数声明

int main()
{
    using namespace std;
    List st;
    string str;
    cout << "At first: " << endl;
    if(st.isempty() == 1)
        str = "Yes!";
    if(st.isempty() == 0)
        str = "No!";
    cout << "isEmpty? " << str << endl;
    if(st.isfull() == 1)
        str = "Yes!";
    if(st.isfull() == 0)
        str = "No!";    
    cout << "isFull? " << str << endl;  
    st.add(1);
    st.add(2);
    st.add(3);
    st.add(4);
    cout << "\nNow: " << endl;
    if(st.isempty() == 1)
        str = "Yes!";
    if(st.isempty() == 0)
        str = "No!";        
    cout << "isEmpty? " << str << endl;
    if(st.isfull() == 1)
        str = "Yes!";
    if(st.isfull() == 0)
        str = "No!";
    cout << "isFull? " << str << endl;
    //理解了visit函数,整个程序就很简单了
    void(*pf)(Item & item);//这是个指针函数(姑且这么称呼),并没有确定的函数名字,即可以指向同类型的所有函数
    pf = func;//指向func函数
    st.visit(pf);
    system("PAUSE");//显示暂停
    return 0;
}   

void func(Item & item)
{
    std::cout << item << std::endl;
}

你可能感兴趣的:(C++,课后练习)