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

//第一题
//count.h
#include 
class Count {
private:
	std::string m_name;
	std::string m_id;
	double m_money;
public:
	Count();
	Count(const std::string name, const std::string id, const double money);
	void disp() const;
	void add(const double money);
	void sub(const double money);
};


//count.cpp
#include 
#include "count.h"

Count::Count()
{
	m_name = "";
	m_id = "";
	m_money = 0;
}

Count::Count(const std::string name, const std::string id, const double money)
{
	m_name = name;
	m_id = id;
	m_money = money;
}

void Count::disp() const
{
	std::cout << "Name: " << m_name << std::endl << "ID: " << m_id << std::endl << "Money: " << m_money << std::endl;
}

void Count::add(const double money)
{
	m_money += money;
}

void Count::sub(const double money)
{
	m_money -= money;
}

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

int main()
{
	Count a("Tree", "33673504", 66666.66);
	a.disp();
	a.add(666.66);
	a.disp();
	a.sub(666.66);
	a.disp();
	return 0;
}



//第二题
//Person.h
#include 

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

//Person.cpp
#include "Person.h"
#include 
#include 

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

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

void Person::FormalShow() const
{
	std::cout << lname << ", " << fname << std::endl;
}

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

int main()
{
	Person one;
	Person two("Smythecraft");
	Person three("Dimwiddy", "Sam");
	one.Show();
	one.FormalShow();
	two.Show();
	two.FormalShow();
	three.Show();
	three.FormalShow();
	return 0;
}


//第三题
//Golf.h
class Golf {
private:
	static const int LEN = 40;
	char m_fullname[LEN];
	int m_handicap;
public:
	Golf();
	Golf(const char *fullname, const int handicap = 0);
	int setGolf();
	void setHandicap(const int handicap);
	void showGolf() const;
};


//Golf.cpp
#include "Golf.h"
#include 
#include 

Golf::Golf()
{
	m_fullname[0] = 0;
	m_handicap = 0;
}

Golf::Golf(const char *fullname, const int handicap)
{
	strcpy_s(m_fullname, fullname);
	m_handicap = handicap;
}

int Golf::setGolf()
{
	char fullname[100] = { 0 };
	int handicap = 0;
	std::cin.getline(fullname, 100);
	std::cin >> handicap;
	Golf g(fullname, handicap);
	*this = g;
	return fullname[0] == 0 ? 0 : 1;
}

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

void Golf::showGolf() const
{
	std::cout << "FullName: " << m_fullname << std::endl << "handicap: " << m_handicap << std::endl;
}

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

int main()
{
	Golf g("Tree", 666);
	g.showGolf();
	g.setGolf();
	g.showGolf();
	g.setHandicap(9999);
	g.showGolf();
	return 0;
}




//第四题
//SALES.h
namespace SALES
{
	class Sales {
	private:
		static const int QUARTERS = 4;
		double m_sales[QUARTERS];
		double m_average;
		double m_max;
		double m_min;
	public:
		Sales();
		Sales(const double *sales, const int n);
		void setSales();
		void showSales() const;
	};
}

//SALES.cpp
#include "SALES.h"
#include 

SALES::Sales::Sales()
{
	m_sales[0] = m_sales[1] = m_sales[2] = m_sales[3] = m_average = m_max = m_min = 0;
}

SALES::Sales::Sales(const double *sales, const int n)
{
	if (n < QUARTERS)
	{
		double max = -1, min = 99999, average = 0;
		for (int i = 0; i < n; ++i)
		{
			m_sales[i] = sales[i];
			max = max < sales[i] ? sales[i] : max;
			min = min > sales[i] ? sales[i] : min;
			average += sales[i];
		}
		for (int i = QUARTERS - n; i > 0; --i)
			m_sales[QUARTERS - i] = 0;
		m_max = max;
		m_min = min;
		m_average = average / n;
	}
	else
	{
		double max = -1, min = 99999, average = 0;
		for (int i = 0; i < QUARTERS; ++i)
		{
			m_sales[i] = sales[i];
			max = max < sales[i] ? sales[i] : max;
			min = min > sales[i] ? sales[i] : min;
			average += sales[i];
		}
		m_max = max;
		m_min = min;
		m_average = average / QUARTERS;
	}
}

void SALES::Sales::setSales()
{
	double max = -1, min = 99999, average = 0;
	for (int i = 0; i < QUARTERS; ++i)
	{
		std::cin >> m_sales[i];
		max = max < m_sales[i] ? m_sales[i] : max;
		min = min > m_sales[i] ? m_sales[i] : min;
		average += m_sales[i];
	}
	m_max = max;
	m_min = min;
	m_average = average / QUARTERS;
}

void SALES::Sales::showSales() const
{
	std::cout << "Array: " << std::endl;
	for (int i = 0; i < QUARTERS; ++i)
		std::cout << m_sales[i] << "\t";
	std::cout << std::endl;
	std::cout << "Max: " << m_max << std::endl;
	std::cout << "Min: " << m_min << std::endl;
	std::cout << "Ave: " << m_average << std::endl;
}

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

int main()
{
	double arr[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
	SALES::Sales s(arr, 3);
	s.showSales();
	s.setSales();
	s.showSales();
	return 0;
}



//第五题
//Stack.h
struct Customer {
	char fullname[35];
	double payment;
};

typedef Customer ElemType;

class Stack
{
private:
	static const int m_SIZE = 100;
	ElemType m_Data[m_SIZE];
	int m_Top;
public:
	Stack() { m_Top = 0; }
	bool Push(const ElemType Data);
	bool Pop(ElemType &Data);
	bool isEmpty() const;
	bool isFull() const;
};

//Stack.cpp
#include "Stack.h"

bool Stack::Push(const ElemType Data)
{
	if (isFull() == true)
		return false;
	m_Data[m_Top++] = Data;
	return true;
}

bool Stack::Pop(ElemType &Data)
{
	if (isEmpty() == true)
		return false;
	Data = m_Data[--m_Top];
	return true;
}

bool Stack::isEmpty() const 
{
	return m_Top == 0 ? true : false;
}

bool Stack::isFull() const
{
	return m_Top == m_SIZE ? true : false;
}

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

int main()
{
	Stack s;
	Customer c1, c2, c3;
	strcpy_s(c1.fullname, "Tree");
	c1.payment = 10;
	strcpy_s(c2.fullname, "Yuuji");
	c2.payment = 20;
	strcpy_s(c3.fullname, "FUCK");
	c3.payment = 30;
	s.Push(c1);
	s.Push(c2);
	s.Push(c3);
	Customer T;
	double sum = 0;
	s.Pop(T);
	sum += T.payment;
	s.Pop(T);
	sum += T.payment;
	std::cout << sum << std::endl;
	return 0;
}



//第六题
//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);
};


//Move.cpp
#include "Move.h"
#include 

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

void Move::showmove() const
{
	std::cout << "(" << x << ", " << y << ")" << std::endl;
}

Move Move::add(const Move &m) const
{
	Move Tmp((*this).x + m.x, (*this).y + m.y);
	return Tmp;
}

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

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

int main()
{
	Move m(2, 3);
	m.showmove();
	Move n(3, 4);
	n.showmove();
	Move t = m.add(n);
	t.showmove();
	return 0;
}



//第七题
//plorg.h
class Plorg
{
private:
	char m_name[20];
	int m_CI;
public:
	Plorg(char *name = "Plorga", int CI = 50);
	void setName(const char *name);
	void setCI(const int CI);
	void disp() const;
};

//plorg.cpp
#include "Plorg.h"
#include 
#include 

Plorg::Plorg(char * name, int CI)
{
	strcpy_s(m_name, name);
	m_CI = CI;
}

void Plorg::setName(const char * name)
{
	strcpy_s(m_name, name);
}

void Plorg::setCI(const int CI)
{
	m_CI = CI;
}

void Plorg::disp() const
{
	std::cout << "Name: " << m_name << std::endl << "CI: " << m_CI << std::endl;
}


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

int main()
{
	Plorg p("Tree", 66);
	p.disp();
	p.setCI(666);
	p.disp();
	p.setName("Yuuji");
	p.disp();
	system("PAUSE");
	return 0;
}


//第八题 
//List.h
typedef int ElemType;

class List
{
private:
	static const int SIZE = 100;
	ElemType m_Data[SIZE];
	int m_Length;
public:
	List() { m_Length = 0; }
	bool addItem(const ElemType Data);
	bool isEmpty() const;
	bool isFull() const;
	void visit(void(*pf)(ElemType &Data));
};

//List.cpp
#include "List.h"

bool List::addItem(const ElemType Data)
{
	if (isFull() == true)
		return false;
	m_Data[m_Length++] = Data;
	return true;
}

bool List::isEmpty() const
{
	return m_Length == 0 ? true : false;
}

bool List::isFull() const
{
	return m_Length == SIZE ? true : false;
}

void List::visit(void(*pf)(ElemType &Data))
{
	for (int i = 0; i < m_Length; ++i)
		(*pf)(m_Data[i]);
}


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

void Print(ElemType &Data);

int main()
{
	List l;
	l.addItem(1);
	l.addItem(2);
	l.addItem(3);
	l.addItem(4);
	std::cout << "isEmpty? " << l.isEmpty() << std::endl;
	std::cout << "isFull? " << l.isFull() << std::endl;
	void(*pf)(ElemType &Data);
	pf = Print;
	l.visit(pf);
	system("PAUSE");
	return 0;
}

void Print(ElemType &Data)
{
	std::cout << Data << std::endl;
}


你可能感兴趣的:(C++,Primer,Plus学习笔记)