zoukankan      html  css  js  c++  java
  • c++ primer plus(文章6版本)中国版 编程练习答案第八章

    编程练习答案第八章

    8.1写输出字符串的函数,存在默认参数表示输出频率,莫感觉1.(原标题太扯了,的问题的细微变化的基础上,含义)

    //8.1编写一个输出字符串的函数。有一个默认參数表示输出次数,默觉得1.(原题太扯啦,题意基础上小修改)
    #include <iostream>
    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 <iostream>
    #include <string>
    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 << '	' << candbar.weight << '	' << 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 <iostream>
    #include <cctype>
    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 <iostream>
    #include <cstring>
    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 <iostream>
    #include <array>
    using namespace std;
    
    template<typename T>
    const T& max5 (const array<T, 5>& arr)
    {
    	unsigned	max = 0;
    	for (unsigned i = 0; i < 5; ++i)
    		if (arr[i] > arr[max])
    			max = i;
    	return (arr[max]);
    }
    
    int main ()
    {
    	array<int, 5>	iarray = {32, 20, 99, 256, 9};
    	for (const auto& e : iarray)
    		cout << e << ' ';
    	cout << " max: " << max5(iarray) << endl;
    	
    	array<double,5>  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 <iostream>
    #include <cstring>
    using namespace std;
    
    template <typename T>
    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 <iostream>
    using namespace std;
    
    struct debts
    {
    	char name[50];
    	double amount;
    };
    
    template <typename T>
    double SumArray(T arr[], int n)
    {	
    	cout << "template A
    ";
    	double	sum = 0;
    	for (int i = 0; i < n; i++)
    		sum += arr[i];
    	return (sum);
    }
    
    template <typename T>
    double SumArray(T * arr[], int n)
    {
    	cout << "template B
    ";
    	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;
    }

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    选择排序--简单(c语言)
    冒泡排序--简单(c++)
    冒泡排序--简单(c语言)
    直接插入排序--简单(c++)
    Microsoft Azure File 服务简介
    【 D3.js 选择集与数据详解 — 1 】 使用datum()绑定数据
    物联网操作系统HelloX V1.77(beta)版本发布
    对XX证券报关于物联网操作系统的几个问题的答复
    使用 Azure Site Recovery 将内部部署虚拟化工作负荷迁移至 Azure
    使用 Azure Site Recovery 灾难恢复至 Azure 的功能现已正式发布
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4646607.html
Copyright © 2011-2022 走看看