第十章编程练习答案
10.1为复习题5类提供定义,并演示
//10.1为复习题5类提供定义,并演示
#include
using namespace std;
class BankAccount
{
string m_name;
string m_num;
unsigned m_balance;
public:
BankAccount (string name, string num, unsigned balance)
{
m_name = name;
m_num = num;
m_balance = balance;
}
void show () const
{
cout << m_name << endl;
cout << m_num << endl;
cout << m_balance << endl;
}
void incDeposit (unsigned amount)
{
m_balance += amount;
}
void decDeposit (unsigned amount)
{
if (m_balance <= amount)
m_balance = 0;
else
m_balance -= amount;
}
};
int main ()
{
BankAccount bank("Tom", "1", 1024);
bank.show();
bank.incDeposit(128);
bank.show();
bank.decDeposit(2048);
bank.show();
}
10.2根据以下类定义和例子,完成类方法定义
//10.2根据以下类定义和例子,完成类方法定义
#include
#include
using namespace std; class Person { static const unsigned LIMIT = 25; string lname; char fname[LIMIT]; public: Person() {lname = ""; fname[0] = '\0'; } // #1 Person(const string & ln, const char * fn = "Heyyou"); // the following methods display lname and fname void Show() const; // firstname lastname format void FormalShow() const; // lastname, firstname format }; Person::Person(const string & ln, const char * fn) { lname = ln; strcpy(fname, fn); } 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"); three.Show(); three.FormalShow(); }
10.3编写golf类,包含姓名和成绩两个数据,并实现调用
//10.3编写golf类,包含姓名和成绩两个数据,并实现调用
#include
#include
using namespace std; const int Len = 40; class golf { string fullname; int handicap; public: golf ():fullname("no"),handicap(0) {} golf (const char * name, int hc) { fullname = name; handicap = hc; } void showgolf() { cout << fullname << '\t' << handicap << endl; } }; int main () { golf golfer; golf golfers("tom",100); golfer.showgolf(); golfers.showgolf(); }
10.4根据编程练习9.4,用Sales类完成
//10.4根据编程练习9.4,用Sales类完成
#include
using namespace std;
class Sales
{
static const int QUARTERS = 4;
double sales[QUARTERS];
double size;
double average;
double max;
double min;
public:
Sales (double arr[], int n)
{
unsigned times = n < QUARTERS (unsigned)n : QUARTERS;
for (unsigned i = 0; i < times; ++i)
sales[i] = arr[i];
for (unsigned i = times; i < QUARTERS; ++i)
sales[i] = 0;
size = times;
average = calcAverage();
max = calcMax();
min = calcMin();
}
void setSales()
{
cout << "输入" << QUARTERS << "个销售记录:";
size = QUARTERS;
for (unsigned i = 0; i < QUARTERS; ++i) {
cin >> sales[i];
if (!cin) {
size = i;
break;
}
}
for (unsigned i = (unsigned)size; i < QUARTERS; ++i)
sales[i] = 0;
cin.clear();
average = calcAverage();
max = calcMax();
min = calcMin();
}
double calcAverage () const
{
double sum = 0;
for (unsigned i = 0; i < size; ++i)
sum += sales[i];
return (sum / size);
}
double calcMax () const
{
unsigned idxMax = 0;
for (unsigned i = 0; i < size; ++i)
if (sales[i] > sales[idxMax])
idxMax = i;
return (sales[idxMax]);
}
double calcMin () const
{
unsigned idxMin = 0;
for (unsigned i = 0; i < size; ++i)
if (sales[i] < sales[idxMin])
idxMin = i;
return (sales[idxMin]);
}
void showSales () const
{
cout << "sales: ";
for (const auto& e : sales)
cout << e << ' ';
cout << endl;
cout << "average: " << average << endl;
cout << "max: " << max << endl;
cout << "min: " << min << endl;
}
};
int main ()
{
double salesLst[] = {12.2, 11.16, 10.61, 16.24, 11.53};
Sales salesBook(salesLst, sizeof(salesLst)/sizeof(salesLst[0]));
salesBook.showSales();
}
10.5编写一个程序,从栈中添加和删除customer结构(包括全名和数量两个成员),每删除一个结构,把数量计入总数。
//10.5编写一个程序,从栈中添加和删除customer结构(包括全名和数量两个成员),每删除一个结构,把数量计入总数。
#include
using namespace std;
struct customer{
char fullname[35];
double payment;
};
typedef customer Item;
class Stack{
Item items[10];
int top;
public:
Stack() :top(0){ }
bool isEmpty() const { return top==0; }
bool isFull() const { return top==10; }
bool push(const Item &item)
{
if(isFull())
{
cout<<"Error !Stack is full!"<
10.6根据以下类编写测试
//10.6根据以下类编写测试 #include
#include
using namespace std; class Move { double m_x; double m_y; public: Move (double a, double b) { m_x = a; m_y = b; } void showMove () const { cout << '(' << m_x << ", " << m_y << ')'; } Move add (const Move& m) const { return (Move(m.m_x + m_x, m.m_y + m_y)); } void reset (double a, double b) { m_x = a; m_y = b; } }; int main () { Move one(12, 4); one.showMove(); cout << endl; Move two(1, 1); one.add(two).showMove(); cout << endl; one.reset(3,4); one.showMove(); cout << endl; }
10.7编写类Plorg和函数实现plorg这些特征:
数据:名称不超过19字符;有满意指数整数CI
操作:新的plorg有名称,CI值50;CI可修改;plorg可报告名称和CI;默认名称为“Plorga”
//10.7编写类Plorg和函数实现plorg这些特征:
//数据:名称不超过19字符;有满意指数整数CI
//操作:新的plorg有名称,CI值50;CI可修改;plorg可报告名称和CI;默认名称为“Plorga”
#include
#include
using namespace std; class Plorg { char _name[20]; unsigned CI; public: Plorg () : CI(50) { strcpy(_name,"Plorga"); } Plorg (const char* name) { CI = 50; strcpy(_name, name); _name[19] = '\0'; } void setCI (unsigned ci) { CI = ci; } void showPlorg () const { cout << _name << ' ' << CI << endl; } }; int main () { Plorg plorg; plorg.showPlorg(); plorg.setCI(12); plorg.showPlorg(); }
10.8设计简单列表类:可储存多种类型;可创建空列表;可在列表中添加数据项;可确定列表的空满;可访问每一个项并操作
#include
#include
using namespace std; class TList { public: typedef int Item; public: TList (const Item arr[] = NULL, unsigned n = 0); bool isFull (void) const; bool isEmpty (void) const; bool append (const Item& item); void visit (void (*pf) (Item& item)); private: static const unsigned mk_capacity = 4; private: Item m_content[mk_capacity]; unsigned m_size; }; TList::TList (const Item arr[], unsigned n) { if (NULL == arr) { m_size = 0;