logo头像

不忘初心,奋力前行

C++ Primer Plus(第五版)第8章编程题答案

本文于551天之前发表,文中内容可能已经过时,如有问题,请联系我。

1. 编写通常接受一个参数(字符串的地址),并打印该字符串的函数。然而,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值,而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让您能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示该函数是如何工作的。

//C++ Primer Plus Edition 5
//Chapter 8 Homework 1

#include
using namespace std;
void show_ch(char str, int n = 0);
int num = 0;
int main()
{
char name[10] = “zhang”;
cout << “如果第二个参数为0,那么输出(这是第一次调用):” << endl;
show_ch(name);
cout << “输出结束” << endl;
cout << “第二次调用输出结果:” << endl;
show_ch(name, 3);
cout << “第三次调用输出结果:” << endl;
show_ch(name, 9);
cout << “第四次调用输出结果:” << endl;
show_ch(name, 32);
cout << “完成!” << endl;
system(“pause”);
return 0;
}
void show_ch(char
str, int n)
{
++num;
if (n == 0)
{
cout << str << endl;
}
else
for (int i = 0; i < num; i++)
cout << str << endl;
}

2.CandyBar结构包含3个成员。第一个成员存储candy bar的品牌名称;第二个成员存储candy bar的重量(可能有小数);第三个成员存储candy bar的热量(整数)。请编写一个程序,它使用一个这样的函数,即将CandyBar的引用、char指针、double和int作为参数并用最后3个值设置相应的结构成员。最后3个参数的默认值分别为“Millennium Munch”、2.85和350。另外,该程序还包含一个以CandyBar的引用为参数,并显示结构内容的函数,请尽可能的使用const。

//C++ Primer Plus Edition 5
//Chapter 8 Homework 2

#include

#include
using namespace std;
struct CandyBar {
string name;
double weight;
int hot;
};
void set_CandyBar(CandyBar &candy, char* n = “Millennium Munch”, double w = 2.85, int h = 360);
void show_CandyBar(const CandyBar &c);
int main() {
CandyBar candy;
set_CandyBar(candy);
show_CandyBar(candy);
system(“pause”);
return 0;
}

void set_CandyBar(CandyBar &c, char* n, double w, int h)
{
c.name = n;
c.weight = w;
c.hot = h;

}
void show_CandyBar(const CandyBar &c)
{
cout << “Name: “ << c.name << endl;
cout << “Weight: “ << c.weight << endl;
cout << “Hot: “ << c.hot << endl;
}

3.编写一个函数,它接受一个指向string对象的引用作为参数,并将该string对象的内容转换成大写,为此可使用表6.4描述的函数toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数。

//C++ Primer Plus Edition 5
//Chapter 8 Homework 3

#include

#include

#include
using namespace std;

string convert_Big(string &str);
int main() {
string text,up;
cout << “Enter a string (q to quit):” << endl;
getline(cin, text);
while (text != “q” && text!=”Q”)
{
cout << convert_Big(text) << endl;
cout << “Next string (q to quit): “ << endl;
getline(cin, text);

}
system("pause");
return 0;

}
//string可以被看做是字符数组
string convert_Big(string & str)
{
int limit = str.size();
for (int i = 0; i < limit; i++)
{
if(isalpha(str[i]))
str[i] = toupper(str[i]);
}
return str;
}

4.下面是一个程序框架 //省略框架 请提供其中描述的函数和原型,从而完成该程序。注意,应有两个show()函数,每个都使用默认参数。请尽可能的使用const参数。set() 使用new分配足够的空间来存储制定的字符串。这里使用的技术与设计和实现类使用的相似。(可能害必须修改头文件的名称,删除using编译指令,这取决于所用的编译器。)

//C++ Primer Plus Edition 5
//Chapter 8 Homework 4

#include
using namespace std;

#include

struct stringy {
char str;
int ct;
};
void show(const char
st, int n = 1);
void show(const stringy &strc, int n = 1);
void set(stringy& st, const char *t);

int main() {
stringy beany;
char testing[] = “Reality isn’t what it used to be.”;
set(beany, testing);
show(beany);
show(beany, 2);
testing[0] = ‘D’;
testing[1] = ‘u’;
show(testing);
show(testing, 3);
show(“Done!”);
system(“pause”);
return 0;
}

void set(stringy &st, const char *n)
{
st.ct = strlen(n) + 1;
st.str = new char[st.ct];
strcpy(st.str, n);
}
void show(const stringy &strc, int n)
{
for (int i = 0; i < n; i++)
cout << strc.str << endl;
}

void show(const char *st, int n)
{
for (int i = 0; i < n; i++) {
if (strcmp(st, “Done!”) == 0)
{
cout << “Done!” << endl;
break;
}
cout << st << endl;

}

}

5.编写模板函数max5(),它将一个包含5个T类型元素的数组作为参数,并返回数组中最大的元素(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该函数,将T替换为一个包含5个int值的数组和一个包含5个double值的数组,以测试该函数。

//C++ Primer Plus Edition 5
//Chapter 8 Homework 5

#include
using namespace std;
template
T max5(T *a);
int main() {
int inte[5] = { 23, 55, 65, 12, 34 };
int max_int = max5(inte);
double dou[5] = { 12.3,14.3,11.9,89.6,1.8 };
double max_dou = max5(dou);
cout << “Integer MAX= “ << max_int << endl;
cout << “Double MAX= “ << max_dou << endl;
system(“pause”);
return 0;
}

template
T max5(T *a)
{
T temp = a[0];
for (int i = 1; i < 5; i++)
{
if (a[i] < temp)
temp = a[i];
}
return temp;
}

6.编写模板函数maxn(),它将由一个T类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组中最大的元素。在程序对它进行测试,该程序使用一个包含6个int元素的数组和一个包含4个double元素的数组来调用该函数。程序还包含一个具体化,它将char指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用由5个字符串指针组成的数组来测试该具体化。

//C++ Primer Plus Edition 5
//Chapter 8 Homework 6

#include
using namespace std;
template
T maxn(T a, const int n);
template<> const char
maxn(const char **a, const int n);

int main()
{
int a[6] = {3,6,1,8,2,9 };
double b[4] = { 9.8,8.56,193.1,4.3 };
const char *c[5] = { “HAHAHA”, “BALABALA”, “XIAOMOXIAN”, “hengli”, “very good” };
cout << maxn(a, 6) << endl;
cout << maxn(b, 4) << endl;
cout << maxn(c, 5) << endl;
system(“pause”);
return 0;
}

template
T maxn(T *a, const int n)
{
for (int i = 0; i < n; i++)
if (a[i] > a[0]) a[0] = a[i];
return a[0];
}

template<> const char maxn(const char *a, const int n)
{
int i, j = 0;
for (i = 0; i<n; i++)
if (strlen(a[i])>strlen(a[j])) j = i;
return a[j];

}

7.修改程序清单8.14,使其使用两个名为SumArray()的模板函数来返回数组元素的总和,而不是显示数组的内容。程序应显示thing的总和以及所有debt的总和。

//C++ Primer Plus Edition 5
//Chapter 8 Homework 7

#include
using namespace std;
template
T ShowArray(T arr[], int n);

template
T ShowArray(T arr[], int n);
struct debts {
char name[50];
double amount;
};
int main(void) {
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 << “Listing Mr.E’s counts of things:”;
int ans_int = ShowArray(things, 6);
cout << ans_int << endl;
cout << “Listing Mr.E’s debts:”;
double ans_dou= ShowArray(pd, 3);
cout << ans_dou << endl;
system(“pause”);
return 0;
}

template

T ShowArray(T arr[], int n) {
T ans = arr[0];
cout << “Template A\\n”;
for (int i = 1; i < n; i++)
ans+=arr[i];
return ans;
}

template
T ShowArray(Tarr[], int n)
{
T ans=
arr[0];
cout << “Template B\\n”;
for (int i = 1; i < n; i++)
ans += *arr[i];
return ans;
}

支付宝打赏 微信打赏 QQ钱包打赏

感觉不错?欢迎给我 打个赏~我将不胜感激!