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

2014-11-24 12:43:34 · 作者: · 浏览: 0

第八章编程练习答案

8.1编写一个输出字符串的函数,有一个默认参数表示输出次数,默认为1.(原题太扯啦,题意基础上小改动)

//8.1编写一个输出字符串的函数,有一个默认参数表示输出次数,默认为1.(原题太扯啦,题意基础上小改动)
#include 
  
   
using namespace std;

void show (const char* str, int time=1)
{
	unsigned	n=(time>0) time:-time;
	for (unsigned i = 0; i < n; ++i) 
		cout << str << endl;
	cout << "-----------------" << endl;
}

int main () 
{     
	const char* str = "hello word!";
	show(str);
	show(str,0);
	show(str,1);
	show(str,-1);
} 
  

8.2CandyBar结构体有品牌,重量和热量3个成员,编写一个含有默认值的函数为成员赋值和一个显示内容函数

//8.2CandyBar结构体有品牌,重量和热量3个成员,编写一个含有默认值的函数为成员赋值和一个显示内容函数
//CandyBar& candbar, const char* strBrand = "Millennium Munch", double weight = 2.85, int calories = 350
#include 
  
   
using namespace std;

struct CandyBar
{
	string Brand;
	double weight;
	int calories;
};

void input (CandyBar& candbar, const char* Brand = "Millennium Munch", double weight = 2.85, int calories = 350)
{
	candbar.Brand = Brand;
	candbar.weight = weight;
	candbar.calories = calories;
}

void show (const CandyBar& candbar)
{
	cout << candbar.Brand << '\t' << candbar.weight << '\t' << candbar.calories << endl;
}

int main () 
{     
	CandyBar	candbar1, candbar2;
	input(candbar1);
	show(candbar1);
	input(candbar2, "world", 2,3 );
	show(candbar2);
} 
  

8.3编写一个函数,接受string引用,并把string内容变大写,之后利用循环测试

//8.3编写一个函数,接受string引用,并把string内容变大写,之后利用循环测试
#include 
  
   
#include 
   
     using namespace std; string& upper (string& str) { for (char& e : str) e = (char)toupper(e); return (str); } int main () { while (true) { cout << "Enter a string (q to quit): "; string str; getline(cin, str); if (!cin || "q" == str || "Q" == str) break; cout << upper(str) << endl; } } 
   
  

8.4按以下程序框架,完成函数

//8.4按以下程序框架,完成函数
#include 
  
   
#include 
   
     using namespace std; struct stringy { char * str; int ct; }; void set (stringy& stry, const char* szTxt) { stry.ct = (int)strlen(szTxt); stry.str = new char [stry.ct + 1]; strcpy(stry.str, szTxt); } void show (const char* szTxt, unsigned times = 1) { for (unsigned i = 0; i < times; ++i) cout << szTxt << endl; } void show (const stringy& stry, unsigned times = 1) { for (unsigned i = 0; i < times; ++i) cout << stry.str << endl; } void destroy (stringy& stry) { delete [] stry.str; stry.str = NULL; } int main() { stringy beany; char testing[] = "Reality isn't what it used to be."; set(beany, testing); show(beany); show(beany, 2); destroy(beany); testing[0] = 'D'; testing[1] = 'u'; show(testing); show(testing, 3); show("Done!"); }
   
  

8.5利用一个模板函数max5(),返回给予的5个数的最大值(应用c++11的array)

//8.5利用一个模板函数max5(),返回给予的5个数的最大值(应用c++11的array)
#include 
  
   
#include 
   
     using namespace std; template
    
      const T& max5 (const array
     
      & arr) { unsigned max = 0; for (unsigned i = 0; i < 5; ++i) if (arr[i] > arr[max]) max = i; return (arr[max]); } int main () { array
      
        iarray = {32, 20, 99, 256, 9}; for (const auto& e : iarray) cout << e << ' '; cout << " max: " << max5(iarray) << endl; array
       
         darray = {1.2,2.3,4.5,32.3,4.3}; for (const auto& e : darray) cout << e << ' '; cout << "max: " << max5(darray) << endl; }
       
      
     
    
   
  

8.6编写模板函数maxn(),参数为一个数组和元素数目,返回数组最大值,然后具体化一个字符串为元素的字符串,返回最长字符串

//8.6编写模板函数maxn(),参数为一个数组和元素数目,返回数组最大值,
//然后具体化一个字符串为元素的字符串,返回最长字符串
#include 
  
   
#include 
   
     using namespace std; template 
    
      const T& maxn (const T arr[], unsigned n) { unsigned max = 0; for (unsigned i = 0; i < n; ++i) if (arr[i] > arr[max]) max = i; return (arr[max]); } char* maxn ( char* arr[], unsigned n) { unsigned max = 0; for (unsigned i = 0; i < n; ++i) if (strlen(arr[i]) > strlen(arr[max])) max = i; return (arr[max]); } int main () { int iArray[] = {32, -1, 99, 0, 256, 9}; for (const auto& e : iArray) cout << e << ' '; cout << " ----max: " << maxn(iArray, sizeof(iArray)/sizeof(iArray[0])) << endl; double dArray[] = {-3.2, 221.22, 9.9, 0, 1}; for (const auto& e : dArray) cout << e << ' '; cout << " ----max: " << maxn(dArray, sizeof(dArray)/sizeof(dArray[0])) << endl; const char* szArray[] = {"aaaa","bbbbbb","cc","fffffffffff","kkkk"}; for (const auto& e : szArray) cout << '\"' << e << '\"' << ' '; cout << " ----max: " << '\"' << maxn(szArray, sizeof(szArray)/sizeof(szArray[0])) << '\"' << endl; }
    
   
  

8.7对程序清单8.14,使用两个SumArray()模板返回数组元素总和。程序返回thing以及debt的总和

//8.7对程序清单8.14,使用两个SumArray()模板返回数组元素总和。程序返回thing以及debt的总和
#include 
  
   
using namespace std;

struct debts
{
	char name[50];
	double amount;
};

template 
   
     double SumArray(T arr[], int n) { cout << "template A\n"; double sum = 0; for (int i = 0; i < n; i++) sum += arr[i]; return (sum); } template 
    
      double SumArray(T * arr[], int n) { cout << "template B\n"; double sum = 0; for (int i = 0; i < n; i++) sum += *arr[i]; return (sum); } int main() { int things[6] = {13, 31, 103, 301, 310, 130}; struct debts mr_E[3] = { {"Ima Wolfe", 2400.0},{"Ura Foxe", 1300.0},{"Iby Stout", 1800.0} }; double * pd[3]; for (int i = 0; i < 3; i++) pd[i] = &mr_E[i].amount; cout << "the total number of Mr. E's things: " << SumArray(things, 6) << endl; cout << "the sum of Mr. E's all debts: " << SumArray(pd, 3) << endl; }