logo头像

不忘初心,奋力前行

C++ Primer Plus(第五版)第10章编程题答案

本文于541天之前发表,文中内容可能已经过时,如有问题,请联系我。

1.为复习题5描述的类提供方法定义,并编写一个小程序来演示所有的特性。 bankacc.h

#ifndef BANKACC_H_1

#define BANKACC_H_1
class Bank {
char name[30];
char account[7];
double debt;
public:
Bank();
Bank(const char *co, const char *ac, double db = 0.0);
~Bank();
void show() const;
void deposit(const double money);
void withdrawal(const double money);
};

#endif // !BANKACC_H_1

bankacc.cpp

#include

#include

#include

#include”bankacc.h”
using namespace std;
Bank::Bank() {
strcpy(name, “no name”);
name[30] = ‘\\0’;
strcpy(account, “100000”);
account[6] = ‘\\0’;
debt = 0.0;
}
Bank::Bank(const char *co, const char *ac, double db)
{
strncpy(name, co, 29);
strncpy(account, ac,6);
name[30] = ‘\\0’;
account[6] = ‘\\0’;
debt = db;

}
Bank::~Bank() {
cout << “Your account has been deleted.Thank your for using our service.\\n”;
}
void Bank::show() const
{
cout << “Account Name: “ << name << endl;
cout << “Account ID: “ << account << endl;
cout << “Debt: $” << debt << endl;
}
void Bank::deposit(const double money)
{
debt += money;
cout << “Your Debt: $” << debt << endl;
cout << “Thank you for using deposit service.Bye~\\n”;

}
void Bank::withdrawal(const double money) {
if (money <= debt)
{
debt -= money;
cout << “Your Debt: $” << debt << endl;
}

else
    cout << "Error, you do not have enough money.\\n";
cout << "Thank you for using withdrawal service.Bye~\\n";

}

banker.cpp

#include

#include

#include

#include”bankacc.h”
using namespace std;

int main() {
cout << “This is Bank example 1:” << endl;
Bank banks1;
banks1.show();
cout << “After depositing…”<<endl;
double depos1 = 3692.21;
banks1.deposit(depos1);
cout << “After withdrawal…” << endl;
banks1.withdrawal(1252.63);
cout << “#########################################” << endl;
double money2 = 8945.32;
Bank banks2 = Bank(“Yushuai Zhang”, “H6365”, money2);
banks2.show();
cout << “After depositing…” << endl;
double depos2 = 235.21;
banks2.deposit(depos2);
cout << “After withdrawal…” << endl;
banks2.withdrawal(625.63);

system("pause");
return 0;

}

2.下面是一个非常简单的类定义: (……代码省略……) 通过提供未定义的方法来完成这个类的实现。然后,编写一个使用这个类的程序,它使用了几种可能的构造函数调用(没有参数、一个参数和两个参数)以及两种显示方法。 person.cpp

#include

#include

#include
using namespace std;
const int LIMIT = 25;
class Person {
private:
string lname;
char fname[LIMIT];
public:
Person();
Person(const string &ln, const char*fn = “Heyyou”);
void Show() const;
void FormalShow() const;
};
Person::Person()
{
lname = “”;
fname[0] = ‘\\0’;

}
Person::Person(const string & ln, const char *fn) {
lname = ln;
strncpy(fname, fn, LIMIT-1);
fname[LIMIT - 1] = ‘\\0’;

}
void Person::Show()const {
cout << fname << “ “ << lname << endl;

}

void Person::FormalShow() const {
cout << lname << “ “ << fname << endl;

}
int main() {
Person one;
Person two(“Smythecraft”);
Person three(“Dimwiddy”, “Sam”);
one.Show();
cout << endl;
one.FormalShow();
cout << endl;
two.FormalShow();
cout << endl;
three.Show();
system(“pause”);
return 0;
}

3.完成第9章的编程练习1,但要用正确的golf类声明替换那里的代码。用带合适参数的构造函数替换setgolf(golf &, const char , int), 以提供初始值。保留setgolf()的交互版本,但要用构造函数来实现它(例如,setgolf()的代码应该获得数据,将数据传递给构造函数来创建一个临时对象,并将其赋给调用对象,即this)。 golf.h

#ifndef GOLF_H

#define GOLF_H
class Golf {
private:
static const int Len = 40;
char fullname[Len];
int handicap;
public:
Golf(const char * name = “HHHHHH”, int hc = 10);
~Golf();
bool setGolf();
void sethandicap(int hc);
void showGolf()const;
};

#endif // GOLF_H

golf.cpp

#include

#include

#include “golf.h”
using namespace std;

Golf::Golf(const char * name, int hc)
{
strncpy(fullname, name, 40);
fullname[39] = ‘\\0’;
handicap = hc;
}

Golf::~Golf()
{
cout << “Bye!” << endl;
}

bool Golf::setGolf()
{
char fullname[Len];
int handicap;

cout << "Please input the name of the golf player: ";
cin.get(fullname, Len); 
if (cin)
{
    cout << "Please input the handicap of the golf player: ";
    cin >> handicap;
    cin.get();
    *this = Golf(fullname, handicap);
    return true;
}
else
    return false;

}

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

void Golf::showGolf()const
{
std::cout << “hullname: “ << fullname << “, \\thandicap: “ << handicap << std::endl;
}

main.cpp

#include

#include “golf.h”
using namespace std;
int main()
{
Golf one;
Golf two;
Golf three;

one.showGolf();         // 默认构造函数
two.setGolf();          // 交互版本
two.showGolf();
two.sethandicap(5000);  //成员函数
two.showGolf();         //成员函数
three.sethandicap(20);
three.showGolf();
system("pause");
return 0;

}

5.考虑下面的结构声明:

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

编写一个程序,它从栈中添加和删除customer结构(栈用Stack类声明表示)。每次customer结构被删除时,其payment的值都被加入到总数中,并报告总数。注意:应该可以直接使用Stack类而不作修改;只需修改typedef声明,使Item的类型为customer,而不是unsigned long即可。 stack.h

#pragma once

#ifndef STACK_H_

#define STACK_H_
typedef struct customer
{
char fullname[35];
double payment;
}CUSTOMER;

typedef customer Item;

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

#endif // STACK_H_

static.cpp

#include

#include “stack.h”
using namespace std;
Stack::Stack()
{
top = 0;
total = 0;
}

Stack::~Stack()
{
cout << “Bye!\\n”;
}

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];
total +=item.payment;
cout << “PO#” << item.fullname << endl;
cout << “Total = “ << total << endl;
return true;
}
else
return false;
}

main.cpp

#include

#include

#include “stack.h”

int main()
{
using namespace std;
Stack st; // create an empty stack
char ch;
CUSTOMER 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’)
{
while (cin.get() != ‘\\n’)
continue;
if (!isalpha(ch))
{
cout << ‘\\a’;
continue;
}
switch (ch)
{
case ‘A’:
case ‘a’:
cout << “Enter a PO fullname to add:\\t”;
cin.getline(po.fullname, 35);
cout << “Enter a PO payment to add:\\t”;
cin >> po.payment;
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);
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. 根据类声明,提供成员函数的定义和测试这个类的程序。

#include
using namespace std;
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);
};
Move::Move(double a, double b)
{
x = a;
y = b;
}
void Move::showmove()const {
cout << “x= “ << x << endl;
cout << “y= “ << y << 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;
}

int main() {
Move move1;
cout << “Move1:\\n”;
move1.showmove();
Move move2 = Move(2.3, 1.9);
cout << “Move2:\\n”;
move2.showmove();
move1 = move1.add(move2);
cout << “Move1 after adding move2:\\n”;
move1.showmove();
move1.reset();
move2.reset();
cout << “Reset Move1:\\n”;
move1.showmove();
cout << “Reset Move2:\\n”;
move2.showmove();
system(“pause”);
return 0;

}

备注: 操作系统:Windows 10 x64(1803) 编译器:Microsoft Visual Studio 2015

支付宝打赏 微信打赏 QQ钱包打赏

感觉不错?欢迎给我 打个赏~我将不胜感激!