C++ Prime Plus 第六版 第十章 編程練習 對象和類 參考答案 詳細版

以下答案本人在VS2019中均已親自測試編譯通過,完美運行.

10.1

//Bank.h

#pragma once
#include<iostream>
class BankAccounts
{
public:
	BankAccounts();
	BankAccounts(const std::string& name, const std::string& accounts, long double money);
	void showAccounts() const;						//顯示信息
	void deposit(long double money);				//存款功能
	void Withdraw(long double money);				//取款功能
	~BankAccounts();

private:
	std::string m_name;
	std::string m_accounts;
	long double m_money;
};
//Bank.cpp

#include"Bank.h"
BankAccounts::BankAccounts()
{
	std::cout << "這是默認參數:" << std::endl;
	m_name = "NO NAME";
	m_accounts = "000000";
	m_money = 0.0;
}

BankAccounts::BankAccounts(const std::string& name, const std::string& accounts, long double money)
{
	m_name = name;
	m_accounts = accounts;
	m_money = money;
}

void BankAccounts::showAccounts() const
{
	using::std::cout;
	using::std::endl;
	cout << "儲戶姓名:" << m_name << endl
		<< "儲戶賬號:" << m_accounts << endl
		<< "當前存款:" << m_money << endl;
}

void BankAccounts::deposit(long double money)				  //存款功能
{
	if (money > 0)
	{
		m_money += money;
		std::cout << "存款成功!!!\n";
	}
	else
		std::cout << "您的操作非法!!!\n";
}

void BankAccounts::Withdraw(long double money)				  //取款功能
{
	if (m_money > 0 && money > 0)
	{
		m_money -= money;
		std::cout << "取款成功!!!\n";
	}
	else
		std::cout << "您的操作非法!!!\n";
}

BankAccounts::~BankAccounts()
{
}
//main.cpp

#include<iostream>
#include"Bank.h"
int main()
{
	using namespace std;
	BankAccounts a1;	 //調用無參數的默認構造函數
	a1.showAccounts();
	cout << endl;
	BankAccounts a2("小明", "888888888VIP", 5000.0);	//調用有參數數的構造函數
	a2.showAccounts();
	double Addmoney;
	cout << "請輸入存款金額:";
	cin >> Addmoney;
	a2.deposit(Addmoney);
	a2.showAccounts();
	double Submoney;
	cout << "請輸入取款金額:";
	cin >> Submoney;
	a2.Withdraw(Submoney);
	a2.showAccounts();
}

10.2

//Persion.h

#pragma once
#include<iostream>
class person
{
public:
	person();
	person(const std::string& ln, const char* fn = "Heyyou");
	void Show() const;
	void FormalShow() const;
	~person();

private:
	static const int LIMIT = 25;
	std::string lname;		//last name
	char fname[LIMIT];		//first name
};
//Person.cpp

#include"Person.h"
#include<string.h>
person::person()
{
	lname = " ";
	fname[0] = '\0';
}
person::person(const std::string& ln, const char* fn)
{
	lname = ln;
	strcpy_s(fname, fn);
}
void person::Show() const
{
	using::std::cout;
	using::std::endl;
	cout << "first name: " << fname << endl
		<< "last  name: " << lname << endl;
}
void person::FormalShow() const
{
	using::std::cout;
	using::std::endl;
	cout << "last  name: " << lname << endl
		<< "first name: " << fname << endl;
}
person::~person()
{
}
//main.cpp

#include<iostream>
#include"Person.h"
int main()
{
	person one;
	person two("wu xiaoming");
	person three("xiaored", "Sam");
	one.Show();
	one.FormalShow();
	std::cout << std::endl;
	two.Show();
	two.FormalShow();
	std::cout << std::endl;
	three.Show();
	three.FormalShow();
	std::cout << std::endl;
}

10.3

//golf.h

#ifndef GOLF_H
#define GOLF_H
#include<iostream>
#include<string>

class golf
{
public:
	golf();
	golf(const std::string& name, int hc);
	golf(golf& g);
	void sethandicap(int hc);
	void showgolf() const;
	~golf();
private:
	std::string fullname;
	int handicap;
};
#endif
//golf.cpp

#include"golf.h"
golf::golf()
{
	fullname = "no name";
	handicap = 0;
}

golf::golf(const std::string& name, int hc)
{
	fullname = name;
	handicap = hc;
}

golf::golf(golf& g)
{
	using std::cout;
	using std::cin;
	cout << "請輸入姓名:";
	getline(cin, g.fullname);
	fullname = g.fullname;
	cout << "請輸入等級:";
	cin >> g.handicap;
	handicap = g.handicap;
}
void golf::sethandicap(int hc)
{
	handicap = hc;
}

void golf::showgolf() const
{
	using std::cout;
	using std::cin;
	using std::endl;
	cout << "姓名: " << fullname << endl;
	cout << "等級: " << handicap << endl;
}

golf::~golf()
{
}
//main.cpp

#include<iostream>
#include"golf.h"
int main()
{
	golf a1;
	a1.showgolf();
	std::cout << std::endl;
	golf a2("WU", 888);
	a2.showgolf();
	std::cout << std::endl;
	golf a3;
	golf a4(a3);
	a4.showgolf();
	std::cout << std::endl;
	a4.sethandicap(999);
	a4.showgolf();
}

10.4

//Salesclass.h

#pragma once
#include<iostream>
namespace SLASS
{
	class Sales
	{
	public:
		Sales();
		Sales(const double arr[], int n);
		Sales(Sales& s);
		void showSales() const;
		~Sales();

	private:
		static const int QUARTERS = 4;
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	};
}
//Salesclass.cpp

#include"Salesclass.h"
namespace SLASS
{
	Sales::Sales()
	{
		for (int i = 0; i < QUARTERS; i++)
			sales[i] = 0;
		average = 0;
		max = 0;
		min = 0;
	}
	Sales::Sales(const double arr[], int n)
	{
		double sum = 0;
		max = arr[0], min = arr[0];
		for (int i = 0; i < n; i++)
		{
			sales[i] = arr[i];
			sum += sales[i];
		}
		for (int i = 1; i < n; i++)
		{
			if (max < sales[i])
				max = sales[i];
			if (min > sales[i])
				min = sales[i];
		}
		average = sum / n;
	}
	Sales::Sales(Sales& s)
	{
		using::std::cout;
		using::std::cin;
		using::std::endl;
		double sum = 0;
		cout << "請輸入4個數:";
		for (int i = 0; i < QUARTERS; i++)
		{
			cin >> s.sales[i];
			sales[i] = s.sales[i];
			sum += sales[i];
		}
		max = sales[0], min = sales[0];
		for (int i = 1; i < QUARTERS; i++)
		{
			if (max < sales[i])
				max = sales[i];
			if (min > sales[i])
				min = sales[i];
		}
		average = sum / QUARTERS;
	}
	void Sales::showSales() const
	{
		using::std::cout;
		using::std::endl;
		for (int i = 0; i < QUARTERS; i++)
			cout << "sales[" << i << "] = " << sales[i] << endl;
		cout << "average :" << average << endl
			<< "  max   :" << max << endl
			<< "  min   :" << min << endl << endl;
	}
	Sales::~Sales()
	{
	}
}
//main.cpp

#include<iostream>
#include"Salesclass.h"
int main()
{
	using namespace SLASS;
	Sales a1;
	a1.showSales();
	double arr[4] = { 200, 100, 2, 101 };
	Sales a2(arr, 4);
	a2.showSales();
	Sales a3;
	Sales a4(a3);
	a4.showSales();
}

10.5

//stack.h

#ifndef STACK_H_
#define STACK_H_

struct customer
{
	char fullname[35];
	double payment;
};

typedef customer Item;

class Stack
{
public:
	Stack();
	bool isempty() const;
	bool isfull() const;
	bool push(const Item& item);
	bool pop(Item& item);
	~Stack();

private:
	enum { MAX = 10 };
	Item items[MAX];
	int top;
};
#endif // !STACK_H;
//stack.cpp

#include"stack.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;
}
Stack::~Stack()
{
}
//stacker.h.cpp

#include<iostream>
#include<cctype>
#include"stack.h"
int main()
{
	using namespace std;
	Stack st;
	char ch;
	long double SumPayment = 0;
	customer people{ "no name", 0.0 };
	cout << "Please enter A to add a purchase order, \n"
		<< "P to process a PoP , 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 a people's fullname to add: ";
			cin.getline(people.fullname, 35);
			cout << "Enter a people's payment to add: ";
			cin >> people.payment;
			if (st.isfull())
				cout << "stack already full!!!\n";
			else
				st.push(people);
			break;
		case 'p':
		case 'P':
			if (st.isempty())
				cout << "stack already isempty!!!\n";
			else
			{
				st.pop(people);
				SumPayment += people.payment;
				cout << "people # fullname г║" << people.fullname << " popped\n"
					<< "people # payment г║" << people.payment << " popped\n"
					<< "Total amount paid г║" << SumPayment << " гд\n";
			}
			break;
		}
		cout << "Please enter A to add a purchase order, \n"
			<< "P to process a PoP , or Q to quit.\n";
	}
	cout << "Bye!!!\n";
	return 0;
}

10.6

//move.h

#pragma once
#include<iostream>
class Move
{
public:
	Move(double a = 0, double b = 0);
	void showmove() const;
	Move add(const Move& m);
	void reset(double a = 0, double b = 0);
private:
	double x;
	double y;
};

 

//move.cpp

#include"move.h"

Move::Move(double a, double b)
{
	x = a;
	y = b;
}
void Move::showmove() const
{
	std::cout << "x: " << x << "\ny: " << y << std::endl;
}
Move Move::add(const Move& m)
{
	Move n;
	n.x = x + m.x;
	n.y = y + m.y;
	return n;
}
void Move::reset(double a, double b)
{
	x = a;
	y = b;
}
//main.cpp

#include"move.h"
int main()
{
	Move a1;
	a1.showmove();
	a1.reset(20.0, 20.0);
	a1.showmove();
	Move a2;
	a2.reset(80.0, 80.0);
	a2.showmove();
	Move a3;
	a3 = a2.add(a1);
	a3.showmove();
	std::cout << "\nBye!!!\n";
}

10.7

//Plorgaclass.h

#pragma once

class Plorg
{
public:
	Plorg();
	Plorg(const char* _name, int _CI = 50);
	void modify_CI(int _CI);
	void show() const;
	~Plorg();

private:
	char name[19];
	int CI;
};
//Plorgaclass.cpp

#include"Plorgaclass.h"
#include<iostream>
#include<cstring>

Plorg::Plorg()
{
	strcpy_s(name, "Plorga");
	CI = 0;
}
Plorg::Plorg(const char* _name, int _CI)
{
	strcpy_s(name, _name);
	CI = _CI;
}
void Plorg::modify_CI(int _CI)
{
	CI = _CI;
}
void Plorg::show() const
{
	std::cout << "name: " << name << std::endl
		<< " CI: " << CI << std::endl;
}
Plorg::~Plorg()
{
}

//main.cpp

#include"Plorgaclass.h"
int main()
{
	Plorg a1;
	a1.show();
	a1.modify_CI(88);
	a1.show();
	Plorg a2("testname", 66);
	a2.show();
}

10.8

//list.h


#pragma once

typedef int Item;
class List
{
public:
	List();
	bool isfull() const;
	bool isempty() const;
	bool add(const Item& item);
	void visit(void(*pf)(Item& item));
	void showitem() const;
	~List();

private:
	enum { MAX = 10 };
	Item num[MAX];
	int top = 0;
};

void mul100(Item& item);
//list.cpp

#include"list.h"
#include<iostream>
List::List()
{
	top = 0;
}
bool List::isempty() const
{
	return top == 0;
}

bool List::isfull() const
{
	return top == MAX;
}
bool List::add(const Item& item)
{
	if (top < MAX)
	{
		num[top++] = item;
		return true;
	}
	else
		return false;
}
void List::visit(void(*pf)(Item& item))
{
	for (int i = 0; i < top; i++)
	{
		pf(num[i]);
	}
}
void List::showitem() const
{
	for (int i = 0; i < top; i++)
	{
		std::cout << "#" << i + 1 << ": " << num[i] << std::endl;
	}
}
List::~List()
{
}
void mul100(Item& item)
{
	item *= 100;
}
//main.cpp

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

using namespace std;

int main()
{
	cout << "Please enter C to create your list, P to process the list, S to show the items, Q to quit:\n";
	char ch;
	List l;
	int data;
	while (cin >> ch && toupper(ch) != 'Q')
	{
		while (cin.get() != '\n')
			continue;
		switch (ch)
		{
		case 'C':
		case 'c':
			if (l.isfull())
				cout << "The list already full.\n";
			else
			{
				cout << "Enter the data: ";
				cin >> data;
				l.add(data);
			}
			break;
		case 'P':
		case 'p':
			if (l.isempty())
				cout << "The list already empty.\n";
			else
			{
				l.visit(mul100);
				cout << "Every data has multiplication 100.\n";
			}
			break;
		case 'S':
		case 's':
			l.showitem();
			break;
		}
		cout << "Please enter C to create your list, P to process the list, S to show the items, Q to quit:\n";
	}
	l.showitem();
	cout << "Bye!!!\n";
	system("pause");
	return 0;
}

 

發佈了15 篇原創文章 · 獲贊 1 · 訪問量 646
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章