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

小编 2026-06-05 阅读:1141 评论:0
1. //头文件stock00.h #ifndef STOCK00_H_ #define STOCK00_H_ #include<cstring> // class definition...

1.

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

#include<cstring>

// 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<iostream>
#include<cstring>
#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<iostream>
#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<string>
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<iostream>
#include<string>
#include<cstring>
#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<iostream>
#include<string>
#include<cstring>
#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<iostream>
#include<cstring>
#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<iostream>
#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<iostream>
#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 <<sales[i] << \" \" << std::endl;
	std::cout << \"The average is:\" << average << std::endl;
	std::cout << \"The max is:\" << max << std::endl;
	std::cout << \"The min is:\" << min << std::endl;
}
//主函数usestock0.cpp
#include<iostream>
#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<iostream>
#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<iostream>
#include<cctype>
#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<iostream>
#include<cstring>
#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<iostream>
#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<iostream>
#include<string>
#include<cstring>
#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<iostream>
#include<string>
#include<cstring>
#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<iostream>
#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<iostream>
#include<string>
#include<cctype>
#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<<endl;
	st.add(1);
	st.add(2);
	st.add(3);
	st.add(4);
	cout << \"\\nNow:\" << 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 << endl;
	void(*pf)(Item & item);
	pf = func;
	st.visit(pf);
	system(\"pause\"); //显示暂停

	return 0;
}


void func(Item & item)
{
	cout << item << endl;
}

 

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表