C++ Primer Plus(第六版)第十章课后编程答案

1.

//头文件stock00.h
#ifndef STOCK00_H_
#define STOCK00_H_

#include

// class definition
class BankAccount
{
private:
	char name[40];    //or std::string name;
	char acctnum[25];    //or std::string acctnum;
	double balance;
public:
	BankAccount(const char *client, const char *num, double bal = 0.0);
	//or BankAccount(const std::string & client, const std::string & num,double bal=0.0);

	void show(void) const;
	void deposit(double cash);
	void withdraw(double cash);
};

#endif
//类定义stock00.cpp
#include
#include
#include"stock00.h"

BankAccount::BankAccount(const char * client, const char * num, double bal)
{
	strncpy(name, client,39);           //直接赋值
    name[39]='\0';
	strncpy(acctnum, num,24);
    acctnum[24]='\0';
	balance = bal;
}

void BankAccount::deposit(double cash)
{
	using std::cout;
	if (cash < 0)
	{
		cout << "Number of cash can't be negative.\n";
	}
	else
		balance += cash;
}

void BankAccount::withdraw(double cash)
{
	using std::cout;
	if (cash < 0)
	{
		cout << "Number of cash can't be negative.\n";
	}
	else if (cash > balance)
		cout << "You can't get more than you have!\n";
	else
		balance -= cash;
}

void BankAccount::show(void) const
{
	std::cout << "Name: " << name << '\n'
		<< "AcctNum: " << acctnum << '\n'
		<< "Balance: " << balance << '\n';
}
//主函数usestock0.cpp
#include
#include "stock00.h"
int main()
{
	using std::cout;
	cout << "Bank Account information:\n";
	BankAccount b1("huangfu shuyun", "ZHT", 1200);
	b1.show();
	BankAccount b2("zhao qian", "PTO", 5000);
	b2.show();

	cout << "\nAfter a month:\n";
	b1.deposit(1000);
	b1.show();
	b1.withdraw(2000);
	b1.show();
	b2.withdraw(500);
	b2.show();
	b2.deposit(2000);
	b2.show();

	return 0;

}

2.

//头文件stock00.h
#ifndef STOCK00_H_
#define STOCK00_H_

#include
using namespace std;
// class definition
class Person
{
private:
	static const int LIMIT = 25;
	string lname;       //Person's last name
	char fname[LIMIT];   //Person's first name
public:
	Person() { lname = ""; fname[0] = '\0'; }      //#1
	Person(const string & ln, const char *fn = "Heyyou");    //#2
	//the following methods display lname and fname
	void Show() const;           //firstname lastname format
	void FormalShow() const;    //lastname,firstname format
};

#endif
//类定义stock00.cpp
#include
#include
#include
#include"stock00.h"

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

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

void Person::FormalShow() const
{
	std::cout << "Formal Name:" << std::endl;
	std::cout << lname << " " << fname << std::endl;
}
//主函数usestock0.cpp
#include
#include
#include
#include"stock00.h"

int main()
{
	using std::cout;
	using std::endl;
	Person one;
	one.Show();
	cout << endl;
	one.FormalShow();
	cout << endl;

	Person two("Smythecraft");
	two.Show();
	cout << endl;
	two.FormalShow();
	cout << endl;

	Person three("Dimwdiddy", "Sam");
	three.Show();
	cout << endl;
	three.FormalShow();
	cout << endl;

	return 0;
}

3.

//头文件stock00.h
#ifndef STOCK00_H_
#define STOCK00_H_

//class definition
using namespace std;
const int Len = 40;
class Golf{
private:
	char fullname[Len];
	int handicap;
public:
	Golf(char *name, int hc=0);
	int setgolf();
	void sethandicap(int hc);
	void showgolf()const;
};

#endif
//类定义stock00.cpp
#include
#include
#include "stock00.h"

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

int Golf::setgolf()
{
	using std::cout;
	using std::endl;
	static int i = 0;      //静态变量
	cout << "#" << ++i << ":\n";
	cout << "Please enter the name: ";
	cin.getline(fullname, Len);
	cout << "Please enter the grade:";
	cin >> handicap;
	cin.get();
	
	Golf g(fullname, handicap);
	*this = g;
	return fullname==""?0:1;        //判断是否为空行
}

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

void Golf::showgolf()const
{
	using std::cout;
	using std::endl;
	cout << "The name of golf is: " << fullname << endl;
	cout << "The grade of golf is: " << handicap << endl;
}





//主函数usestock0.cpp
#include
#include"stock00.h"

int main()
{
	using std::cout;
	using std::endl;

	Golf g("huangfu shuyun", 20);
	g.showgolf();
	cout << endl;
	g.setgolf();
	cout << endl;
	g.showgolf();
	cout << endl;
	g.sethandicap(100);
	g.showgolf();

	return 0;
}


4.

//头文件stock00.h
#ifndef STOCK00_H_
#define STOCK00_H_

namespace SALES     //将类保留在名称空间中
{
	const int QUARTERS = 4;
	class Sales
{
private:
	double sales[QUARTERS];
	double average;
	double max;
	double min;
public:
	Sales();
	Sales(const double *ar, int n);
	void setSales();
	void showSales() const;
};
}
#endif







//类定义stock00.cpp
#include
#include "stock00.h"
//类函数位于名称空间SALES中

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

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

void SALES::Sales::setSales()
{
	std::cout << "Please enter sales:";
	double sum = 0.0;
	for (int i = 0; i < QUARTERS; i++)
	{
		std::cin >> sales[i];
		sum += sales[i];
	}
	std::cin.get();
	average = sum / QUARTERS;
	max = min = sales[0];
	for (int i = 0; i < QUARTERS; i++)
	{
		if (max < sales[i])
			max = sales[i];
		if (min > sales[i])
			min = sales[i];
	}
}

void SALES::Sales::showSales() const
{
	for (int i = 0; i < QUARTERS; i++)
		std::cout <
//主函数usestock0.cpp
#include
#include"stock00.h"
const int ArSize = 4;
int main()
{
	using std::cin;
	using std::cout;
	using std::endl;

	double a[ArSize];         
	cout << "Please enter numbers:";
	for (int i = 0; i < ArSize; i++)
		cin >> a[i];
	SALES::Sales s(a, ArSize);
	s.showSales();

	s.setSales();      
	s.showSales();

	return 0;
}


5.

//头文件stock00.h   从程序清单10.10改写而来
#ifndef STOCK00_H_
#define STOCK00_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
//类定义stock00.cpp
#include
#include "stock00.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;
}



//主函数usestock0.cpp
#include
#include
#include"stock00.h"

int main()
{
	using namespace std;
	double payment=0;
	Stack st;    //创建一个空栈
	char ch;
	Item 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')    //Q,q退出
	{
		while (cin.get() != '\n')
			continue;
		if (!isalpha(ch))
		{
			cout << '\a';
			continue;
		}
		switch (ch)
		{
		case 'A':
		case 'a':cout << "Enter a PO fullname:";
			cin.getline(po.fullname,40);
			cout << "Enter a PO number to add:";
			cin >> po.payment;
			cin.get();
			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);
					 cout << "PO:" << po.fullname << " payment:" << po.payment << " popped.\n";
					 payment += po.payment;
					 cout << "The total of payment is:" << payment << endl;
				 }
				 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.

//头文件stock00.h
#ifndef STOCK00_H_
#define STOCK00_H_

using namespace std;
// class definition
class Move
{
private:
	double x;
	double y;
public:
	Move(double a = 0, double b = 0);     //sets x,y to a,b
	void showmove() const;                     //shows current x,y values
	Move add(const Move &m) const;
	void reset(double a = 0, double b = 0);
};

#endif
//类定义stock00.cpp
#include
#include
#include"stock00.h"

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

void Move::showmove() const
{
	using std::cout;
	using std::endl;
	cout << "Please show current x,y values: " << x << " " << 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;
}
//主函数usestock0.cpp
#include
#include"stock00.h"

int main()
{
	using std::cout;
	using std::endl;
    
	Move m1(1, 2);
	Move m2(2, 3);
	Move m3(-1, -1);
	cout << "The first place:\n";
	m1.showmove();

	cout << "Move m2:\n";
	m1.add(m2).showmove();
	cout << "Then move m3:\n";
	m1.add(m2).add(m3).showmove();

	cout << "Place reset:\n";
	m1.reset();
	m1.showmove();
	return 0;
}

7. 

//头文件stock00.h
#ifndef STOCK00_H_
#define STOCK00_H_

//class definition
using namespace std;
class Plorg
{
private:
	string name;         //char name[20];
	int CI;
public:
	Plorg(string name="Plorga" , int CI = 50);      //默认构造参数
	                                            //Plorg(char name[20]="Plorga" , int CI = 50); 
	void update(const int & n);
	void show() const;
};

#endif
//类定义stock00.cpp
#include
#include
#include
#include"stock00.h"

Plorg::Plorg(string m , int n)         //Plorg::Plorg(char * m , int n)  
{
	name = m;              //strcpy(name,m); 字符数组的赋值           
	CI = n;
}

void Plorg::update(const int & n)
{
	CI = n;
}

void Plorg::show() const
{
	using std::cout;
	using std::endl;
	cout << "Please show the name and CI of Plorg: " << endl;
	cout << "name: " << name << " CI: " << CI << endl;
}

//主函数usestock0.cpp
#include
#include
#include
#include"stock00.h"

int main()
{
	using std::cout;
	using std::endl;
    
	Plorg m1;
	m1.show();

	string s;         //char s[20];
	int n;
	cout << "Please enter the plorg name:";
	getline(cin, s);         //cin.getline(s,20);
	cout << "Please enter the CI:";
	cin >> n;
	Plorg m2(s, n);
	m2.show();

	int count;
	cout << "Please enter the CI:";
	cin >> count;
	m1.update(count);
	m1.show();
	
	return 0;
}

8.参考网上代码:

//头文件stock00.h  类定义
#ifndef STOCK00_H_
#define STOCK00_H_

typedef int Item;    //类型别名声明

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

#endif
//类方法实现stock00.cpp
#include
#include "stock00.h"

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

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

bool List::add(const Item item)
{
	if (top < MAX)
	{
		items[top++] = item;
		return true;
	}
	else
		return false;
}

void List::visit(void(*pf)(Item & item))
{
	std::cout << "\nDisplay the item:\n";
	for (int i = 0; i < top; i++)
		(*pf)(items[i]);         //显示每个item
}
//主函数usestock0.cpp
#include
#include
#include
#include"stock00.h"
using namespace std;

void func(Item & item);//函数声明

int main()
{
	List st;
	string str;
	cout << "At first:" << endl;
	if (st.isempty() == 1)
		str = "Yes!";
	if (st.isempty() == 0)
		str = "No!";
	cout << "isempty? " << str << endl;
	if (st.isfull() == 1)
		str = "Yes!";
	if (st.isfull() == 0)
		str = "No!";
	cout << "isfull? " << str<

 

你可能感兴趣的:(C++编程习题)