c++ Primer Plus 第十章 答案

第1题

bankaccount.h

//bankaccount.h

#ifndef BANKACCOUNT_H_
#define BANKACCOUNT_H_

class Bankaccount
{
private:
	std::string m_name;
	std::string m_account;
	double m_money;

public:
	Bankaccount();
	Bankaccount(const std::string& name, const std::string& account, double money);
	~Bankaccount();
	void show() const;
	void deposit(const double in_money);
	void withdraw(const double out_money);
};

#endif // !BANKACCOUNT_H_

bankaccount.cpp

//bankaccount.cpp

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

Bankaccount::Bankaccount()
{
	std::cout << "Default constructor called\n";
	m_name = "no name";
	m_account = "000000";
	m_money = 0.0;
}

Bankaccount::Bankaccount(const std::string& name, const std::string& account, double money)
{
	std::cout << "Not Default constructor called\n";
	m_name = name;
	m_account = account;
	m_money = money;
}

Bankaccount::~Bankaccount() {}

void Bankaccount::show() const
{
	using std::cout;
	cout << "******************\n";
	cout << "Name: " << m_name << "\n";
	cout << "Account: " << m_account << "\t Money: " << m_money << "\n";
	cout << "******************\n";
	cout << "\n\n";
}

void Bankaccount::deposit(const double in_money)
{
	if (in_money < 0)
	{
		std::cout << "Number of in_money can't be negative.\n";
	}
	else
	{
		m_money += in_money;
	}
}

void Bankaccount::withdraw(const double out_money)
{
	if (out_money < 0)
	{
		std::cout << "Number of in_money can't be negative.\n";
	}
	else if (out_money > m_money)
	{
		std::cout << "You can't withdraw more than you have!\n";
	}
	else
	{
		m_money -= out_money;
	}
}

main.cpp

//main.cpp

#include <iostream>
#include "bankaccount.h"
void showmenu();

int main()
{
	std::cout<<std::fixed;
	std::cout.precision(2);

	Bankaccount BA;
	BA.show();

	Bankaccount A1 = Bankaccount("xiao ming","000001",1000000);
	A1.show();

	showmenu();
	double cash;
	char ch;
	while (std::cin >> ch && toupper(ch) != 'Q')
	{
		switch (ch)
		{
		case 's':
		case 'S':A1.show();
			break;

		case 'd':
		case 'D':std::cout << "How many for deposit: ";
			std::cin >> cash;
			A1.deposit(cash);
			A1.show();
			break;

		case 'w':
		case 'W':std::cout << "How many for withdraw: ";
			std::cin >> cash;
			A1.withdraw(cash);
			A1.show();
			break;

		default:
			std::cout << "Error input.\n";
			break;
		}
		showmenu();
	}
	std::cout << "Bye!\n";
}

void showmenu()
{
	using std::cout;
	cout << "**********Menu List**********\n"
		<< "s) Show information \t d) For deposit\n"
		<< "w) For withdraw \t q) Quit\n"
		<< "Input your choose: ";
}

第2题
persons.h

#ifndef PERSONS_H_
#define PERSONS_H_
#include <string>

class Person
{
private:
	static const int LIMIT = 25;
	std::string lname;  //Person's last name
	char fname[LIMIT]; //Person's first name

public:
	Person() { lname = ""; fname[0] = '\0'; }  //#1
	Person(const std::string& ln, const char* fn = "Heyyou");  //#2
	//~Person();
	//the following methods display lname and fname
	void Show() const;  //firstname lastname format
	void FormalShow() const;  //lastname, firstname format
};

#endif

persons.cpp

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

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

void Person::Show() const
{
	std::cout << "Show()\n";
	std::cout << "First name: " << fname << "\t Last name: " << lname << std::endl;
}

void Person::FormalShow() const
{
	std::cout << "FormalShow()\n";
	std::cout << "Last name: " << lname << "\t First name: " << fname << std::endl;
}

main.cpp

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

int main()
{
	using namespace std;
	cout << "Person one:\n";
	Person one;
	one.Show();
	one.FormalShow();

	cout << "\nPerson two:\n";
	Person two("Smythecraft");
	two.Show();
	two.FormalShow();

	cout << "\nPerson three:\n";
	Person three("Dimwiddy", "Sam");
	three.Show();
	three.FormalShow();
}

第3题
golf.h

#ifndef GOLF_H_
#define GOLF_H_

class Golf
{
private:
	static const int Len = 40;
	char Fullname[Len];
	int Handicap;

public:
	Golf();
	Golf(const char* name, int hc);
	~Golf();

	int setGolf();
	void sethandicap(int hc);
	void showgolf() const;
};

#endif // !GOLF_H_

golf.cpp

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

Golf::Golf()
{
	Fullname[0] = '\0';
	Handicap = 0;
}

Golf::Golf(const char* name, int hc)
{
	strcpy_s(Fullname, strlen(name) + 1, name);
	Handicap = hc;
}


Golf::~Golf()
{

}

int Golf::setGolf()
{
	using std::cout;
	using std::endl;
	using std::cin;
	char temp_Fullname[Len - 1];
	int temp_Handicap;
	cout << "Please enter the full name of golf player:\n";
	cin.getline(temp_Fullname, Len - 1);
	if (strlen(temp_Fullname) == 0)
	{
		return 0;
	}
	else
	{
		cout << "Please enter the handicap of golf player:\n";
		cin >> temp_Handicap;
		cin.get();
		*this = Golf(temp_Fullname, temp_Handicap);
		return 1;
	}
}

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

void Golf::showgolf() const
{
	std::cout << "Fullname: " << Fullname << std::endl;
	std::cout << "Handicap: " << Handicap << std::endl;
}

main.cpp

#include <iostream>
#include "golf.h"
const int max = 5;

int main()
{
	Golf g0;
	g0.showgolf();

	Golf g1("xiao ming", 23);
	g1.showgolf();
	g1.sethandicap(100);
	g1.showgolf();

	Golf g[max]; 
	int i;
	for ( i = 0; i < max; i++)
	{
		int temp = g[i].setGolf();
		if (temp == 0)
		{
			std::cout << "None name entered. Exit\n";
			break;
		}
	}

	for (int j = 0; j < i; j++)
	{
		g[j].showgolf();
	}
}

第4题
sales.h

#ifndef SALES_H_
#define SALES_H_

class Sales
{
private:
	static const int QUARTERS = 4;
	double sales[QUARTERS];
	double average;
	double max;
	double min;

public:
	Sales();
	Sales(const double ar[], int n);
	~Sales();
	void setSales();
	void showSales() const;
};

#endif // !SALES_H_

sales.cpp

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

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

Sales::Sales(const double ar[], int n)
{
	for (int i = 0; i < n; i++)
	{
		sales[i] = ar[i];
	}
	for (int i = n; i < QUARTERS; i++)
	{
		sales[i] = 0;
	}

	double sum = 0;
	max = sales[0];
	min = sales[0];
	for (int i = 0; i < QUARTERS; i++)
	{
		sum += sales[i];
		max = max > sales[i] ? max : sales[i];
		min = min < sales[i] ? min : sales[i];
	}
	average = sum / QUARTERS;
	
}

Sales::~Sales()
{

}

void Sales::setSales()
{
	using std::cout;
	using std::cin;

	int times;
	cout << "Input sales times: ";
	cin >> times;
	while (times < 0 || times>4)
	{
		cout << "Times must between 0-4.\n"
			<< "Inpute sales times: ";
		cin >> times;
	}
	double* ar = new double[QUARTERS];
	for (int i = 0; i < times; i++)
	{
		cout << "Enter Quarter #" << i + 1 << " sales: ";
		cin >> ar[i];
	}
	*this = Sales(ar, times);
}

void Sales::showSales() const
{
	using std::cout;
	using std::endl;
	for (int i = 0; i < QUARTERS; i++)
	{
		cout << "Quarter " << i + 1 << " sales: " << sales[i] << endl;
	}
	cout << "Average: " << average << endl;
	cout << "Max: " << max << endl;
	cout << "Min: " << min << endl << endl;
}

main.cpp

#include "sales.h"

int main()
{
	double arr1[4] = { 100,800,400,300 };
	Sales s1(arr1, 4);
	s1.showSales();

	double arr2[2] = { 100,800 };
	Sales s2(arr2, 2);
	s2.showSales();

	Sales s3;
	s3.setSales();
	s3.showSales();
}

第5题
stack.h

#ifndef STACK_H_
#define STACK_H_

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

typedef customer Item;

class Stack
{
private:
	enum { max = 3 };
	customer customers[max];
	int top;

public:
	Stack();
	~Stack() {};
	bool isempty() const;
	bool isfull() const;
	void push(const customer& ct);
	void pop(customer& ct);
};

#endif // !STACK_H_

stack.cpp

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

Stack::Stack()
{
	top = 0;
}

bool Stack::isempty() const
{
	if (top == 0)
	{
		std::cout << "Stack already empty.\n";
		return true;
	}
	else
	{
		return false;
	}
}

bool Stack::isfull() const
{
	if (top == max)
	{
		std::cout << "Stack already full.\n";
		return true;
	}
	else
	{
		return false;
	}
}


void Stack::push(const customer& ct)
{
	if (top == max)
	{
		std::cout << "Stack already full.\n";
	}
	else if (top >= 0 && top < max)
	{
		customers[top++] = ct;
	}
}

void Stack::pop(customer& ct)
{
	if (!isempty())
	{
		ct = customers[--top];	
	}
}

main.cpp

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

int main()
{
	using std::cout;
	using std::cin;
	Stack st;
	char ch;
	customer temp;
	double totalpayment = 0.0;
	cout << "Enter A to push customer information. D to pop customer information. Q to quit.\n";
	while (cin >> ch && toupper(ch) != 'Q')
	{
		while (cin.get() != '\n')
		{
			continue;
		}
		if (!isalpha(ch))
		{
			cout << "Must to be a letter, Enter your choice:\n";
			continue;
		}
		switch (ch)
		{
		case 'a':
		case 'A':
			if (!st.isfull())
			{
				cout << "Enter fullname: ";
				cin.getline(temp.fullname, 35);
				cout << "Enter payment: ";
				cin >> temp.payment;
				st.push(temp);
			}
			break;

		case 'd':
		case 'D':
			if (!st.isempty())
			{
				st.pop(temp);
				totalpayment += temp.payment;
				cout << "The customer " << temp.fullname << " information has been delete.\n";
				cout << "Totalpayment is " << totalpayment << " now.\n";
			}
			break;

		default:
			cout << "Error input.\n";
			break;
		}
		cout << "Enter A to push customer information. D to pop customer information. Q to quit.\n";
	}
	cout << "Totalpayment is " << totalpayment << ".\n";
	cout << "Bye.\n";
}

第6题
move.h

#ifndef MOVE_H_
#define MOVE_H_

class Move
{
private:
	double x;
	double y;

public:
	Move(double a = 0, double b = 0);  //sets x, y to a, b
	void showmove() const;
	Move add(const Move& m) const;
	void reset(double a = 0, double b = 0);  //resets x, y to a, b
};

#endif // !MOVE_H_

move.cpp

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

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

void Move::showmove() const
{
	std::cout << "\tx = " << x << std::endl;
	std::cout << "\ty = " << y << std::endl;
}

Move Move::add(const Move& m) const
{
	Move temp;
	temp.x = x + m.x;
	temp.y = y + m.y;
	return temp;
}

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

main.cpp

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

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

	cout << "M1:\n";
	Move m1;
	m1.showmove();
	cout << "m1 reset:\n";
	m1.reset(1, 2);
	m1.showmove();

	cout << "M2:\n";
	Move m2(3, 4);
	m2.showmove();
	Move m3 = m2.add(m1);  //也可以m2 = m2.add(m1);
	cout << "M2 + M1:\n";
	cout << "m1:\n";
	m1.showmove();
	cout << "m2:\n";
	m2.showmove();
	cout << "m3:\n";
	m3.showmove();
}

第7题
plorg.h

#ifndef PLORG_H_
#define PLORG_H_

class Plorg
{
private:
	char c_name[20];
	int c_CI;

public:
	Plorg();
	Plorg(const char* name, int ci = 50);
	void reset_CI(int ci);
	void showPlorg() const;
	~Plorg() {};
};

#endif // !PLORG_H_

plorg.cpp

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

Plorg::Plorg()
{
	strcpy_s(c_name, "Plorga");
	c_CI = 50;
}

Plorg::Plorg(const char* name, int ci)
{
	strcpy_s(c_name, name);
	c_CI = ci;
}

void Plorg::reset_CI(int ci)
{
	c_CI = ci;
}

void Plorg::showPlorg() const
{
	std::cout << "Name: " << c_name << std::endl;
	std::cout << "CI:" << c_CI << std::endl;
}

main.cpp

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


int main()
{
	using std::cout;
	using std::cin;
	using std::endl;
	
	Plorg p1;
	p1.showPlorg();
	p1.reset_CI(40);
	p1.showPlorg();

	Plorg p2("xiao ming",66);
	p2.showPlorg();

	char name[20];
	int ci;
	cout << "Enter name: ";
	cin.getline(name, 20);
	cout << "Enter CI: ";
	cin >> ci;
	Plorg p3(name, ci);
	p3.showPlorg();
}

第8题
list.h

#ifndef LIST_H_
#define LIST_H_

struct Student
{
	char fullname[25];
	int age;
	int ID;
};

typedef Student Item;

class List
{
private:
	enum { MAX = 3 };
	Student students[MAX];
	int top;

public:
	List();
	~List() {};
	bool isempty() const;
	bool isfull() const;
	void visit(void (*pf) (Item& item));
	void show() const;
	void add_data(const Item& item);
	void del_data();
};

void ageplus(Item& item);
void IDplus(Item& item);
void showmenu();

#endif // !LIST_H_

list.cpp

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

List::List()
{
	top = 0;
}

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

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

void List::show() const
{
	if (top == 0)
	{
		std::cout << "List is empty.\n";
	}
	else
	{
		std::cout << "List have " << top << " items:\n";
		for (int i = 0; i < top; i++)
		{
			std::cout << "#" << i + 1 << ":\tName: " << students[i].fullname << "\tAge: " << students[i].age << "\tID: " << students[i].ID << std::endl;
		}
	}
}

void List::add_data(const Item& item)
{
	if (top < MAX)
	{
		strcpy_s(students[top].fullname, item.fullname);
		students[top].age = item.age;
		students[top].ID = item.ID;
		top++;
		if (top == MAX)
		{
			std::cout << "List already full now.\n";
		}
	}
	else
	{
		std::cout << "List already full.\n";
	}
}

void List::del_data()
{
	if (top > 0)
	{
		--top;
		strcpy_s(students[top].fullname, "\0");
		students[top].age = 0;
		students[top].ID = 0;
	}
	else
	{
		std::cout << "List is empty.\n";
	}
}

void List::visit(void (*pf) (Item& item))
{
	for (int i = 0; i < top; i++)
	{
		pf(students[i]);
	}
}

void ageplus(Item& item)
{
	item.age += 1;
}

void IDplus(Item& item)
{
	item.ID += 100000;
}

void showmenu()
{
	using std::cout;
	
	cout<<"Enter your choice:\n"
		"a) add data \t d) delete data \n"
		"p) age update \t i) ID update \n"
		"s) show data \t q) quit \n"
		"your choice is: ";
}

main.cpp

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

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

	char ch;
	Student st;
	List l;
	showmenu();
	while (cin >> ch && toupper(ch) != 'Q')
	{
		while (cin.get() != '\n')
		{
			continue;
		}
		if (!isalpha(ch))
		{
			cout << "You must input a letter.\n"
				"Your choice is: ";
			continue;
		}

		switch (ch)
		{
		case 'a':
		case 'A':
			if (l.isfull())
			{
				cout << "List already full.\n";
			}
			else
			{
				cout << "Enter fullname: ";
				cin.getline(st.fullname, 25);
				cout << "Enter age: ";
				cin >> st.age;
				cout << "Enter ID: ";
				cin >> st.ID;
				l.add_data(st);
			}
			break;

		case 'd':
		case 'D':
			l.del_data();
			break;

		case 'i':
		case 'I':
			if (l.isempty())
			{
				cout << "Clould not plus empty list.\n";
			}
			else
			{
				l.visit(IDplus);
				cout << "Every student ID +100000.\n";
			}
			break;

		case 'p':
		case 'P':
			if (l.isempty())
			{
				cout << "Clould not plus empty list.\n";
			}
			else
			{
				l.visit(ageplus);
				cout << "Every student age +1.\n";
			}
			break;

		case 's':
		case 'S':
			l.show();
			break;

		default:
			cout << "Unknown choice.\n";
			break;
		}
		showmenu();
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值