C++ Primer Plus 第六版 第十章課後編程練習答案

1.

bank.h

#include <string>
using namespace std;

class BankAccount
{
    string name;
    string acctnum;
    double balance;
public:
    BankAccount(string & client, string & num, double bal=0.0);//初始化
    void show();
    void deposit(double bal);//存款
    void withdraw(double bal);//取款
};

bank.cpp

#include <iostream>
#include "bank.h"
using namespace std;

BankAccount::BankAccount(string &client, string &num, double bal)
{
    //左實參,右形參
    name = client;
    acctnum = num;
    balance = bal;
}

void BankAccount::show()
{
    cout << "Bank Name: " << name << ", Account Num: " << acctnum << ", Balance: " << balance << endl;
}

void BankAccount::deposit(double bal)
{
    if(bal > 0)
    {
        balance += bal;
    }
    else
    {
        cout << "Deposit fail./n";
    }
}

void BankAccount::withdraw(double bal)
{
    if(bal > 0)
    {
        balance -= bal;
    }
    else
    {
        cout << "Withdraw fail./n";
    }
}

play.cpp

#include "bank.h"
#include <iostream>
using namespace std;

int main()
{
    cout << "Enter your information: \n";
    string client;
    string num;
    double bal;
    cout << "Name: ";
    getline(cin, client);
    cout << "AccountNum: ";
    getline(cin, num);
    cout << "Balance: ";
    cin >> bal;

    BankAccount myaccount(client, num, bal);//實例化
    myaccount.show();
    char flag;
    cout << "Choose,'d' for deposit, 'w' for withdraw, 'q' for quit: ";
    cin >> flag;
    while (flag != 'q')
    {
        if(flag == 'd')
        {
            cout << "Enter account of deposit: ";
            cin >> bal;
            myaccount.deposit(bal);
            myaccount.show();
        }
        else if (flag == 'w')
        {
            cout << "Enter account of withdraw: ";
            cin >> bal;
            myaccount.withdraw(bal);
            myaccount.show();
        }
        else
            break;
        cout << "Choose,'d' for deposit, 'w' for withdraw, 'q' for quit: ";
        cin >> flag;
    }
    cout << "Over!";
    return 0;
}

2.

person.h

#ifndef PERSON_H
#define PERSON_H
using namespace std;
#include <string>

class Person
{
    static const int LIMIT = 25;
    string lname;
    char fname[LIMIT];
public:
    Person() {lname = ""; fname[0] = '\0'; }
    Person(const string & ln, const char * fn = "Heyyou");//初始化
    void Show();
    void FormalShow();
};

#endif

person.cpp

#include "person.h"
#include <cstring>
#include <iostream>
using namespace std;

Person::Person(const string & ln, const char * fn)
{
    lname = ln;
    strcpy(fname, fn);
}

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

void Person::FormalShow()
{
    cout << "Name: " << lname << " " << fname << endl;
}

play.cpp

#include <iostream>
#include "person.h"
using namespace std;

int main()
{
    Person one;
    Person two("Smythecraft");
    Person three("Dimwiddy", "Sam");

    cout << "The 1st person's name: \n";
    cout << "Format 1: ";
    one.Show();
    cout << "Format 2: ";
    one.FormalShow();

    cout << "The 2nd person's name: \n";
    cout << "Format 1: ";
    two.Show();
    cout << "Format 2: ";
    two.FormalShow();

    cout << "The 3rd person's name: \n";
    cout << "Format 1: ";
    three.Show();
    cout << "Format 2: ";
    three.FormalShow();

    cout << "Finished.";

    return 0;
}

3.   this指針沒明白

golf.h

//改爲類
#ifndef GOLF_H
#define GOLF_H

const int Len = 40;
class Golf
{
    char fullname[Len];
    int handicap;
public:
    Golf(const char * name, int hc);
    Golf();
    void sethandicap(int hc);
    void showgolf();
};

#endif

golf.cpp

#include "golf.h"
#include <iostream>
using namespace std;

Golf::Golf(const char *name, int hc)
{
    strcpy(fullname, name);
    handicap = hc;
}

Golf::Golf()
{
    cout << "Enter fullname of golf player: ";
    cin.getline(fullname, Len);
    cout << "Enter handicap of golf player: ";
    cin >> handicap;
    cin.get();
    *this = Golf(fullname, handicap);//??????????
}

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

void Golf::showgolf()
{
    cout << "Name: " << fullname << endl;
    cout << "Handicap: " << handicap << endl;
}

play.cpp

#include "golf.h"
using namespace std;

int main()
{
    Golf g;//初始
    g.showgolf();
    g.sethandicap(100);
    g.showgolf();

    Golf g1("Zhang San", 200);
    g1.showgolf();
    g1.sethandicap(300);
    g1.showgolf();

    return 0;
}

4.

namesp.h

//struct改爲類
#ifndef NAME_H
#define NAME_H

namespace SALES
{
    const int QUARTERS = 4;
    class Sales
    {
        double sales[QUARTERS];
        double average;
        double max;
        double min;
    public:
        void setSales(const double ar[], int n);
        void setSales();
        void showSales();
    };
}

#endif

namesp.cpp

#include "namesp.h"
#include <iostream>
using namespace std;

namespace SALES
{
    void Sales::setSales(const double ar[], int n)
    {
        if(n < 4)
        {
            for (int i=0; i<n; i++)
            {
                sales[i] = ar[i];
            }
            for (int j=n; j<4; j++)
            {
                sales[j] = 0;
            }
        }
        else
        {
            for (int i=0; i<4; i++)
            {
                sales[i] = ar[i];
            }
        }
        average = (sales[0] + sales[1] + sales[2] + sales[3]) / 4;
        double max0 = 0.0;
        double min0 = 1000000.0;
        for(int i=0; i<4; i++)
        {
            if(sales[i] > max0)
                max0 = sales[i];
            if(sales[i] < min0)
                min0 = sales[i];
        }
        max = max0;
        min = min0;
    }

    void Sales::setSales()
    {
        cout << "Enter 4 quarters: \n";
        for (int i=0; i<4; i++)
        {
            cout << "The #" << i+1 << "quarter is: ";
            cin >> sales[i];
        }
        average = (sales[0] + sales[1] + sales[2] + sales[3]) / 4;
        double max0 = 0.0;
        double min0 = 1000000.0;
        for(int i=0; i<4; i++)
        {
            if(sales[i] > max0)
                max0 = sales[i];
            if(sales[i] < min0)
                min0 = sales[i];
        }
        max = max0;
        min = min0;
    }

    void Sales::showSales()
    {
        cout << "The 4 quarters are $" << sales[0] << ", $" << sales[1] << ", $" << sales[2] << ", $" << sales[3] << endl;
        cout << "The average income is: $" << average << endl;
        cout << "The maximum income is: $" << max << endl;
        cout << "The minimum income is: $" << min << endl;
    }
}

 play.cpp

#include "namesp.h"
#include <iostream>
using namespace std;
using namespace SALES;

int main()
{
    Sales s1;
    cout << "The 1st sale's information: \n";
    s1.setSales();
    s1.showSales();
    cout << endl << endl;

    Sales s2;
    cout << "The 2nd sale's information: \n";
    double ar[2] = {1.1, 2.2};
    s2.setSales(ar, 2);
    s2.showSales();
    cout << endl << endl;

    return 0;
}

5. 結果有些問題

stack.h

#ifndef STACK_H
#define STACK_H

struct customer
{
    char fullname[35];
    double payment;
};
typedef customer Item;

class Stack
{
    Item items[3];
    int top;
public:
    Stack();
    bool isempty();
    bool isfull();
    bool push(Item & item);
    bool pop(Item & item);
};

#endif

stack.cpp

#include "stack.h"

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

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

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

bool Stack::push(Item & item)
{
    if(top < 3)
    {
        items[top++] = item; //只能是top++,不能是++top
        return true;
    }
    else
        return false;
}

bool Stack::pop(Item & item)
{
    if(top > 0)
    {
        item = items[--top]; //??????
        return true;
    }
    else
        return false;
}

play.cpp

#include "stack.h"
#include <iostream>
using namespace std;

int main()
{
    Stack s;
    customer tmp;
    char ch;
    double totalpayment = 0.0;//總支出
    cout << "Enter 'A' to add, 'D' to delete, 'Q' to quit:\n";
    while (cin>>ch && toupper(ch)!='Q')
    {
        while (cin.get() != '\n') //過濾掉多餘的輸入,只取輸入的第一個字符
            continue;

        switch (toupper(ch))
        {
            case 'A':
                cout << "Enter name: ";
                cin.getline(tmp.fullname, 35);
                cout << "Enter payment: ";
                cin >> tmp.payment;
                cin.get();
                if(s.isfull())
                    cout << "Full!\n";
                else
                    s.push(tmp);
                break;
            case 'D':
                if(s.isempty())
                    cout << "Empty!\n";
                else
                {
                    totalpayment += tmp.payment;
                    s.pop(tmp);
                    cout << "Information of " << tmp.fullname << " has been deleted!\n";
                }
                break;
        }
        cout << "Enter 'A' to add, 'D' to delete, 'Q' to quit:\n";
    }
    cout << "Total payment: " << totalpayment << endl;
    cout << "Over!\n";

    return 0;
}

6. add功能沒明白

move.h

#ifndef MOVE_H
#define MOVE_H

class Move
{
    double x;
    double y;
public:
    Move(double a = 0, double b = 0);
    void showmove();
    //add沒明白
    void reset(double a=0, double b=0);
};

#endif

move.cpp

#include "move.h"
#include <iostream>
using namespace std;

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

void Move::showmove()
{
    cout << "x= " << x << ", y= " << y << endl;
}

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

play.cpp

#include <iostream>
#include "move.h"
using namespace std;

int main()
{
    Move m1(1.1, 2.2);
    Move m2(3.3, 4.4);
    cout << "1st move:\n";
    m1.showmove();
    cout << "2nd move:\n";
    m2.showmove();
    cout << "Reset move:\n";
    double a,b;
    cout << "x= ";
    cin >> a;
    cout << "y= ";
    cin >> b;
    m2.reset(a, b);
    cout << "3rd move:\n";
    m2.showmove();

    return 0;
}

7.

plorg.h

#ifndef PLORG_H
#define PLORG_H

class Plorg
{
    char name[19];
    int CI;
public:
    Plorg();//初始
    void SetCI(int a);
    void showplorg();
    void newplorg(char * name0);
};
#endif

plorg.cpp

#include "plorg.h"
#include <iostream>
#include <cstring>
using namespace std;

Plorg::Plorg()
{
    strcpy(name, "Plorga");
    CI = 0;
}

void Plorg::showplorg()
{
    cout << "Name: " << name << ", CI: " << CI << endl;
}

void Plorg::SetCI(int a)
{
    CI = a;
}

void Plorg::newplorg(char * name0)
{
    strcpy(name, name0);
    CI = 50;
}

play.cpp

#include <iostream>
#include "plorg.h"
using namespace std;

int main()
{
    Plorg p;
    cout << "Default plorg:\n";
    p.showplorg();

    Plorg p1;
    cout << "Enter new plorg:\n";
    char name1[19];
    cin.getline(name1, 19);
    p1.newplorg(name1);
    p1.showplorg();

    cout << "Change CI:\n";
    int a;
    cin >> a;
    p1.SetCI(a);
    p1.showplorg();

    return 0;
}

8.暫

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章