C++PrimerPlus第十章编程练习答案参考

本文深入探讨了C++中类与对象的应用,通过多个实例展示了如何定义类、创建对象、实现成员函数,并进行存款、取款等操作。同时,介绍了如何通过继承扩展类的功能,实现了银行账户、个人资料、高尔夫球手信息、销售业绩统计等功能,涵盖了构造函数、析构函数、运算符重载等关键概念。
//bank.h
#ifndef BANK_H_
#define BANK_H_
#include<string>

class Bank
{
	private:
		std::string m_name;
		std::string m_id;
		double m_deposit;

	public:
		Bank();
		Bank(std::string name, std::string id, double deposit);
		void show() const;
		void save(double num);
		void withdraw(double num);
};

#endif

//bank.cpp
#include<iostream>
#include"bank.h"

Bank::Bank(){}

Bank::Bank(std::string name, std::string id, double deposit)
{
	m_name = name;
	m_id = id;
	m_deposit = deposit;
}

void Bank::show() const
{
	std::cout << "[" << m_name << "]\n"
	<< " ID: " << m_id << '\t' << "Deposit: " << m_deposit;
}

void Bank::save(double num)
{
	if(num<0)
		std::cout << "You can't save money less than zero! "
		<< "Transaction is aborted.\n";
	else
	{
		m_deposit += num;
		std::cout << "Deposit successfully!\n";
	}
}

void Bank::withdraw(double num)
{
	if(num>m_deposit)
		std::cout << "You can't withdraw money more than you have! "
		<< "Transaction is aborted.\n";
	else
	{
		m_deposit -= num;
		std::cout << "Withdraw successfully!\n";
	}
}

//main.cpp
#include<iostream>
#include<cctype>
#include"bank.h"

int main()
{
	using namespace std;

	double in_money;
	double out_money;
	char ch;

	Bank* account = new Bank{"李四", "361", 40000};
	account->show();
	cout << "\nPlease enter S to save money, W to withdraw money, 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 'S':
			case 's':
				cout << "Please enter the money you want to save: ";
				cin >> in_money;
				account->save(in_money);
				break;
			case 'W':
			case 'w':
				cout << "Please enter the money you want to withdraw: ";
				cin >> out_money;
				account->withdraw(out_money);
				break;
		}
  		account->show();
		cout << "\nPlease enter S to save money, W to withdraw money, or Q to quit:\n";
	}
	cout << "Bye!\n";

	return 0;
}
//person.h
#ifndef PERSON_H_
#define PERSON_H_
#include<string>

class Person
{
	private:
		static const int LIMIT = 25;
		std::string lname;
		char fname[LIMIT];
	public:
		Person(){lname = ""; fname[0] = '\0';}
		Person(const std::string &ln, const char *fn = "Heyyou");
		void Show() const;
		void FormalShow() const;
};

#endif

//person.cpp
#include<iostream>
#include<cstring>
#include"person.h"

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

void Person::Show() const
{
	std::cout << "LastName: " << lname;
}

void Person::FormalShow() const
{
	std::cout <<"\nFirstName: " << fname;
}

//main.cpp
#include<iostream>
#include"person.h"

int main()
{
	using std::cout;
	using std::endl;
	Person one;
	Person two("Smythecraft");
	Person three{"Dimwiddy", "Sam"};
	one.Show();
	one.FormalShow();
	cout << endl;
	two.Show();
	two.FormalShow();
	cout << endl;
	three.Show();
	three.FormalShow();

}
//Golf.h
#ifndef GOLF_H
#define GOLF_H
#include<cstring>

static const int Len = 40;
class Golf
{
	private:
		char fullname[40];
		int handicap;
	public:
		Golf();
		Golf(const char *name, int hc)
		{
			strcpy(fullname, name);
			handicap = hc;
		}
		int setGolf();
		int setGolf(const char *name, int hc);
		int setHandicap(const int hc);
		void showGolf() const;
		
};

#endif

//Golf.cpp
#include<iostream>
#include"Golf.h"

Golf::Golf(){}

int Golf::setGolf()
{
	using std::cout;
	using std::cin;
	cout << "Enter the fullname and handicap:\n";
	if(cin.getline(fullname, Len) && fullname[0]!='\0')
	{
		cin >> handicap;
		cin.get();
		return 1;
	}
	else
		return 0;
}

int Golf::setGolf(const char *name, int hc)
{
	Golf g = Golf(name, hc);
	*this = g;
	if(fullname[0]!='\0')
		return 1;
	else
		return 0;
}

int Golf::setHandicap(const int hc)
{
	handicap = hc;
}

void Golf::showGolf() const
{
	using std::cout;
	cout << "Fullname[" << fullname << "]\t"
	<< "Handicap: " << handicap << "\n";
}

//main.cpp
#include<iostream>
#include"Golf.h"

const int Size=3;
int main()
{
	using std::cout;
	using std::cin;

	Golf anny[Size];
	Golf ann[Size];
	char* name = new char[Len];
	int hc, i=0;
	//第一组:遇到空行自动设置,不跳过循环
	for(i=0; i<Size; i++)
	{
		cout << "Enter the fullname and handicap:\n";
		cin.getline(name, Len);
		if(name[0]=='\0')
		{
			hc=0;
			anny[i].setGolf(name, hc);
			cout << "Empty!\n";
			continue;
		}
		else
		{
			cin >> hc;
			anny[i].setGolf(name, hc);
			cin.get();
		}
	}
	delete[] name;
	cout << "________________________\n";
	i=0;
	//第二组:遇到空行直接结束循环
	while(i<Size && ann[i].setGolf())
		++i;
 //将剩余的进行赋值
    while(i++<Size)
		ann[i].setGolf("", 0);
		
	cout << "\nAll the players:\n";
	for(i=0; i<Size; i++)
	{
		anny[i].showGolf();
	}
	for(i=0; i<Size; i++)
	{
		ann[i].showGolf();
	}
}
//names.h
#include<iostream>
namespace SALES
{
	static const int QUARTERS = 4;
	class Sales
	{
		private:
			double sales[QUARTERS];
			double m_Average;
			double m_Max;
			double m_Min;
		public:
			Sales();
			Sales(const double ar[], int n);
			void setSales();
			void showSales() const;
	};
}

//definition
#include"names.h"

namespace SALES
{
	Sales::Sales()
	{
		for(int i=0; i<QUARTERS; i++)
			sales[i] = 0.0;
		m_Average = 0.0;
		m_Max = 0.0;
		m_Min = 0.0;
	}
	Sales::Sales(cosnt double ar[], int n)
	{
		int times = QUARTERS>n ? n:QUARTERS;
		double arMax=ar[0], arMin=ar[0];
		double total=0.0;
		for(int i=0; i<times; i++)
		{
			sales[i] = ar[i];
			total += sales[i];
			if(arMax<ar[i])
				arMax = ar[i];
			if(arMin>ar[i])
				arMin = ar[i];
		}
		m_Max = arMax;
		m_Min = arMin;
		m_Average = total/times;
		if(n<QUARTERS)
		{
			for(int i=n; i<QUARTERS; i++)
				sales[i] = 0.0;
		}
	}
	
	void Sales::setSales()
	{
		int size;
		double total=0.0;
		cout << "Enter the array size: ";
		cin >> size;
		double* ar = new double[size];
		cout << "Enter " << size << " double array:\n";
		for(int i=0; i<size; i++)
		cin >> ar[i];
		std::cout << "Enter the array size(size<5): ";
		std::cin >> size;
		std::cout << "Enter " << size << " array:\n";
		for(int i=0; i<size; i++)
		{
			std::cin >> sales[i];
			total += sales[i];
		}
		double max = sales[0];
		double min = sales[0];
		for(int i=1; i<size; i++)
		{
			if(max<sales[i])
				max = sales[i];
			if(min>sales[i])
				min = sales[i];
		}
		m_Max = max;
		m_Min = min;
		m_Average = total/size;
		if(size<QUARTERS)
		{
			for(int i=size; i<QUARTERS; i++)
				sales[i] = 0.0;
		}
	}

	void Sales::showSales() const
	{
		using std::cout;
		using std::endl;
		for(int i=0; i<QUARTERS; i++)
			cout << "[" << i+1 << "] " << sales[i] << endl;
		cout << "MAX: " << m_Max << endl
		<< "MIN: " << m_Min << endl
		<< "AVERAGE: " << m_Average << endl;
	}
}

//main.cpp
#include"names.h"

int main()
{
	using namespace SALES;
	using namespace std;

	Sales* s1 = new Sales;
	Sales* s2 = new Sales;
	
	s1->setSales();
	s1->showSales();
	
	s2->setSales();
	s2->showSales();

	return 0;
}
//stack.h
#ifndef STACK_H_
#define STACK_H_

static double total=0;

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);
		double getTotal(){ return total;}
};

#endif

//stack.cpp
#include<iostream>
#include"stack.h"

Stack::Stack()
{//empty 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)
	{
		total += item.payment;
		items[top++] = item;
		std::cout << " Total: " << getTotal() << '\n';
		return true;
	}
	else
		return false;
}
bool Stack::pop(Item &item)
{
	if(top>0)
	{
		total += item.payment;
		item = items[--top];
		std::cout << " Total: " << getTotal() << '\n';
		return true;
	}
	else
		return false;
}

//stacker.cpp
#include<iostream>
#include<cctype>
#include"stack.h"

/*
    模拟售货员的行为
    使用栈的后进先出的方式,从购物筐的最上面开始处理购物订单
*/
int main()
{
	using namespace std;
	Stack st;
	Item item;
	char ch;

	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 the fullname to add:";
				cin.getline(item.fullname, 35);
				cout << "Enter the payment to add:";
				cin >> item.payment;
				if(st.isFull())
					cout << "stack already full\n";
				else
					st.push(item);
				break;
			case 'P':
			case 'p':
				if(st.isEmpty())
					cout << "stack already empty\n";
				else
				{
					st.pop(item);
					cout << "PO #" << item.fullname << " popped\n";
				}
				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_H_
#define MOVE_H_

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<iostream>
#include"move.h"

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

void Move::showMove() const
{
	std::cout << "The x = " << x
	<< ", and y = " << y << '\n';
}

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;
}

//main.cpp
#include<iostream>
#include"move.h"

int main()
{
	using std::cout;
	using std::cin;
	double a, b;
	Move* m1 = new Move();
	cout << "Enter two numbers: " ;
	cin >> a >> b;
	Move m2(a, b);
	
	m1->showMove();
	m1->add(m2);
	m1->showMove();
	m2.showMove();
	m1->reset(a, b);
	m1->showMove();
	
	return 0;
}
//plorg.h
#ifndef PLORG_H_
#define PLORG_H_

const int size=19;

class Plorg
{
	private:
		char name[size];
		int CI;

	public:
		Plorg(int ci=50);
		void setCI(int ci);
		void showPlorg() const;
		
};

#endif

//plorg.cpp
#include<iostream>
#include<cstring>
#include"plorg.h"


Plorg::Plorg(int ci)
{
	strcpy(name, "Plorga");
	CI = ci;
}
void Plorg::setCI(int ci)
{
	CI = ci;
}

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

//main.cpp
#include<iostream>
#include"plorg.h"

int main()
{
	using std::cin;
	int ci;
	cin >> ci;
	Plorg* p1 = new Plorg();
	p1->setCI(ci);
	p1->showPlorg();
	
	Plorg p2(ci);
	p2.showPlorg();
	p2.setCI(80);
	p2.showPlorg();
	
	return 0;
}
  1. 第八题看见链表啥的,直接学链表去了,这里附上一个刚写成的单链表的实现的链接。至于题目嘛,嘿嘿,先欠着这是链表的链接:
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值