C++ Primer Plus 第六版 第十章 练习题参考答案

//1
//bank1.h
#pragma once
#include <string>

class bank
{
private :
    std::string name;
    std::string account;
    double asset;
public:
    bank(); //default constructor
    bank(const std::string & na, const std::string & acc, double a);
    ~bank(); //destructor
    void push(double n);
    void pop(double n);
    void show();
};
//ban1.cpp
#include<iostream>
#include"bank1.h"
bank::bank()
{
    name = "no name";
    account = "no account";
    asset = 0.0;
}
bank::bank(const std::string & na, const std::string &acc, double n)
{
    name = na;
    account = acc;
    asset = n;
}
void bank::push(double n)
{
    asset += n;
    std::cout << "Pushing money...Done!\n";
}
void bank::pop(double n)
{
    int temp = asset-n;
    if (temp>0)
        asset = temp;
    else
        asset = asset;
    std::cout << "there is no enough money to pop!Failed!\n";
}
void bank::show()
{
    std::cout << "the name in this bank: " << name<<"\n";
    std::cout << "the account in the bank: " << account << "\n";
    std::cout << "the asset in this bank now is: " << asset << "\n";
}
bank::~bank()
{

}
//main.cpp
#include<iostream>
#include<string>
#include"bank1.h"

int main()
{   
    std::cout << "please input the info of the bank account.\n";
    std::cout << "the name in this bank is: ";
    std::string na;
    getline(std::cin, na);
    std::cout << "the account registrated in the bank is:\n ";
    std::string account;
    getline(std::cin, account);
    std::cout << "the asset in this bank is: ";
    double n;
    std::cin >> n;
    std::cout << "the default constructor make:\n ";
    bank b1;
    b1.show();
    bank b2 = bank(na, account, n);
    b2.show();
    int pus;
    std::cout << "how much do you want to push: ";
    std::cin >> pus;
    b2.push(pus);
    std::cout << "after push: ";
    b2.show();
    std::cout << "how much do you want to pop: ";
    int pop;
    std::cin >> pop;
    b2.pop(pop);
    std::cout << "after pop: ";
    b2.show();
    system("pause");
    return 0;
}
//2**************************************************
//person.h
#include <string>

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");
    ~person();
    void show() const;
    void formalshow() const;
};
//person.cpp
#include<iostream>
#include"Person.h"

person::person(const std::string & ln, const char * fn)
{
    lname = ln;
    strcpy_s(fname, strlen(fn) + 1, fn);
}

person::~person()
{
}

void person::show() const
{
    std::cout << "the fname is: " << fname<< " \n " ;
    std::cout << "the lname is: " << lname<< " \n " ;
}

void person::formalshow() const
{
    std::cout << "the lname is: " << lname << " \n ";
    std::cout << "the fname is: " << fname << " \n ";
}
//main.cpp
#include<iostream>
#include<string>
#include"Person.h"
const int limits = 25;
int main()
{   
    std::string lname;
    std::cout << "please input the lname: ";
    std::cin >> lname;
    std::cin.get();
    char fname[limits];
    std::cout << "please input the fname: ";
    std::cin.get(fname, limits);
    person p1;
    p1.show();
    p1 = person(lname, fname);
    p1.show();
    p1.formalshow();
    person p2("Smythecraft");
    p2.show();
    person p3("Dimwiddy", "Sam");
    p3.show();
    system("pause");
    return 0;
}
//3**************************************************
//golf.h
#include <string>

class golf
{
private :
    static const int Len = 40;
    char fullname[Len];
    int handicap;
public:
    golf() { fullname[0] = '\0'; handicap = 0; };
    ~golf();
    golf & setgo(const char *name, int hc);
    void handi(int hc);
    void show();
};
//golf.cpp
#include<iostream>
#include"golf.h"

golf & golf::setgo(const char * name, int hc) 
{
    strcpy_s(fullname, strlen(name) + 1, name);
    handicap = hc;
    return *this;
}

void golf::handi(int hc)
{
    handicap = hc;
    std::cout << "the handicap is updated.\n";
}

void golf::show()
{
    std::cout << "the fullname is: " << fullname<<"\n";
    std::cout << "the handicap now is: " << handicap<<"\n";
}

golf::~golf()
{
}
//main.cpp
#include"golf.h"
const int limits = 40;
int main()
{   
    char ch[40];
    int hc;
    std::cout << "please input the fullname: ";
    std::cin.get(ch, 40).get();
    std::cout << "please input the handicap: ";
    std::cin >> hc;
    golf g;
    golf *p = &g;
    p=&p->setgo(ch, hc);
    p->show();
    system("pause");
    return 0;
}
//4**************************************************
//sales.h
namespace SALES
{
    class sale {
    private:
        static const int QUAETERS = 4;
        double sales[QUAETERS];
        double average;
        double max;
        double min;
    public:
        sale() { {0; }; 0; 0; 0; };
        ~sale();
        void setsales(const double ar[], int n=4);
        void showsales();
        template <class T>
        int arrlen(T& array){return (sizeof(array) / sizeof(array[0]));}
    };
}
//sales.cpp
#include<iostream>
#include"sales.h"


SALES::sale::~sale()
{
}

void SALES::sale::setsales(const double ar[], int n)
{
    double sum = 0;
    max = -99999999;
    min = 99999999;
    for (int i = 0; i < n; ++i)
    {
        sales[i] = ar[i];
        sum += sales[i];
        max = ar[i] > max ? ar[i] : max;
        min = ar[i] < min ? ar[i] : min;
    }
    average = sum / n;
}

void SALES::sale::showsales()
{
    std::cout << "*****the info*****\n";
    std::cout << "the sales:" << std::endl;
    for (int i = 0; i < arrlen(sales); ++i)
        std::cout << sales[i] << "\t\n";
    std::cout << "the average: " << average << "\n";
    std::cout << "the min: " << min << "\n";
    std::cout << "the max: " << max << "\n";
}
//main.cpp
#include<string>
#include"sales.h"
int main()
{   
    double a[4] = { 1,3,2,4 };
    SALES::sale s;
    s.setsales(a);
    s.showsales();
    system("pause");
    return 0;
}
//5*******************************************
//stack.h
#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 // !STACK_H_
//sales.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(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;
}
//main.cpp
#include<iostream>
#include<string>
#include"stack.h"
void get_customer(customer & cu);
int main()
{   
    using namespace std;
    Stack st;
    customer temp;
    double payments = 0;
    char ch;

    cout << "Please enter A to add a customer,\n"
        << "P to process a customer, and Q to quit.\n";
    while (cin >> ch && (ch = toupper(ch)) != 'Q')
    {
        while (cin.get() != '\n')
            continue;
        if (!isalpha(ch))
        {
            cout << '\a';
            continue;
        }
        switch (ch)
        {
        case 'a':
        case 'A':    if (st.isfull())
                        cout << "stack already full\n";
                     else
                     {
                        get_customer(temp);
                        st.push(temp);
                     }
                     break;
        case 'p':
        case 'P':    if (st.isempty())
                        cout << "stack already empty\n";
                     else {
                        st.pop(temp);
                        payments += temp.payment;
                        cout << temp.fullname << " processed. ";
                        cout << "Payments now total $"
                             << payments << "\n";
                     }
                     break;
        }
        cout << "Please enter A to add a customer,\n"
            << "P to process a customer, and Q to quit.\n";
    }
    cout << "Done!\n";

    system("pause");
    return 0;
}
void get_customer(customer & cu)
{
    using namespace std;
    cout << "please input customer name: ";
    cin.getline(cu.fullname, 35);
    cout << "please input customer payment: ";
    cin >> cu.payment;
    while (cin.get() != '\n')
        continue;
}
//6****************************************
//move.h
#ifndef MOVE_H_
#define MOVE_H_
class Move
{
private:
    double x;
    double y;
public:
    Move(double a = 0, double b = 0);
    ~Move();
    void showmove() const;
    Move add(const Move &m)const;
    void reset(double a = 0, double b = 0);
};
#endif // !STACK_H_
//move.cpp
#include<iostream>
#include"move.h"

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

Move::~Move()
{
}

void Move::showmove() const
{
    std::cout << "Now x is: " << x << '\n';
    std::cout << "Now y is: " << y << '\n';
}

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

void Move::reset(double a, double b)
{
    x = 0;
    y = 0;
}
//main.cpp
#include<iostream>
#include<string>
#include"move.h"
int main()
{   
    using namespace std;
    Move m1 = {5,4};
    std::cout << "now m1 info is:\n";
    m1.showmove();
    Move m = { 1,1 };
    std::cout << "now m info is:\n";
    m.showmove();
    Move *p = &m;
    p = &p->add(m1);
    std::cout << "now m1+m info is:\n";
    p->showmove();
    p->reset();
    std::cout << "reset the data:\n";
    p->showmove();
    system("pause");
    return 0;
}
//7*****************************************
//plorga.h
#ifndef PLORG_H_
#define PLORG_H_
class plorg
{
private:
    static const int max = 19;
    char name[max];
    int CI;

public:
    plorg(char *na = "vincent",int ci =50);
    ~plorg();
    void set(int ci);
    void show();
    void update(char *na, int ci);
};
#endif // !STACK_H_
//plorga.cpp
#include<iostream>
#include<cstring>
#include<string>
#include"plorg.h"

plorg::plorg(char * na, int ci)
{
    strcpy_s(name, strlen(na) + 1, na);
    CI = ci;
}

plorg::~plorg()
{
}

void plorg::set(int ci)
{   
    CI = ci;
}

void plorg::show()
{
    std::cout << "the name is: " << name<<"\n";
    std::cout << "the CI Value is: " << CI << "\n";
}

void plorg::update(char * na, int ci)
{
    strcpy_s(name, strlen(na) + 1, na);
    CI = ci;
}
//main.cpp
#include<iostream>
#include<string>
#include"plorg.h"
int main()
{   
    char name[19];
    int ci;
    plorg p1 = plorg();
    p1.show();
    std::cout << "please input the name: ";
    std::cin.get(name, 19).get();
    std::cout << "please input the CI value: ";
    std::cin >> ci;
    std::cout << "update the CI value: \n";
    p1.set(ci);
    p1.show();
    std::cout << "after reset:\n ";
    p1.update(name, ci);
    p1.show();
    system("pause");
    return 0;
}
//8
//list.h
#ifndef LIST_H_
#define LIST_H_

struct songs
{
    std::string songname;
    int year;
};
typedef struct songs Item;
class list 
{
private:
    static const int len = 10;
    Item items[len];
    int count;
public:
    list(void);
    ~list();
    bool isempty(void);
    bool isfull(void);
    int itemcount();
    bool add(Item item);
    void visit(void(*p)(Item item));
};
#endif // !LIST_H_
//list.cpp
#include<iostream>
#include<cstring>
#include<string>
#include"list.h"

list::list(void)
{
     count = 0;
}

list::~list()
{
}

bool list::isempty(void)
{
    if (count == 0)
        return true;
    else 
        return false;
}

bool list::isfull(void)
{
    if (count == len)
        return true;
    else
        return false;
}

int list::itemcount()
{
    return count;
}

bool list::add(Item item)
{
    if (isfull())
        return false;
    else
        items[count++] = item;
        return true;
}

void list::visit(void(*p)(Item item))
{
    for (int i = 0; i < count; ++i)
        (*p)(items[i]);

}
//main.cpp
#include <iostream>
#include <cstdlib>     
#include <string>
#include "list.h"     
void show(Item item);

int main(void)
{
    using namespace std;
    list m;
    Item s;
    cout << "please input the songs you like: ";
    while (getline(cin, s.songname) && s.songname != "\0")
    {
        cout << "please input the songdate,just the year like XXXX: ";
        cin >> s.year;
        while (cin.get() != '\n')
            continue;
        if (m.isfull())
        {
            cout << "you just filled the list.Done.\n";
            break;
        }
        cout << "Enter next song please.(empty line to quit): ";
    }
    if (m.isempty())
        cout << "No songs been entered!";
    else
    {
        cout << "the songs you just input is as belows:\n";
        m.visit(show);
    }

    system("pause");
    return 0;
}
void show(Item item)
{
    std::cout << "the song is " << item.songname << "\n";
    std::cout << "its open year is " << item.year << "\n";
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,001评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,210评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,874评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,001评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,022评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,005评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,929评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,742评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,193评论 1 309
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,427评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,583评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,305评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,911评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,564评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,731评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,581评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,478评论 2 352