码迷,mamicode.com
首页 > 编程语言 > 详细

c++ primer plus(第6版)中文版 第十章编程练习答案

时间:2014-05-15 04:26:27      阅读:423      评论:0      收藏:0      [点我收藏+]

标签:c++   c++ primer plus   源代码   第十章   答案   

第十章编程练习答案

10.1为复习题5类提供定义,并演示

//10.1为复习题5类提供定义,并演示
#include <iostream>
using namespace std;

class BankAccount
{
	string		m_name;
	string		m_num;
	unsigned	m_balance;	
public:
	BankAccount (string name, string num, unsigned balance)
{
	m_name = name;
	m_num = num;
	m_balance = balance;
}
	void show () const
{
	cout << m_name << endl;
	cout << m_num << endl;
	cout << m_balance << endl;
}
void incDeposit (unsigned amount)
{
	m_balance += amount;
}

void decDeposit (unsigned amount)
{
	if (m_balance <= amount)
		m_balance = 0;
	else
		m_balance -= amount;
}
};

int main () 
{     
	BankAccount bank("Tom", "1", 1024);
	bank.show();
	bank.incDeposit(128);
	bank.show();
	bank.decDeposit(2048);
	bank.show();
} 

10.2根据以下类定义和例子,完成类方法定义

//10.2根据以下类定义和例子,完成类方法定义
#include <iostream>
#include <cstring>
using namespace std;

class Person {
	static const unsigned LIMIT = 25;
	string lname;
	char fname[LIMIT]; 
public:
	Person() {lname = ""; fname[0] = ‘\0‘; } // #1
	Person(const string & ln, const char * fn = "Heyyou");
	// the following methods display lname and fname
	void Show() const; // firstname lastname format
	void FormalShow() const; // lastname, firstname format
};
Person::Person(const string & ln, const char * fn)
{
	lname = ln;
	strcpy(fname, fn);
}
void Person::Show() const
{
	cout << fname << ‘ ‘ << lname << endl;
}
void Person::FormalShow() const
{
	cout << lname << ", " << fname << endl;
}

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

10.3编写golf类,包含姓名和成绩两个数据,并实现调用

//10.3编写golf类,包含姓名和成绩两个数据,并实现调用
#include <iostream>
#include <string>
using namespace std;
const int Len = 40;

class golf
{
	string fullname;
	int handicap;
public:
	golf ():fullname("no"),handicap(0) {}
	golf (const char * name, int hc)
	{
		fullname = name;
		handicap = hc;
	}
	void showgolf()
	{
		cout << fullname << ‘\t‘ << handicap << endl;
	}
};

int main () 
{
	golf golfer;
	golf	golfers("tom",100);
	golfer.showgolf();
	golfers.showgolf();
} 

10.4根据编程练习9.4,用Sales类完成

//10.4根据编程练习9.4,用Sales类完成
#include <iostream>
using namespace std;

class Sales
{
	static const int QUARTERS = 4;
	double sales[QUARTERS];
	double size;
	double average;
	double max;
	double min;
public:	
	Sales (double arr[], int n)
	{
		unsigned	times = n < QUARTERS ? (unsigned)n : QUARTERS;
		for (unsigned i = 0; i < times; ++i) 
			sales[i] = arr[i];
		for (unsigned i = times; i < QUARTERS; ++i)
			sales[i] = 0;
		size = times;
		average = calcAverage();
		max = calcMax();
		min = calcMin();
	}
	void setSales()
	{
		cout << "输入" << QUARTERS << "个销售记录:";
		size = QUARTERS;
		for (unsigned i = 0; i < QUARTERS; ++i) {
			cin >> sales[i];
			if (!cin) {
				size = i;
				break;
			}
		}
		for (unsigned i = (unsigned)size; i < QUARTERS; ++i)
			sales[i] = 0;
		cin.clear();
		average = calcAverage();
		max = calcMax();
		min = calcMin();
	}
	double calcAverage () const
	{
		double	sum = 0;
		for (unsigned i = 0; i < size; ++i)
			sum += sales[i];
		return (sum / size);
	}
    double calcMax () const
	{
		unsigned	idxMax = 0;
		for (unsigned i = 0; i < size; ++i)
			if (sales[i] > sales[idxMax])
				idxMax = i;
		return (sales[idxMax]);
	}
	double calcMin () const
	{
		unsigned	idxMin = 0;
		for (unsigned i = 0; i < size; ++i)
			if (sales[i] < sales[idxMin])
				idxMin = i;
		return (sales[idxMin]);
	}

	void showSales () const
	{
		cout << "sales: ";
		for (const auto& e : sales)
			cout << e << ‘ ‘;
		cout << endl;
		cout << "average: " << average << endl;
		cout << "max: " << max << endl;
		cout << "min: " << min << endl;
	}
};

int main () 
{     
	double	salesLst[] = {12.2, 11.16, 10.61, 16.24, 11.53};
	Sales salesBook(salesLst, sizeof(salesLst)/sizeof(salesLst[0]));
	salesBook.showSales();
} 

10.5编写一个程序,从栈中添加和删除customer结构(包括全名和数量两个成员),每删除一个结构,把数量计入总数。

//10.5编写一个程序,从栈中添加和删除customer结构(包括全名和数量两个成员),每删除一个结构,把数量计入总数。
#include <iostream>
using namespace std;

struct customer{  
    char fullname[35];  
    double payment;  
};  
typedef customer Item;  
class Stack{   
    Item items[10];  
    int top;  
public:  
	Stack() :top(0){  }  
	bool isEmpty() const	{  return top==0;	}  
	bool isFull() const  	{  return top==10; }  
	bool push(const Item &item)  
	{  
		if(isFull())  
		{  
			cout<<"Error !Stack is full!"<<endl;  
			return false;  
		}  
		else  
			items[top++]=item;  
		return true;  
	}  
	bool pop(Item &item)  
	{  
		if(top<=0)  
		{  
			cout<<"Error !Stack is empty"<<endl;  
			return false;  
		}  
		else  
		{  
			item=items[--top];  
			return true;  
		}  
	} 
}; 
int main ()  
	{  
		static int total=0;  
		Stack sc;  
		customer c[5]={  {"I",10},  {"II",20},  {"III",40},  {"IV",50},  {"V",60}  };
		customer s[10];//用于存储pop出来的customer  
		for(int i=0;i<5;i++)  
		{  
			sc.push(c[i]);  
			cout<<"now push "<<c[i].fullname<<"  payment is "<<c[i].payment<<endl;  
		}  
		for(int i=0;i<5;i++)  
		{  
			sc.pop(s[i]);  
			total+=s[i].payment;  
			cout<<"total="<<total<<endl;  
		} 
} 

10.6根据以下类编写测试

//10.6根据以下类编写测试
#include <iostream>
#include <cstring>
using namespace std;

class Move
{
	double	m_x;
	double	m_y;
public:
	Move (double a, double b)
	{
		m_x = a;
		m_y = b;
	}
	void showMove () const
	{
		cout << ‘(‘ << m_x << ", " << m_y << ‘)‘;
	}	
	Move add (const Move& m) const
	{
		return (Move(m.m_x + m_x, m.m_y + m_y));
	}
	void reset (double a, double b)
	{
		m_x = a;
		m_y = b;
	}
};

int main () 
{     
	Move one(12, 4);
	one.showMove();
	cout << endl;
	Move two(1, 1);
	one.add(two).showMove();
	cout << endl;
	one.reset(3,4);
	one.showMove();
	cout << endl;
} 

10.7编写类Plorg和函数实现plorg这些特征:
数据:名称不超过19字符;有满意指数整数CI
操作:新的plorg有名称,CI值50;CI可修改;plorg可报告名称和CI;默认名称为“Plorga”

//10.7编写类Plorg和函数实现plorg这些特征:
//数据:名称不超过19字符;有满意指数整数CI
//操作:新的plorg有名称,CI值50;CI可修改;plorg可报告名称和CI;默认名称为“Plorga”
#include <iostream>
#include <cstring>
using namespace std;

class Plorg
{
	char	_name[20];
	unsigned	CI;
public:
	Plorg () : CI(50) 
	{
		strcpy(_name,"Plorga");
	}
	Plorg (const char* name)
	{
		CI = 50;
		strcpy(_name, name);
		_name[19] = ‘\0‘;
	}	
	void setCI (unsigned ci)
	{
		CI = ci;
	}
	void showPlorg () const
	{
		cout << _name << ‘	‘ << CI << endl;
	}
};

int main () 
{     
	Plorg	plorg;
	plorg.showPlorg();
	plorg.setCI(12);
	plorg.showPlorg();
} 

10.8设计简单列表类:可储存多种类型;可创建空列表;可在列表中添加数据项;可确定列表的空满;可访问每一个项并操作

#include <iostream>
#include <cstring>
using namespace std;

class TList
{
	public:
		typedef	int	Item;

	public:
		TList (const Item arr[] = NULL, unsigned n = 0);
		bool isFull (void) const;
		bool isEmpty (void) const;
		bool append (const Item& item);
		void visit (void (*pf) (Item& item));

	private:
		static const unsigned	mk_capacity = 4;

	private:
		Item		m_content[mk_capacity];
		unsigned	m_size;
		
};
		
TList::TList (const Item arr[], unsigned n) {
	if (NULL == arr) {
		m_size = 0;
		return;
	}

	m_size = mk_capacity < n ? mk_capacity : n;
	for (unsigned i = 0; i < m_size; ++i) {
		m_content[i] = arr[i];
	}
}
		
bool
TList::isFull (void) const
{
	return (mk_capacity == m_size);
}
		
bool
TList::isEmpty (void) const
{
	return (0 == m_size);
}

bool 
TList::append (const Item& item)
{
	if (isFull()) {
		return (false);
	}

	m_content[m_size++] = item;
	return (true);
}
		
void
TList::visit (void (*pf) (Item& item))
{
	for (unsigned i = 0; i < m_size; ++i) {
		pf(m_content[i]);
	}
}

static void
show (TList::Item& item)
{
	cout << item << ‘ ‘;
}

int
main (void) 
{     
	TList	one;
	one.visit(show);
	cout << endl;
	cout << "是否空:" << boolalpha << one.isEmpty();
	cout << endl << "=========" << endl;

	TList::Item	arr[] = {1, 2, 3};
	TList	two(arr, sizeof(arr)/sizeof(arr[0]));
	two.visit(show);
	cout << endl;
	cout << "是否满:" << two.isFull();
	cout << endl;
	cout << "是否空:" << two.isEmpty();
	cout << endl;
	cout << "追加一项:" << two.append(16);
	cout << endl;
	two.visit(show);
	cout << endl;
} 


c++ primer plus(第6版)中文版 第十章编程练习答案,布布扣,bubuko.com

c++ primer plus(第6版)中文版 第十章编程练习答案

标签:c++   c++ primer plus   源代码   第十章   答案   

原文地址:http://blog.csdn.net/gd275313/article/details/25650531

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!