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

/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/3/11
* 描述:
************************************************* */
#include <iostream>
#include <string>
using namespace std;void print(const string str, int n=0);int main()
{cout << "Enter a word.\n";string str;if (cin >> str){print(str); //输出一遍print(str, 1);    //一遍print(str);     //一遍print(str, 4);  //三遍print(str, 4);  //四遍}return 0;
}
void print(const string str, int n)
{static int flag=0;    //static 记录此函数调用了几次。if (n == 0)cout << str << endl;else{for (int i = 0;i < flag;i++)cout << str;cout << endl;}flag++; //要放在else后面。
}

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

/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/3/11
* 描述:
************************************************* */
#include <iostream>
#include <string>
using namespace std;struct CandyBar
{string band;double weight;int calorie;
};CandyBar& set(CandyBar& candy, const char ar[]="Mellennium Munch", const double num1=2.85,const int num2=350);//设定默认值,返回引用
void print(const CandyBar& candy);int main()
{CandyBar candy1;set(candy1);print(candy1);set(candy1, "Mars", 3.7, 100);print(candy1);return 0;
}
CandyBar& set(CandyBar& candy, const char ar[], const double num1, const int num2)
{candy.band = ar;candy.weight = num1;candy.calorie = num2;return candy;  //返回传入的引用值
}
void print(const CandyBar& candy)
{cout << "Band: " << candy.band << endl;cout << "Weight: " << candy.weight << endl;cout << "Calorie: " << candy.calorie << endl;
}

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

/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/3/11
* 描述:
************************************************* */
#include <iostream>
#include <string>
#include <cctype>
using namespace std;void toup(string& str);int main()
{cout << "Enter a string (q to quit): \n";string str;getline(cin, str);while (str != "q"){toup(str);cout << "Next string (q to quit): \n";getline(cin, str);}cout << "Bye.\n";return 0;
}
void toup(string& str)
{char temp;for (int i = 0;i < str.length();i++){temp = str[i];str[i]=toupper(temp); //使用cctype里的toupper函数}cout << str << endl;
}

4.下面是一个程序框架:

#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{char *str;int ct;
};
int main()
{stringy beany;char testing[]="Reality isn't what it used tobe.";set(beany,testing);show(beany);show(beany,2);testing[0]='D';testing[1]='u';show(testing);show(testing,3);show("Done! ");return 0;
}

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

/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/3/11
* 描述:
************************************************* */
#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{char* str;int ct;
};void set(stringy& stry, const char* ar);
void show(const stringy& stry,int times=1);
void show(const char* ar, int times = 1);
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! ");return 0;
}
void set(stringy& stry, const char* ar)
{stry.ct = strlen(ar)+1;stry.str = new char[stry.ct];strcpy_s(stry.str, stry.ct, ar);//采用三个参数的版本,否则不支持这么做
}
void show(const stringy& stry,int times)
{for (int i = 0;i < times;i++){cout << stry.str << endl;cout << "length: " << stry.ct << endl;}cout << endl;
}
void show(const char* ar, int times)
{for (int i = 0;i < times;i++)cout << ar << endl;cout << endl;
}

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

/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/3/11
* 描述:
************************************************* */
#include <iostream>
#include <cstring>
using namespace std;
template<class T>
T max5(const T* ar,int arsize=5);
int main()
{int num1[5] = { 1,3,4,6,8 };double num2[5] = { 12.3,21.3,43.5,34.5,14.1 };cout << max5(num1) << endl;cout << max5(num2) << endl;return 0;
}
template<class T>
T max5(const T* ar, int arsize)
{T temp=ar[0];for (int i = 1;i < 5;i++)if (ar[i] > temp)temp = ar[i];return temp;
}

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

/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/3/11
* 描述:
************************************************* */
#include <iostream>
#include <cstring>
using namespace std;template<typename T>
T maxn(T* ar, const int num)
{T temp = ar[0];for (int i = 0;i < num;i++)if (ar[i] > temp)temp = ar[i];return temp;
}
template<>
const char* maxn(const char* ar[], const int n)//挖个坑,此处输入参数与返回参数不加const 就永远输出最后一个ar[],还没想清楚原因。
{int flag = 0;unsigned int length = 0; for (int i = 0;i < n;i++){if (strlen(ar[i]) > length){length = strlen(ar[i]);flag = i;}}return ar[flag];
}
int main()
{int num1[6] = { 1,23,45,32,43,54 };double num2[4] = { 1.2,3.2,5.3,4.3 };cout << "The biggest number is " << maxn(num1, 6) << "."<<endl;cout << "The biggest number is " << maxn(num2, 4) << "."<< endl;const char* arr[] = { "a","bb","cccccc","wwww","dddddd" };const char* pt = maxn(arr, 5);cout << "The biggest number is " << pt << "." << endl;cout << "Address: " << &pt << endl;return 0;
}

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

/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/3/11
* 描述:
************************************************* */
#include <iostream>template <typename T>
T SumArray(T arr[], int n);template <typename T>
T SumArray(T* arr[], int n);struct debts
{char name[50];double amount;
};int main()
{using namespace std;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;int sum1 = SumArray(things, 6);cout << "The sum of things is " << sum1 << endl;double sum2 = SumArray(pd, 3);cout << "The sum of the debts is " << sum2 << endl;system("pause");return 0;
}template<typename T>
T SumArray(T arr[], int n)
{T sum = 0;for (int i = 0;i < n;i++){sum += arr[i];}return sum;
}
template<typename T>
T SumArray(T* arr[], int n)
{T sum = 0;for (int i = 0;i < n;i++){sum += *arr[i];}return sum;
}

C++ Primer Plus (第六版)编程练习记录(chapter8 函数探幽)相关推荐

  1. C Primer Plus 第六版编程练习第五章答案

    1,编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间.使用#define或const创建一个表示60的符号常量或const变量.通过while循环让用户重复输入值,直到用户输入小于或等于0 ...

  2. C++ Primer Plus 第六版编程练习——第6章

    ★★★★★备注★★★★★ 使用的编译环境为 Visual Studio 2017 默认省略了如下内容: #include "stdafx.h"                    ...

  3. C Primer Plus 第六版---编程练习2

    1.编写一个程序,调用一次 printf()函数,把你的姓名打印在一行.再调用一次 printf()函数,把你的姓名分别打印在两行.然后,再调用两次printf()函数,把你的姓名打印在一行.输出应如 ...

  4. C Primer Plus 第六版---编程练习4

    1.编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印出来. /*输入名和姓打印"名,姓" */ #include<stdio.h> #d ...

  5. C Primer Plus 第六版编程练习第七章答案

    1,编写一个程序读取输入,读到#字符停止,然后报告读取空格数,换行符数目以及所有的其它字符数目. /*7.12*/ #include<stdio.h> #define STOP '#' # ...

  6. C Primer Plus 第六版 编程练习第四章答案 最新出炉

    文章目录 1,编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印出来. 2,编写一个程序,提示用户输入名字,并执行以下操作: 3,编写一个程序,读取一个浮点数,首先以小数 ...

  7. C++ Primer Plus第六版第六章编程练习 第4题, 加入Benevolent Order of Programmer后,在BOP大会上

    /*************************************************************************************************** ...

  8. C++ Primer Plus 第六版 所有章节课后编程练习答案

    我的独立博客地址:www.blog4jimmy.com,欢迎大家关注 下面的是C++ Primer Plus 第六版所有章节的课后编程练习的答案,都是博主自己写的,有不对的地方请大家留言指出讨论讨论 ...

  9. C Primer Plus第六版(中文版)编程练习答案(完美修订版)汇总

    //本文是博主编写的C Primer Plus第六版(中文版)编程练习答案的所有链接; //使用超链接汇总于此,若是有用请点赞收藏并分享给他人; C Primer Plus 第六版(中文版)第二章(完 ...

  10. 深夜里学妹竟然问我会不会C?我直接把这篇文章甩她脸上(C Primer Plus 第六版基础整合)

    C Primer Plus 第六版 前言 第一章 初识C语言 一.C语言的起源 二.C语言的应用 三.C语言的特点 四.编译的过程 五.编码机制 1.简述 2.完成机制 六.在UNIX系统上使用C 七 ...

最新文章

  1. Linux文件分割与合并:splitcat(转载)
  2. Android自适应屏幕大小和布局
  3. oracle 用户 多个表空间
  4. 要让机器人切土豆丝,英伟达首先给土豆建了个模
  5. 设置网页打开默认全屏_提升Adsense收入的三个关键设置
  6. ArrayList 和 LinkedList 的自定义实现
  7. [Luogu P2014]选课 (树形DP)
  8. 博客开源系统(待续......)
  9. 真·抬头发票!| 今日最佳
  10. 在计算机硬件中mo是指,计算机导论 - [课件]第2章 计算机系统的硬件.ppt
  11. 128位计算机 ps2,64位就最强?为啥没有128位电脑?
  12. vgh电压高了有什么_一文告诉你电压互感器的作用是什么?
  13. 洛谷P3389 【模板】高斯消元法
  14. ie8 的断字/断行 bug
  15. js中 new Date()使用说明
  16. 亲戚的孩子说要寄养在我家,上学方便,怎么明确的拒绝?
  17. python中的exec()函数和eval()函数
  18. esayexcel导出动态表头数据
  19. IAR8.3 STM8安装过程
  20. Shapley Values

热门文章

  1. 内置 DSP,回音消除,噪音抑制全双工通话芯片—ATH8809
  2. 在线去雾开发,使用阿贝云服务器
  3. 元旦节前后,Python兼职接单的小高潮来了
  4. tp5.1 集成支付宝支付方法(二)
  5. Date_Calendar_SimpleDateFormat_大浮点数和大整数
  6. 【CSDN】MD编辑器使用指南
  7. 三分钟带你了解物联网的发展史
  8. 看片显示服务器不稳定,无限“看片”,资源随便下,这回爽了!
  9. 计算机视觉中transformer的理解
  10. Android 开发 设置banner圆角,滑动时,图片圆角失效