程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> c++ primer plus(第6版)中文版 第十章編程練習答案

c++ primer plus(第6版)中文版 第十章編程練習答案

編輯:C++入門知識

第十章編程練習答案

10.1為復習題5類提供定義,並演示

//10.1為復習題5類提供定義,並演示
#include 
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 
#include 
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 
#include 
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 
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 
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!"<//10.6根據以下類編寫測試
#include 
#include 
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 
#include 
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 
#include 
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;
} 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved