C++PrimerPlus中文第六版第10章编程练习答案

1、

//bankaccount.h
#ifndef BANKACCOUNT_H_
#define BANKACCOUNT_H_

#include<string>

class BankAccount
{
private:
    std::string m_account_name;
    const char* m_account_number;
    double m_deposit;
public:
    BankAccount();
    BankAccount(std::string name, const char* number, double deposit);
    void show();
    void save(double fund);
    void draw(double fund);
};


#endif
//bankaccount.cpp
#include<iostream> #include<string> #include "bankaccount.h" BankAccount::BankAccount() { m_account_name = "Default Name"; m_account_number = "********"; m_deposit = 0; } BankAccount::BankAccount(std::string name, const char* number, double deposit) { m_account_name = name; m_account_number = number; if (deposit < 0) { std::cout << "Deposit can't be negative, the deposit set to 0.\n"; m_deposit = 0; } else m_deposit = deposit; } void BankAccount::show() { using std::cout; using std::endl; using std::ios_base; cout << "Account name: " << m_account_name << endl; cout << "Account number: " << m_account_number << endl; ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield); std::streamsize prec = cout.precision(2); cout << "Account deposit: $" << m_deposit << endl; cout.setf(orig, ios_base::floatfield); cout.precision(prec); } void BankAccount::save(double fund) { m_deposit += fund; } void BankAccount::draw(double fund) { if (fund > m_deposit) std::cout << "You can't draw the money more than you have. The drawing is failed.\n"; else m_deposit -= fund; }

2、

//person.h
#ifndef PRESON_H_
#define PERSON_H_ #include<string> class Person { private: static const int LIMIT = 25; std::string m_lname; char m_fname[LIMIT]; public: Person() { m_lname = ""; m_fname[0] = '\0'; }; Person(std::string lname, const char* fname = "NoFirstName"); void Show() const; void FormalShow() const; }; #endif
//person.cpp
#include<iostream> #include<cstdlib> #include<string> #include "person.h" Person::Person(std::string lname, const char* fname) { m_lname = lname; strcpy_s(m_fname, fname); } void Person::Show() const { using namespace std; cout << m_fname << ", " << m_lname << endl; } void Person::FormalShow() const { using namespace std; cout << m_lname << ", " << m_fname << endl; }
//useperson.cpp
#include<iostream> #include "person.h" int main() { Person one; Person two("Smythecraft"); Person three("Dimwiddy", "Sam"); one.Show(); one.FormalShow(); two.Show(); two.FormalShow(); three.Show(); three.FormalShow(); }

3、

// golf.h
#ifndef GOLF_H_
#define GOLF_H_

class Golf
{
private:
    static const int Len = 40;
    char m_fullname[Len];
    int m_handicap;
public:
    Golf();
    Golf(const char* fullname, int handicap);
    void setgolf(const char* fullname, int hadicap);
    void showgolf();
};

#endif
// golf.cpp
#include<iostream>
#include "golf.h"
#include <string.h>

Golf::Golf()
{
    strcpy_s(m_fullname, "No name");
    m_handicap = 0;
}

Golf::Golf(const char* fullname, int handicap)
{
    strcpy_s(m_fullname, fullname);
    m_handicap = handicap;
}

void Golf::setgolf(const char* fullname, int handicap)
{
    *this = Golf(fullname, handicap);
}

void Golf::showgolf()
{
    using namespace std;
    cout << "Name: " << m_fullname << ", handicap: " << m_handicap << endl;
}
// usegolf.cpp
#include<iostream>
#include "golf.h"
using namespace std;

int main()
{
    Golf ann;
    ann.showgolf();
    Golf andy("Andy", 10);
    andy.showgolf();
    Golf anna;
    anna.setgolf("Anna", 5);
    anna.showgolf();
    return 0;
}

4、名称空间使用不熟练,暂时不用名称空间

// sales.h
#ifndef SALES_H_
#define SALES_H_

const int QUATERS = 4;
class Sales
{
private:
    double sales[QUATERS];
    double average;
    double max;
    double min;
public:
    Sales();
    Sales(const double ar[], int n);
    void setSales(Sales& s);
    void showSales();
};

#endif
// sales.cpp
#include<iostream>
#include "sales.h"

Sales::Sales()
{
    for (int i = 0; i < QUATERS; i++)
        sales[i] = 0.0;
    average = max = min = 0.0;
}

Sales::Sales(const double ar[], int n)
{
    double total = 0.0;
    max = min = 0.0;
    for (int i = 0; i < n; i++)
    {
        sales[i] = ar[i];
        total += sales[i];
        if (sales[i] > max)
            max = sales[i];
        if (sales[i] < min)
            min = sales[i];
    }
    average = total / n;
    for (int i = n - 1; i < QUATERS; i++)
        sales[i] = 0.0;
}

void Sales::setSales(Sales & s)
{
    *this = s;
}

void Sales::showSales()
{
    using std::cout;
    using std::endl;
    using std::ios_base;
    cout << "Spring\tSummer\tAutumn\tWinter\n";
    ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
    std::streamsize prec = cout.precision(3);
    for (int i = 0; i < QUATERS; i++)
        cout << sales[i] << "\t";
    cout << endl;
    cout << "average: " << average << endl;
    cout << "max: " << max << ", min: " << min << endl;
    cout.setf(orig, ios_base::floatfield);
    cout.precision(prec);
}
#include<iostream>
#include "sales.h"
using namespace std;

int main()
{
    Sales XD;
    XD.showSales();
    double sales[QUATERS] = { 10.0, 20.0, 25.0, 15.0 };
    Sales HF(sales, QUATERS);
    HF.showSales();
    XD.setSales(HF);
    XD.showSales();
    Sales HZ;
    XD.setSales(HZ);
    XD.showSales();
    HF.showSales();
    return 0;
}

5、

// stack.h

#ifndef STACK_H_
#define STACK_H_

const int MAXstring = 35;

struct customer
{
    char fullname[MAXstring];
    double payment;
};

typedef customer Item;

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

#endif
//stack.cpp

#include<iostream>
#include "stack.h"

Stack::Stack()
{
    top = 0;
}

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

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

bool Stack::push(Item& item)
{
    using std::cout;
    if (this->isFull())
    {
        cout << "Stack is full, can't push any structure.\n";
        return false;
    }
    else
    {
        items[top++] = item;
        cout << "Push successfully.\n";
        return true;
    }
}

bool Stack::pop(Item& item)
{
    using std::cout;
    if (this->isEmpty())
    {
        cout << "Stacck is empty, can't pop any structure.\n";
        return false;
    }
    else
    {
        item = items[--top];
        cout << "Pop successfully.\n";
        return true;
    }
}
#include<iostream>
#include "stack.h"
using namespace std;

int main()
{
    Stack st;
    char ch;
    double totalPatment = 0.0;
    cout << "A(add), P(pop), Q(quit)\n";

    while (cin >> ch and toupper(ch) != 'Q')
    {
        while (cin.get() != '\n')
            continue;
        if (!isalpha(ch))
            cout << '\a';

        char fullname[MAXstring];
        double payment;
        Item temp;
        switch (ch)
        {
        case 'a':
        case 'A':
            cout << "Enter fullname and payment,\nfullname: ";
            cin.getline(fullname, MAXstring);
            cout << "payment: ";
            cin >> payment;
            while (cin.get() != '\n')
                continue;
            strcpy_s(temp.fullname, fullname);
            temp.payment = payment;
            st.push(temp);
            break;
        case 'p':
        case 'P':
            if(st.pop(temp))
                cout << "Fullname: " << temp.fullname << ", payment: " << temp.payment << endl;
            break;
        default:
            cout << "You should enter a or q.\n";
            break;
        }

        cout << "A(add), P(pop), Q(quit)\n";
    }
    return 0;
}

6、

// move.h

#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
// move.cpp


#include<iostream>
#include "move.h"

Move::Move(double a, double b)
{
    x = a;
    y = b;
}

void Move::showMove() const
{
    using std::cout;
    using std::ios_base;
    ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
    std::streamsize prec = cout.precision(3);
    cout << "(" << x << ", " << y << ")\n";
    cout.setf(orig, ios_base::floatfield);
    cout.precision(prec);
}

Move Move::add(const Move& m) const
{
    Move temp(x + m.x, y + m.y); // 类的成员函可以访问同类型对象的引用作为参数的对象的私有变量
    return temp;
}

void Move::reset(double a, double b)
{
    x = a;
    y = b;
}
#include<iostream>
#include "move.h"
using namespace std;

int main()
{
    Move move1(1,2), move2(3,4), move3;
    move1.showMove();
    move2.showMove();
    move3 = move1.add(move2);
    move3.showMove();
    move3.reset(0, 1);
    move3.showMove();
    return 0;
}

7、

// plorg.h

#ifndef PLORG_H_
#define PLORG_H_

const int MAXsize = 20;
class Plorg
{
private:
    char m_name[MAXsize];
    int m_CI;
public:
    Plorg(const char* name = "Plorga", int CI = 50);
    void setCI(int CI);
    void show() const;
};
#endif
// plorg.cpp

#include<iostream>
#include<string>
#include "plorg.h"

Plorg::Plorg(const char* name, int CI)
{
    strcpy_s(m_name, name);
    m_CI = CI;
}

void Plorg::setCI(int CI)
{
    m_CI = CI;
}

void Plorg::show() const
{
    std::cout << "Name: " << m_name << ", CI: " << m_CI << std::endl;
}
#include<iostream>
#include "plorg.h"

int main()
{
    Plorg p;
    p.show();
    p.setCI(100);
    p.show();
    Plorg q("Yangtsi", 100);
    q.show();
    return 0;
}

8、

// list.h

#ifndef LIST_H_
#define LIST_H_

const int UserDefinedSize = 5;
typedef int Item[UserDefinedSize];

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

#endif
// list.cpp

#include<iostream>
#include "list.h"

List::List()
{
    top = 0;
}

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

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

bool List::add(Item& item)
{
    if (this->isfull())
        return false;
    else
    {
        memcpy(items[top++], item, sizeof(item));
        return true;
    }
}

void List::visit(void (*pf)(Item&))
{
    for (int i = 0; i < MAX; i++)
        pf(items[i]);
}
#include<iostream>
#include "list.h"

void showItem(Item& item);

int main()
{
    List l;
    int a[10][UserDefinedSize];
    for (int i = 0; i < 10; i++)
        for (int j = 0; j < UserDefinedSize; j++)
            a[i][j] = UserDefinedSize * i + j;
    for (int i = 0; i < 10; i++)
        l.add(a[i]);
    l.visit(showItem);
    return 0;
}


void showItem(Item& item)
{
    for (int i = 0; i < UserDefinedSize; i++)
    {
        std::cout << item[i] << '\t';
    }
    std::cout << std::endl;
}

 

posted @ 2023-02-11 10:22  Triphan  阅读(79)  评论(0)    收藏  举报