C++ Primer Plus第五版 第10章 编程练习答案

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/19 
From : C++ Primer Plus第五版第10章编程练习 第1题   
*******************************************************************************************************************/
//头文件
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
using namespace std;
class Account
{
private:
	string name;
	string account;
	double savings;
public:
	Account();
	Account(const string &n,const string &a ,const double money);
	void show()const;
	void save(const double money);
	void withdraw(const double money);
	~Account();
};
#endif
//头文件对应的cpp文件
#include<iostream>
#include <string>
#include "Account.h"
Account::Account()
{
	name = " ";
	account = " ";
	savings =0;
}
Account::Account(const string  &n,const string &a ,const double money)
{
	name = n;
	account = a;
	savings = money;
}
Account::~Account()
{
	cout << "Bye.\n";
}

void Account::show()const
{
	cout << "The name is : " << name << endl;
	cout << "The account is : "<< account <<endl;
	cout << "The saving is : " << savings <<endl;
}
void Account::save(const double money)
{
	if (money <0)
	{
		cerr << "The number of saving can't be negative.\n";
	}
	else
		savings+=money;
}

void Account::withdraw(const double money)
{
	if (money > savings)
	{
		cerr << "The number of withdrawing can't be bigger than savings.\n";
	}
	else
		savings-=money;
}
//演示主程序
#include<iostream>
#include "Account.h"
int main()
{
	Account account1("Bob Smith","452164146413541641",7800);
	account1.show();
	account1.save(1200);
	account1.show();
	account1.save(-1200);
	account1.show();
	account1.withdraw(2000);
	account1.show();
	account1.withdraw(20000);
	account1.show();
	system("pause");
	return 0;
}


/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/11/17 
From : C++ Primer Plus第五版第10章编程练习 第2题   
*******************************************************************************************************************/
//头文件
#ifndef PERSON_H_
#define PERSON_H_
using namespace std;
#include<string>
class Person
{
private:
	static const int LIMIT =25;
	string lname;
	char fname[LIMIT];
public:
	Person(){lname="";fname[0]='\0';}
	Person(const string & ln,const char *fn= "Heyyou");
	void show() const;
	void FormalShow()const;
};
#endif
//头文件对应的cpp文件
#include<iostream>
#include<cstring>
#include"Person.h"
using namespace std;
Person::Person(const string & ln,const char *fn)
{
	lname = ln;
	strncpy(fname,fn,LIMIT-1);
	fname[LIMIT-1]='\0';
}

void Person::show() const
{
	cout << "The name is : " << fname << " " << lname <<endl;
}
void Person::FormalShow()const
{
	cout << "The name is : " << lname << " " << fname <<endl;
}
//演示主程序
#include<iostream>
#include"Person.h"
int main()
{
	Person one;
	Person two("Smythcraft");
	Person three("Dimwiddy","Sam");
	one.show();
	cout << endl;
	one.FormalShow();
	cout << endl;

	two.show();
	cout << endl;
	two.FormalShow();
	cout << endl;

	three.show();
	cout << endl;
	three.FormalShow();
	cout << endl;

	system("pause");
	return 0;
}

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/11/17 
From : C++ Primer Plus第五版第10章编程练习 第3题   
*******************************************************************************************************************/
//头文件
#ifndef GOLF_H_
#define GOLF_H_
const int Len = 40; 
class Golf
{
private:
	char fullname[Len];  
	int handicap; 
public:
	Golf(const char *name, int hc); 
	Golf();  
	void set_handicap(int hc);  
	void showgolf()const; 
};

#endif
//头文件对应的cpp文件
#include "golf.h"
#include<iostream>
#include<cstring>
using namespace std;



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

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

void Golf::showgolf()const
{
	cout << fullname << endl;
	cout << handicap << endl;  
}
//演示主程序
#include <iostream>  
#include "golf.h" 
using namespace std;
int main()  
{  
	Golf Ann( "Ann Birdfree", 24);  
	cout << "The recorded name and handicap are: \n";  
	Ann.showgolf();
	Ann.set_handicap(66);  
	cout << "Your amended name and handicap are: \n";
	Ann.showgolf(); 
	system("pause");
	return 0;  
}  

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/11/17 
From : C++ Primer Plus第五版第10章编程练习 第4题   
*******************************************************************************************************************/
//头文件
#ifndef SALES__
#define SALES__

namespace SALES
{
	const int QUARTERS = 4;
	class Sales
	{
	private:
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	public:
		// default constructor
		Sales();

		// copies the lesser of 4 or n items from the array ar
		// to the sales member and computes and stores the
		// average, maximum, and minimum values of the entered items;
		// remaining elements of sales, if any, set to 0
		Sales(const double ar[], int n);

		// gathers sales for 4 quarters interactively, stores them
		// in the sales member of object and computes and stores the
		// average, maximum, and minumum values
		void setSales();

		// display all information in object
		void showSales();
	};

}

#endif


//头文件对应的cpp文件
#include <iostream>
#include "Sale.h"


namespace SALES
{
	using std::cin;
	using std::cout;
	using std::endl;
	Sales::Sales(const double ar[], int n)
	{
		if (n < 0)
			n = 0;
		int limit = n < QUARTERS ? n : QUARTERS;
		double total = 0;
		min = 0;
		max = 0;
		average = 0;
		if (limit > 0)
			min = max = ar[0];
		int i;
		for (i = 0; i < limit; i++)
		{
			sales[i] = ar[i];
			total += ar[i];
			if (ar[i] > max)
				max = ar[i];
			else if (ar[i] < min)
				min = ar[i];
		}
		for (i = limit; i < QUARTERS; i++)
			sales[i] = 0;
		if (limit > 0)
			average = total / limit;
	}

	Sales::Sales()
	{

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

	void Sales::setSales()
	{
		double sa[QUARTERS];
		int i;
		for (i = 0; i < QUARTERS; i++)
		{
			cout << "Enter sales for quarter " << i + 1 << ": ";
			cin >> sa[i];
		}

		// create temporary object, copy to invoking object
		*this = Sales(sa, QUARTERS);
	}

	void Sales::showSales()
	{
		cout << "Sales:\n";
		for (int i = 0; i < QUARTERS; i++)
			cout << "Quarter " << i + 1 << ": $"
			<< sales[i] << endl;
		cout << "Average: $" << average << endl;
		cout << "Minimum: $" << min << endl;
		cout << "Maximum: $" << max << endl;
	}
}

//演示主程序
#include<iostream>
using namespace std;
int main()
{

	system("pause");
	return 0;
}
#include <iostream>
#include "Sale.h"

int main()
{
	using SALES::Sales;

	double vals[3] = {2000, 3000, 5000};
	Sales forFiji(vals, 3);
	forFiji.showSales();

	Sales red;
	red.showSales();
	red.setSales();
	red.showSales();
	system("pause");
	return 0;
}

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/11/17 
From : C++ Primer Plus第五版第10章编程练习 第5题   
*******************************************************************************************************************/
//头文件
#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
//头文件对应的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;

}
	
//演示主程序
#include <iostream>
#include <cctype>
#include "Stack.h"    
void get_customer(customer & cu);
int main(void)
{
	using namespace std;
	Stack st; // create a stack of customer structures
	customer temp;
	double payments = 0;
	char c;

	cout << "Please enter A to add a customer,\n"
		<< "P to process a customer, and Q to quit.\n";
	while (cin >> c && (c = toupper(c)) != 'Q')
	{
		while (cin.get() != '\n')
			continue;
		if (c != 'A' && c != 'P')
		{
			cout << "Please respond with A, P, or Q: ";
			continue;
		}
		switch (c)
		{
		case 'A'    :    if (st.isfull())
							 cout << "stack already full\n";
						 else
						 {
							 get_customer(temp);
							 st.push(temp);
						 }
						 break;
		case 'P'    :    if (st.isempty())
							 cout << "stack already empty\n";
						 else {
							 st.pop(temp);
							 payments += temp.payment;
							 cout << temp.fullname << " processed. ";
							 cout << "Payments now total $"
								 << payments << "\n";
						 }
						 break;
		default    :    cout << "Whoops! Programming error!\n";
		}
		cout << "Please enter A to add a customer,\n"
			<< "P to process a customer, and Q to quit.\n";
	}
	cout << "Done!\n";
	system("pause");
	return 0;
}

void get_customer(customer & cu)
{
	using namespace std;
	cout << "Enter customer name: ";
	cin.getline(cu.fullname,35);
	cout << "Enter customer payment: ";
	cin >> cu.payment;
	while (cin.get() != '\n')
		continue;
}

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/11/17 
From : C++ Primer Plus第五版第10章编程练习 第6题   
*******************************************************************************************************************/
//头文件
#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
//头文件对应的cpp文件
#include <iostream>
#include "Move.h"

Move::Move (double a, double b)
{
	x = a;
	y = b;
}
void Move::showmove()const
{
	std::cout << "x: " << x << " ,y: " << y << std::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;
}
//演示主程序
#include <iostream>  
#include "move.h"  

int main()  
{  
	Move a;  
	a.showmove();  
	Move b(3,3);  
	b.showmove();  

	a = a.add(b);  
	a.showmove();  
	a.reset(1,1);  
	a.showmove();  
	system("pause");
	return 0;  
}

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/11/17 
From : C++ Primer Plus第五版第10章编程练习 第7题   
*******************************************************************************************************************/
//头文件
#ifndef PLORG_H_
#define PLORG_H_
class Plorg
{
private:
	enum {MAX = 20};  
	char name[MAX];  
	int CI;
public:
	Plorg(char *p= "Plorga", int n =50);
	void change(const int n);
	void show()const;
};
#endif
//头文件对应的cpp文件
#include <iostream>
#include<cstring>
#include "Plorg.h"
Plorg::Plorg(char *p, int n)
{
	strncpy(name,p,MAX-1);
	name[MAX-1]='\0';
	CI = n;
}
void Plorg::change(const int n)
{
	CI = n;
}
void Plorg::show()const
{
	std::cout << "Name: " << name <<", CI: " << CI <<std::endl;
}
//演示主程序#include <iostream>  
#include "Plorg.h"  

int main()  
{  
	Plorg one;  
	Plorg two("David",66);  

	one.show();  
	one.change(666);  
	one.show();  

	two.show();  
	two.change(888);  
	two.show();  
	system("pause");
	return 0;  
}  

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/11/17 
From : C++ Primer Plus第五版第10章编程练习 第8题   
*******************************************************************************************************************/
//头文件
#ifndef SIMPLEST_
#define SIMPLEST_

// program-specific declarations
const int TSIZE = 45;      // size of array to hold title
struct film
{
	char title[TSIZE];
	int rating;
};

// general type definitions
typedef struct film Item;

const int MAXLIST = 10;
class simplist
{
private:
	Item items[MAXLIST];
	int count;
public:
	simplist(void);
	bool isempty(void);
	bool isfull(void);
	int itemcount();
	bool additem(Item item);
	void transverse( void (*pfun)(Item item));
};

#endif


//头文件对应的cpp文件
#include "simplist.h"

simplist::simplist(void)
{
	count = 0;
}

bool simplist::isempty(void)
{
	return count == 0;
}

bool simplist::isfull(void)
{
	return count == MAXLIST;
}

int simplist::itemcount()
{
	return count;
}
bool simplist::additem(Item item)
{
	if (count == MAXLIST)
		return false;
	else
		items[count++] = item;
	return true;
}

void simplist::transverse( void (*pfun)(Item item))
{
	for (int i = 0; i < count; i++)
		(*pfun)(items[i]);
}

//演示主程序
#include <iostream>
#include <cstdlib>         // prototype for exit()
#include "simplist.h"     // simple list class declaration
// array version
void showmovies(Item item); // to be used by transverse()

int main(void)
{
	using namespace std;
	simplist movies;     // creates an empty list
	Item temp;

	if (movies.isfull())    // invokes isfull() member function
	{
		cout << "No more room in list! Bye!\n";
		exit(1);
	}
	cout << "Enter first movie title:\n";
	while (cin.getline(temp.title,TSIZE) && temp.title[0] != '\0')
	{
		cout << "Enter your rating <0-10>: ";
		cin >> temp.rating;
		while(cin.get() != '\n')
			continue;
		if (movies.additem(temp) == false)
		{
			cout << "List already is full!\n";
			break;
		}
		if (movies.isfull())
		{
			cout << "You have filled the list.\n";
			break;
		}
		cout << "Enter next movie title (empty line to stop):\n";
	}
	if (movies.isempty())
		cout << "No data entered. ";
	else
	{
		cout << "Here is the movie list:\n";
		movies.transverse(showmovies);
	}
	cout << "Bye!\n";
	return 0;
}

void showmovies(Item item)
{
	std::cout << "Movie: " << item.title << "  Rating: "
		<< item.rating << std::endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值