C++ premier plus 第六版 编程练习解答(第十章)

10.2

//1.person.h
#ifndef PERSON_H_
#define PERSON_H_

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

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

using namespace std;

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

void Person::Show() const
{
	cout << "The name is: " << frame << " " << lname << endl;
}

void Person::FormalShow() const
{
	cout << "The formal name is: " << lname << " " << fname << endl;
}
//main.cpp
#include <iostream>
#include <string>
#include "person.h"

using namespace std;

int main()
{
	Person one;
	Person two("symthecraft");
	Person three("Dimwiddy", "sum");
	cout << "The person one: \n";
	one.Show();
	one.FormalShow();
	cout << endl;
	cout << "The person two: \n";
	two.Show();
	two.FormalShow();
	cout << endl;
	cout << "The person three: \n";
	three.Show();
	three.FormalShow();
	return 0;
}

10.3

//golf.h
#ifndef GOLF_H_
#define GOLF_H_

class Golf
{
private:
	static const int Len = 40;
	char fullname[Len];
	int handicap;
public:
	Golf(const char * name, int hc);
	int setgolf();
	void sethandicap(int hc);
	void showgolf() const;
};

#endif
//golf.cpp
#include <iostream>
#include <cstring>
#include "golf.h"

using namespace std;

Golf::Golf(const char * name, int hc)
{
	strcpy(fullname, name);
	handicap = hc;
}

int Golf::setgolf()
{
	cin.get();
	cout << "Enter the fullname: ";
	cin.getline(fullname, Len);
	if(strlen(fullname) == 0)
		return 0;
	cout << "Enter the handicap: ";
	cin >> handicap;
	*this = Golf(fullname, handicap);
	return 1;
}

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

void Golf::showgolf() const
{
	cout << "The fullname: " << fullname << endl;
	cout << "The handicap: " << handicap << endl;
}
//main.cpp
#include <iostream>
#include "golf"

using namespace std;

int main()
{
	int i = 0;
	int n;
	cout << "How many members: ";
	cin >> n;
	while(i < n && ann.setgolf())
	{
		ann.showgolf();
		i++;
	}
	cout << "Done!" << endl;
	return 0;
}

10.4

//namesp.h
#ifndef NAMESP_H_
#define NAMESP_H_

namespace SALES
{
	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);
		void setSales();
		void showSales() const;
	};
}

#endif
//namesp.cpp
#include <iostream>
#include "namesp.h"

using namespace std;

namespace SALES
{
	Sales::Sales()
	{
		double sales[QUARTERS] = {0.0};
		double average = 0.0;
		double max = 0.0;
		double min = 0.0;
	}
	Sales:: Sales(const double ar[], int n)
	{
		if(n < QUARTERS)
		{
			for (int i = 0; i < n; ++i)
				sales[i] = ar[i];
			for (int i = 0; i < QUARTERS; ++i)
				sales[i] = 0;
		}
		else
			for(int i = 0; i < QUARTERS; i++)
				sales[i] = ar[i];
		double sum = sales[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;
	}
	void Sales::setSales()
	{
		double sum = 0.0;
		cout << "Enter 4 sales quarters: \n";
		for(int i = 0; i < QUARTERS; i++)
		{
			cout << "# " << i + 1 << ": ";
			cin >> sales[i];
			sum += sales[i];
			if(i == 0)
			{
				max = sales[i];
				min = sales[i];
			}
			else
			{
				max = max > sales[i] ? max : sales[i];
				min = min < sales[i] ? min : sales[i];
			}
		}
		average = sum / QUARTERS;
	}
	void Sales::showSales() const
	{
		cout << "The sales: ";
		for(int i = 0; i < QUARTERS; i++)
			cout << sales[i] << " ";
		cout << endl;
		cout << "The average: " << average << endl;
		cout << "The max: " << max << endl;
		cout << "The min: " << min << endl;
	}
}
//main.cpp
#include <iostream>
#include "namesp.h"

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

	Sales s1;
	cout << "The first: \n";
	s1.setSales();
	s1.showSales();
	cout <<endl;
	double ar1[] = {5,4,8,6,1};
	double ar2[] = {4,7,6};
	cout << "The second: \n";
	Sales s2(ar1, 5);
	s2.showSales();
	cout endl;
	cout << "The third: \n";
	Sales s3(ar2,3);
	s3.showSales();
	return 0;
}

10.5

//stack.h
#ifndef STACK_H_
#define STACK_H_

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

#endif
//stack.cpp
#include "stack.h"
Stack::Stack()
{
	top = 0;
}

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

bool Stack:: isfull() const
{
	turn 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;
}
//main.cpp
#include <iostream>
#include <cctype>
#include "stack.h"

int main()
{
	using namespace std;
	Stack st;
	char ch;
	double total;
	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;
		}
		swich(ch)
		{
			case 'A':
			case 'a': cout << "Enter a customer to add: \nFullname: ";
					  cin.getline(po.fullname, 35);
					  cout << "Payment: ";
					  cin >> po.payment;
					  cin.get();
					  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);
					  	cout << "PO #" << po.fullname << " ayment: " << po.payment << " popped\n";
					  	total += po.payment;
					  	cout << "The total payment: " << total << endl;
					  }
					  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;
}

10.6

//10.6
//move.h
#ifndef MOVE_H_
#define MOVE_H_

class Move
{
private:
	double x;
	double y;
public:
	Move(double a = 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"

using namespace std;

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

void Move::showmove() const
{
	cout << "X is: " << x << endl;
	cout << "Y is: " << y << endl;
	cout << endl;
}

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"

using namespace std;

int main()
{
	Move num1(12.5, 5.6);
	num1.showmove();
	Move num2(45, 23);
	num1.add(num2).showmove();
	num1.showmove();
	num1.reset();
	num1.showmove();
	return 0;
}

10.7

//plorg.h
#ifndef PLORG_H_
#define PLORG_H_

class Plorg
{
private:
	char name[20];
	int CI;
public:
	Plorg(char * str = "Plorga", int n = 50);
	void setplorg(int n);
	void showplorg() const;
};

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

using namespace std;

Plorg::Plorg(char * str, int n)
{
	strcpy(name, str);
	CI = n;
}

void Plorg::setplorg(int n)
{
	CI = n;
}

void Plorg::showplorg() const
{
	cout << "The plorg name: " << name << endl;
	cout << "The plorg CI: " << CI << endl;
	cout << endl;
}
//main.cpp
#include <iostream>
#include <cstring>
#include "plorg.h"

using namespace std;

int main()
{
	Plorg p1;
	p1.showplorg();
	p1.setplorg(54);
	p1.showplorg();
	Plorg p2("Dissv", 32);
	p2.showplorg();
	char str[20];
	int n;
	cout << "Enter the plorg name: ";
	cin.getline(str, 20);
	cout << "Enter the plorg CI: ";
	cin >> n;
	Plorg p3(str, n);
	p3.showplorg();
	return 0;
}
发布了35 篇原创文章 · 获赞 18 · 访问量 6803

猜你喜欢

转载自blog.csdn.net/acslsr/article/details/104430736