C++Primer Plus第十章编程练习答案

第一题

cbank.h

#ifndef __bank__
#define __bank__
#include<cstring>

class bank_account
{
private:				//隐藏数据
	//std::string name;
	//std::string acctnum;
	enum {SIZE = 40};
	char name[SIZE];	//姓名
	char acctnum[SIZE];	//账号
	double balance;		//存款
public:					//公共
	bank_account(const char* client, const char* num, double bal = 0.0);
	//bank_account(const std::string client,const std::string num,double bal);
	void show()const;
	void deposit(double cash);
	void withdram(double cash);
};
#endif

bank.cpp

#define _CRT_SECURE_NO_WARNINGS  //windows认为strncpy该函数不正确所以要加上该宏

#include"cbank.h"
#include<iostream>

/*
* bank_account(const std::string client,const std::string num,double bal)
* {
*		name = client;
*		acctnum = num;
*		balance = bal;
* }
*/

bank_account::bank_account(const char* client, const char* num, double bal)
{
	strncpy(name, client, SIZE - 2);
	name[SIZE - 1] = '\0';
	strncpy(acctnum, num, SIZE - 2);
	acctnum[SIZE - 1] = '\0';
	balance = bal;
}

void bank_account::show()const
{
	using std::cout;
	using std::endl;

	cout << "Name of depositor: " << name << endl
		 << "Depositor account number: " << acctnum
		 << endl << "Saver deposits:" << balance << endl;
}

void bank_account::deposit(double cash)
{
	balance += cash;
}

void bank_account::withdram(double cash)
{
	if (balance > 0)
		balance -= cash;
	else
		std::cout << "no bank";
}

bank_account.cpp

#include"cbank.h"
#include<iostream>

int main()
{
	bank_account back{"hello","21324050109"};//显示调用构造函数
	
	back.show();
	back.deposit(100000);
	back.show();
	back.withdram(10000);
	back.show();

	return 0;
}

第二题

person.h

#ifndef __hash__
#define __hash__

#include<string>

using std::string;

class Person
{
private:
	static const int LIMIT = 25;
	string lname;								//person's lase name
	char fname[LIMIT];							//person's first name
public:
	Person() { lname = ""; fname[0] = '\0'; }
	Person(const string& ln, const char* fn = "Heyyou");
	//以下方法显示 lname 和 fname
	void Show()const;							//firstname lasename format
	void FormalShow()const;						//lastname,firstname,format
};

#endif

person.cpp

#define _CRT_SECURE_NO_WARNINGS

#include"person.h"
#include<string.h>
#include<iostream>

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

void Person::Show()const
{
	using std::cout;
	using std::endl;

	cout << "fname: " << fname << endl
		 << "lname: " << lname << endl;
}

void Person::FormalShow()const
{
	using std::cout;
	using std::endl;

	cout << "lname: " << lname << endl
		 << "fname: " << fname << endl;
}

main.cpp

#include"person.h"
#include<iostream>

int main()
{
	Person one;									//使用默认构造函数
	Person two("Smythecraft");					//将 #2 与一个默认参数一起使用
	Person three("Dimwiddy", "Sam");			//YSE #2,无默认值
	one.Show();
	std::cout << std::endl;
	one.FormalShow();
	std::cout << std::endl;

	using std::cout;
	using std::endl;

	two.Show();
	cout << endl;
	two.FormalShow();
	cout << endl;

	three.Show();
	cout << endl;
	three.FormalShow();
	cout << endl;

	return 0;
}

第三题

golf.h

#define _CRT_SECURE_NO_WARNINGS
#ifndef __golf__
#define __golf__
#include<string.h>

class golf
{
private:
	static const int Len = 40;
	char fullname[Len];
	int handicaps;
public:
	golf() { strncpy(fullname, "NULL", Len); handicaps = 0; }
	golf(const char* full_name,int hand_icap = 0);
	void handicap(int g);
	void showgolf()const;
};

#endif

golf.cpp

#define _CRT_SECURE_NO_WARNINGS					//vs认为strncpy不安全所以加上该宏

#include"golf.h"
#include<iostream>

golf::golf(const char* full_name, int hand_icap)
{
	strncpy(fullname, full_name, Len - 2);
	fullname[Len - 1] = '\0';
	handicaps = hand_icap;
}

void golf::handicap(int g)
{
	handicaps = g;
}

void golf::showgolf()const
{
	using std::cout;
	using std::endl;

	cout << "fullname: " << fullname << endl
		 << "handicap: " << handicaps << endl;
}

main.cpp

#include"golf.h"
#include<iostream>

const int Len = 40;

int main()
{
	golf go;											//调用默认构造函数
	golf go3[4];
	char fullname[Len];
	int handicap;

	go.showgolf();
	go.handicap(2);
	go.showgolf();

	std::cout << std::endl;
	
	for (int i = 0; i < 3; i++)
	{
		printf("fullname: ");							//C函数
		std::cin >> fullname;
		printf("handicap: ");
		std::cin >> handicap;

		go3[i] = golf(fullname, handicap);				//创建临时对象
	}

	std::cout << std::endl;

	go3[0].showgolf();
	go3[1].showgolf();
	go3[2].showgolf();
	go3[3].showgolf();
	
	putchar('\n');										//C风格 cout.put('\n')
	go3[2].handicap(3);
	go3[2].showgolf();

	return 0;
}

第四题

sales.h

#ifndef __sales__
#define __sales__
#include<iostream>

namespace SALES
{
	const int QUARTERS = 4;

	class Sales
	{
		private:
			double sales[QUARTERS];
			double average;
			double max;
			double min;
		public:
			Sales(const double ar[], int n);
			Sales();
			void showSales()const;
	};
}

#endif

sales.cpp

#include"sales.h"

namespace SALES
{
	Sales::Sales(const double ar[], int n)						//定义和声明必须位于同一个名称空间下
	{
		int count, i, temp;

		average = count = i = temp = 0;

		for (i = 0; i < QUARTERS && i < n; i++)
		{
			sales[i] = ar[i];
			count++, average += sales[i];
		}

		while (i < QUARTERS)
			sales[i++] = 0;

		average /= count;
		min = max = sales[0];

		for (int k = 1; k < count; k++)
		{
			if (max < sales[k])
				max = sales[k];
			else if (min > sales[k])
				min = sales[k];
		}
	}

	Sales::Sales()									//可以写个公共成员函数将上面的构造函数里面代码写到公共成员函数里面 然后调用即可 构造函数不可调用构造函数
	{
		using std::cin;
		int i;

		average = 0;
		cin >> sales[0];
		max = min = sales[0];
		average += sales[0];

		for (i = 1; i < QUARTERS; i++)
		{
			cin >> sales[i];
			if (max < sales[i])
				max = sales[i];
			else if (min > sales[i])
				min = sales[i];
			average += sales[i];
		}
		average /= i;
	}

	void Sales::showSales()const
	{
		for (int i = 0; i < QUARTERS; i++)
		{
			printf("sales %d: %.3lf\n", i + 1, sales[i]);			//C风格	%d:打印整数 %lf:打印双精度 %f:打印单精度 %.3lf保留小数点后三位
		}
		printf("average value: %.3lf\nmax: %.3lf\nmin: %.3lf\n",
				average, max, min);
	}
}

main.cpp

#include<iostream>
#include"sales.h"

int main()
{
	using namespace SALES;

	double ar[QUARTERS + 1]{ 1.2, 3.4, 2.4, 1.1, 3.1};

	Sales s = Sales(ar,QUARTERS + 1);	//显示调用构造函数
	Sales ss;							//隐士调用构造函数

	s.showSales();
	std::cout.put('\n');
	ss.showSales();

	return 0;
}

第五题

stack.h

#ifndef STACK_H_
#define STACK_H_

typedef struct customer
{
	char fullname[35];
	double payment;
}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.cpp

#define _CRT_SECURE_NO_WARNINGS //vs认为strncpy不安全所以加上该宏

#include"stack.h"
#include<string.h>
#include<iostream>
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)
	{
		strncpy(items[top].fullname, item.fullname, 35 - 2);
		items[top].fullname[35 - 1] = '\0';
		items[top++].payment = item.payment;
		return true;
	}
	else
		return false;
}

//出栈
bool Stack::pop(Item& item)
{
	static double sum_d = 0.0;					//静态存储

	if (top > 0)
	{
		item = items[--top];
		sum_d += items[top].payment;
		printf("%.3lf\n", sum_d);				//%.3lf打印小数保留小数点三位
		return true;
	}
	else
		return false;
}

main.cpp

/*
* 下面代码其中的一些提示字符串就不做修改了,
*  都是书上的stack类例子的一些提示字符串
*/
#include<iostream>
#include<cctype>
#include"stack.h"
int main()
{
	using namespace std;
	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')
	{
		while (cin.get() != '\n')
			continue;

		if (!isalpha(ch))
		{
			cout << '\a';
			continue;
		}

		switch (ch)
		{
		case 'A':
		case 'a':
			cout << "Please enter a first name: ";
			cin >> po.fullname;
			printf("payment: ");												//C函数
			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);
				printf("fullname: %s\npayment: %.3lf", po.fullname, po.payment); //%s打印字符串 %.3lf保留小数点3位
			}
			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;
}

第六题

move.h

#ifndef __move__
#define __move__

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);
};
#endif

move.cpp

#include"move.h"
#include<iostream>

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

Move Move::add(const Move& m)const
{
	Move move{ m.x,m.y };//显示调用
	return move;
}

void Move::showmove()const
{
	printf("x -> %.3lf\ny -> %.3lf\n", x, y);
	//std::cout << "x -> " << x << '\n' 
			  //<< "y -> " << y << "\n";
}

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

main.cpp

#include<iostream>
#include"move.h"

int main()
{
	Move move;
	Move mo{ 2,5 };

	move = move.add(mo);
	move.showmove();

	move.reset();
	move.showmove();

	return 0;
}

第七题

plorg

#ifndef _plorg_
#define _plorg_
#include<string>

class Plorg
{
private:
	std::string plorg;
	long CI;
public:
	Plorg(std::string plorgs = "Plorga", long CIS = 50);
	void revise(long C);
	void showplorg()const;
};
#endif

plorg.cpp

#include"plorg.h"
#include<iostream>

Plorg::Plorg(const std::string plorgs, long CIS)
{
	plorg = plorgs;
	CI = CIS;
}

void Plorg::revise(long C)
{
	CI = C;
}

void Plorg::showplorg()const
{
	std::cout << "plorg: " << plorg
			  << '\n' << "CI: " << CI << '\n';
}

main.cpp

#include<iostream>
#include"plorg.h"

int main()
{
	Plorg plorg;

	plorg.showplorg();
	plorg.revise(30);
	plorg.showplorg();

	return 0;
}

第8题

list.h

#ifndef _List_
#define _List_
#include<stdbool.h>									//提供bool

typedef long Item;

class list
{
private:
	enum{max = 10};
	Item li[max];									//Item类型列表
	Item top;
public:
	list() { top = 0; }								//默认构造函数
	bool list_empty()const;							//查看是否为空
	bool list_full()const;							//查看是否为满
	bool list_add(Item item);						//添加数据
	void list_show()const;							//查看
	void visit(void (*pf)(Item& item));				//修改数据
};
#endif

list.cpp

#include"list.h"
#include<iostream>

bool list::list_empty()const
{
	return top == 0;
}

bool list::list_full()const
{
	return top == max;
}

bool list::list_add(Item item)
{
	if (list::list_full())
	{
		std::cout << "full\n";
		return false;
	}

	li[top++] = item;
	return true;
}

void list::list_show()const
{
	for (int i = 0; i < top; i++)
	{
		std::cout << li[i] << '\n';						
	}
}

void list::visit(void (*pf)(Item& item))
{
	for (int i = 0; i < top; i++)
	{
		pf(li[i]);							//对数据进行修改
		printf("%d\n", li[i]);
	}
}

main.cpp

#include<iostream>
#include"list.h"
#include<cctype>

void vis(Item& item);					//对调用对象进行更改操作

int main()
{
	list list_li;
	
	if (list_li.list_add(45))
		puts("添加成功");				//该函数输出完会自动在后面加上换行符

	if (list_li.list_add(50))
		puts("添加成功");


	list_li.list_show();
	putchar('\n');						//C函数和cout.put('\n')一样的效果
	list_li.visit(vis);

	return 0;
}

void vis(Item& item)
{
	item -= 10;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值