《C++ Primer Plus》(第6版)第8章编程练习
《C++ Primer Plus》(第6版)第8章编程练习
- 《C++ Primer Plus》(第6版)第8章编程练习
- 1. 打印字符串
- 2. CandyBar
- 3. 将string对象的内容转换为大写
- 4. 设置并打印字符串
- 5. max5()
- 6. maxn()
- 7. SumArray()
《C++ Primer Plus》(第6版)第8章编程练习
1. 打印字符串
编写通常接受一个参数(字符串的地址),并打印该字符串的函数。然而,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让您能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示该函数是如何工作的。
代码:
#include <iostream>
using namespace std;const int strLen = 20;void printStr(char *str, int n = 0);int main()
{char str[strLen] = "bocchi the rock";printStr(str);cout << endl;printStr(str);cout << endl;printStr(str, 10);cout << endl;printStr(str, 30);cout << endl;system("pause");return 0;
}void printStr(char *str, int n)
{static int count = 0;count++;while (n >= 0){if (n == 0){cout << str << endl;return;}else{for (int i = 0; i < count; i++){cout << str << endl;}return;}}
}
运行结果:

2. CandyBar
CandyBar结构包含3个成员。第一个成员存储candy bar的品牌名称;第二个成员存储candy bar的重量(可能有小数);第三个成员存储candy bar的热量(整数)。 请编写一个程序,它使用一个这样的函数,即将CandyBar的引用、char指针、double和int作为参数,并用最后3个值设置相应的结构成员,最后3个参数的默认值分别为“Millennijum Munch "、2.85和350。另外,该程序还包含一个以 CandyBar 的引用为参数,并显示结构内容的函数。请尽可能使用const。
代码:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;const int strLen = 20;typedef struct CandyBar
{char brand[strLen];double weight;int calories;
} CandyBar;void fill_CandyBar(CandyBar &candyBar, const char brand[] = "Millennium Munch", const double weight = 2.85, const int calories = 350);
void display_CandyBar(const CandyBar &candyBar);int main()
{CandyBar c1, c2;fill_CandyBar(c1);display_CandyBar(c1);cout << endl;fill_CandyBar(c2, "Margaret", 4.28, 900);display_CandyBar(c2);system("pause");return 0;
}void fill_CandyBar(CandyBar &candyBar, const char brand[], const double weight, const int calories)
{strcpy_s(candyBar.brand, brand);candyBar.weight = weight;candyBar.calories = calories;
}void display_CandyBar(const CandyBar &candyBar)
{cout << "Brand:" << candyBar.brand << endl;cout << "Weight:" << candyBar.weight << endl;cout << "Calories:" << candyBar.calories << endl;
}
运行结果:

3. 将string对象的内容转换为大写
编写一个函数,它接受一个指向string对象的引用作为参数“并将该string对象的内容转换为大写,为此可使用表6.4描述的函数 toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数,该程序的运行情况如下:
Enter a string (q to quit) : go away
GO AWAY
Next string (q to quit) : good grief!
GOOD GRIEF!
Next string (q to quit): q
Bye.
代码:
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;#define QUIT "q"void strUpper(string &str);int main()
{string s;cout << "Enter a string (q to quit): ";getline(cin, s);while (s != QUIT){strUpper(s);cout << s << endl;cout << "Next string (q to quit): ";getline(cin, s);}cout << "Bye!\n";system("pause");return 0;
}void strUpper(string &str)
{for (int i = 0; i < str.size(); i++){str[i] = toupper(str[i]);}
}
运行结果:

4. 设置并打印字符串
下面是一个程序框架:
#include<iostream>using namespace std;#include<cstring> //for strlen(),strcpy()struct stringy {char * str; //points to a stringint ct; //length of string (not couting '\0')};// prototypes for set(), show(), and show() go hereint main()
{
stringy beany;char testing[]="Reality isn't what it used to be.";set(beany,testing); //first argument is a reference,//allocates space to hold copy of testing//sets str member of beany to point to the//new block, copies testing to new block,//and sets ct member of beanyshow(beany); //prints member string onceshow(beany, 2); //prints member string twicetesting[0]= 'D';testing[1] = 'u';show(testing); //prints testing string onceshow(testing, 3); //prints testing string thriceshow("Done!");return 0;}
请提供其中描述的函数和原型,从而完成该程序。注意,应有两个 show ()函数,每个都使用默认参数。请尽可能的使用 const 参数。 set() 使用 new 分配足够的空间来存储定指的字符串。这里使用的技术与设计和实现类使用的相似。(可能还必须修改头文件的名称,删除 using 编译指令,这取决于所用的编译器。)
代码:
#include <iostream>
using namespace std;#include <cstring> //for strlen(),strcpy()struct stringy
{char *str; // points to a stringint ct; // length of string (not counting '\0')
};// prototypes for set(), show(), and show() go here
void set(stringy &beany, const char *testing);
void show(const stringy &sy, int times = 1);
void show(const char *str, int times = 1);int main()
{stringy beany;char testing[] = "Reality isn't what it used to be.";set(beany, testing); // first argument is a reference,// allocates space to hold copy of testing// sets str member of beany to point to the// new block, copies testing to new block,// and sets ct member of beanyshow(beany); // prints member string onceshow(beany, 2); // prints member string twicetesting[0] = 'D';testing[1] = 'u';show(testing); // prints testing string onceshow(testing, 3); // prints testing string thriceshow("Done!");system("pause");return 0;
}void set(stringy &beany, const char *testing)
{beany.ct = strlen(testing) + 1;beany.str = new char[beany.ct];strcpy_s(beany.str, beany.ct, testing);
}
void show(const stringy &sy, int times)
{for (int i = 0; i < times; i++)cout << sy.str << endl;
}
void show(const char *str, int times)
{for (int i = 0; i < times; i++)cout << str << endl;
}
运行结果:

5. max5()
编写模板函数 max5 (),它将一个包含 5 个 T 类型元素的数组作为参数,并返回数组中最大的元素(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该函数,将 T 替换为一个包含 5 个 int 值的数组和一个包含 5 个 double 值的数组,以测试该函数。
代码:
#include <iostream>
using namespace std;const int ArrSize = 5;template <typename T>
T max5(T arr[])
{T t_max = arr[0];for (int i = 1; i < ArrSize; i++){if (arr[i] > t_max)t_max = arr[i];}return t_max;
}int main()
{int arr1[ArrSize] = {1, 6, 3, 2, 5};double arr2[ArrSize] = {1.20, 2.30, 5.2, 1.4, 2.7};cout << "The maximum value in int array is " << max5(arr1) << endl;cout << "The maximum value in double array is " << max5(arr2) << endl;system("pause");return 0;
}
运行结果:

6. maxn()
编写模板函数 maxn (),它将由一个 T 类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组中最大的元素。在程序对它进行测试,该程序使用一个包含 6 个 int 元素的数组和一个包含 4 个 double 元素的数组来调用该函数。程序还包含一个具体化,它将 char 指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用由 5 个字符串指针组成的数组来测试该具体化。
代码:
#include <iostream>
#include <cstring>
using namespace std;#define intSize 6
#define doubleSize 4
#define charSize 5template <typename T>
T maxn(T arr[], int n)
{T t_max = arr[0];for (int i = 1; i < n; i++){if (arr[i] > t_max)t_max = arr[i];}return t_max;
}template <>
char *maxn(char *arr[], int n)
{const char *s = arr[0];for (int i = 1; i < n; i++){if (strlen(arr[i]) > strlen(s))s = arr[i];}return s;
}int main()
{int arr1[intSize] = {1, 6, 3, 2, 5, 10};double arr2[doubleSize] = {2.3, 5.2, 1.4, 2.7};char *arr3[charSize] = {"a", "bb", "ccc", "dddd", "eeeee"};cout << "The maximum value in int array is " << maxn(arr1, intSize) << endl;cout << "The maximum value in double array is " << maxn(arr2, doubleSize) << endl;cout << "The longest string in char array is " << maxn(arr3, charSize) << endl;system("pause");return 0;
}
运行结果:

7. SumArray()
修改程序清单 8.14 ,使其使用两个名为 SumArray ()的模板函数来返回数组元素的总和,而不是显示数组的内容。程序应显示thing的总和以及所有 debt 的总和。
代码:
#include <iostream>
using namespace std;template <typename T> // template A
T SumArray(T arr[], int n);template <typename T> // template B
T SumArray(T *arr[], int n);struct debts
{char name[50];double amount;
};int main()
{int things[6] = {13, 31, 103, 301, 310, 130};struct debts me_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] = &(me_E[i].amount);cout << "Listing Mr.E's counts:" << endl;cout << SumArray(things, 6) << endl;cout << "Listing Mr.E's debts:" << endl;cout << SumArray(pd, 3) << endl;system("pause");return 0;
}template <typename T>
T SumArray(T arr[], int n)
{T sum = 0;cout << "template A\n";for (int i = 0; i < n; i++)sum += arr[i];return sum;
}template <typename T>
T SumArray(T *arr[], int n)
{T sum = 0;cout << "template B\n";for (int i = 0; i < n; i++)sum += *arr[i];return sum;
}
运行结果:

相关文章:
《C++ Primer Plus》(第6版)第8章编程练习
《C Primer Plus》(第6版)第8章编程练习《C Primer Plus》(第6版)第8章编程练习1. 打印字符串2. CandyBar3. 将string对象的内容转换为大写4. 设置并打印字符串5. max5()6. maxn()7. SumArray()《C Primer Plus》(第6版…...
RAD Studio 11.3 Alexandria Crack
RAD Studio 11.3 Alexandria Crack 瞄准最新平台版本-此版本增加了对Android 13和Apple macOS Ventura的官方支持。它还支持Ubuntu 22 LTS和Microsoft Windows Server 2022。 使用生物特征认证-New为FireMonkey移动应用程序提供了新的移动生物特征认证组件。 部署嵌入式InterBa…...
Stm32 iic 协议使用
/* 第1个参数为I2C操作句柄 第2个参数为从机设备地址 第3个参数为从机寄存器地址 第4个参数为从机寄存器地址长度 第5个参数为发送的数据的起始地址 第6个参数为传输数据的大小 第7个参数为操作超时时间 */ HAL_I2C_Mem_Write(&hi2c2,salve_add,0,0,PA_BUFF,sizeof(PA_BUFF…...
Malware Dev 02 - Windows SDDL 后门利用之 SCManager
写在最前 如果你是信息安全爱好者,如果你想考一些证书来提升自己的能力,那么欢迎大家来我的 Discord 频道 Northern Bay。邀请链接在这里: https://discord.gg/9XvvuFq9Wb我拥有 OSCP,OSEP,OSWE,OSED&…...
每日一题29——山峰数组的顶部
符合下列属性的数组 arr 称为 山峰数组(山脉数组) : arr.length > 3 存在 i(0 < i < arr.length - 1)使得: arr[0] < arr[1] < ... arr[i-1] < arr[i] arr[i] > arr[i1] > ... &g…...
Linux- 系统随你玩之--好用到炸裂的系统级监控、诊断工具
文章目录1、前言2、lsof介绍2.1、问题来了: 所有用户都可以采用该命令吗?3、 服务器安装lsof3.1、安装3.2、检查安装是否正常。4、lsof 命令4.1、常用功能选项4.2、输出内容4.2.1 、FD和 TYPE列5、 lsof 命令实操常见用法6 、常用组合命令7、 结语1、前言…...
第十三节 继承
什么是继承? java中提供一个关键字extends,用这个关键字,我们可以让一个类和另一个类建立父子关系。 public class Student extends People{} student为子类(派生类),people为父类(基类或者超类…...
【优化】性能优化Springboot 项目配置内置Tomcat使用Http11AprProtocol(AIO)
Springboot 项目配置内置tomcat使用Http11AprProtocol(AIO) Windows版本 1.下载Springboot对应版本tomcat包 下载地址 Apache Tomcat - Apache Tomcat 9 Software Downloads 找到bin目录下 tcnative-1.dll 文件 2 放到jdk的bin目录下 Linux版本 在Springboot中内嵌的Tomcat默…...
SpringBoot之@ConfigurationProperties、@EnableConfigurationProperties
ConfigurationProperties 这个注解不仅可以为yml某个类注入还可以为第三方bean绑定属性 为yml某个类注入 只要将对应的yml类对象声明实体pojo并交给spring容器管理,再在类上使用ConfigurationProperties绑定对应的类名即可 涉及到两个知识点,这个类对…...
数组一次性删除多条数据
需求描述 最后提交时删除表格中的空行 实现方法 单行删除 - 并不是一次性删除 表格每行的最后设置删除按钮,点击时将当前行的索引传递给方法,splice 删除当前行。 <el-table :data"tableData" class"myTable" border>..…...
相机删除照片如何恢复?一键解决它
相机删除照片如何恢复?喜欢用相机拍照的人,总会在空闲时多拍几张,这使我们相机中会储存大量的、各种各样的照片。等到回家后,在进行删除,并选出比较好的照片。但也很容易就误删了一些好看的照片。碰到这种意外事&#…...
vue3搭建教程(基于webpack+create-vue+ element-plus)
前言使用vue脚手架搭建vuetswebpack项目搭建步骤:下载node 版本可以 12 或者14或者 16.0,此次使用的>16.0版本,vue-cli通过npm i -g vue/cli 升级到了 vue cli v5.0.8建目录,如(vue3Study)用IDE工具打开…...
代码随想录算法训练营第四十二天 | leetcode 1049. 最后一块石头的重量 II,494. 目标和,474.一和零
代码随想录算法训练营第四十二天 | leetcode 1049. 最后一块石头的重量 II,494. 目标和,474.一和零1049. 最后一块石头的重量 II494. 目标和474.一和零1049. 最后一块石头的重量 II 题目: 有一堆石头,每块石头的重量都是正整数。…...
Java8中Lambda表达式之Collection 的常见用法
背景 在java8中引入了Lambda表达式。其实,他就是一个匿名函数。我们经常会用到一些循环遍历,起始完全就可以通过Lambda来简化我们不必要的操作,下面我们来看一下Lambda常用的方法。 准备条件 DataBuilderprivate static class Person {priv…...
SpringCloud系列知识快速复习 -- part 2(Sentinel微服务保护,Seata分布式事务,Redis分布式缓存和多级缓存)
SpringCloud系列知识快速复习 -- part 2(Sentinel微服务保护,Seata分布式事务,Redis分布式缓存和多级缓存Sentinel微服务保护什么是雪崩问题?解决方法服务保护技术对比流量控制簇点链路Sentinel流控模式流控效果热点参数限流隔离和…...
设置CentOS7的时间与网络同步
1.设置时区为北京时间 [rootlocalhost ~]# timedatectl set-timezone Asia/Shanghai 2.查看系统时间 [rootlocalhost ~]# timedatectl Local time: 四 2023-03-02 17:40:41 CST #系统时间 Universal time: 四 2023-03-02 09:40:41 UTC …...
java开发手册之编程规约
文章目录编程规约命名风格常量定义代码格式OOP规约集合处理并发处理控制语句注释规约其它编程规约 命名风格 1.代码中的命名均不能以下划线或者美元符号开始,也不能以下划线或者美元符号结束 例如:_name | name__ | name$ | $name2.代码中的命名严…...
Camera | 5.Linux v4l2架构(基于rk3568)
上一篇我们讲解了如何编写基于V4L2的应用程序编写,本文主要讲解内核中V4L2架构,以及一些最重要的结构体、注册函数。 厂家在实现自己的摄像头控制器驱动时,总体上都遵循这个架构来实现,但是不同厂家、不同型号的SoC,具…...
机房PDU如何挑选?
PDU PDU(Power Distribution Unit,电源分配单元),也就是我们常说的机柜用电源分配插座,PDU是为机柜式安装的电气设备提供电力分配而设计的产品,拥有不同的功能、安装方式和不同插位组合的多种系列规格,能为不同的电源环境提供适合的机架式电源分配解决方案。PDU的应用,…...
lab备考第二步:HCIE-Cloud-Compute-第一题:FusionCompute
第一题 FusionCompute 一、题目介绍 1.1. 扩容CAN节点与对接共享存储(必选) 题目及【考生提醒关键点】 扩容一台CNA节点,配置管理地址设置为:192.168.100.212。密码设置为:Cloud12#$。【输入之前确认自己的大小写是否…...
[2025CVPR]DeepVideo-R1:基于难度感知回归GRPO的视频强化微调框架详解
突破视频大语言模型推理瓶颈,在多个视频基准上实现SOTA性能 一、核心问题与创新亮点 1.1 GRPO在视频任务中的两大挑战 安全措施依赖问题 GRPO使用min和clip函数限制策略更新幅度,导致: 梯度抑制:当新旧策略差异过大时梯度消失收敛困难:策略无法充分优化# 传统GRPO的梯…...
Auto-Coder使用GPT-4o完成:在用TabPFN这个模型构建一个预测未来3天涨跌的分类任务
通过akshare库,获取股票数据,并生成TabPFN这个模型 可以识别、处理的格式,写一个完整的预处理示例,并构建一个预测未来 3 天股价涨跌的分类任务 用TabPFN这个模型构建一个预测未来 3 天股价涨跌的分类任务,进行预测并输…...
《用户共鸣指数(E)驱动品牌大模型种草:如何抢占大模型搜索结果情感高地》
在注意力分散、内容高度同质化的时代,情感连接已成为品牌破圈的关键通道。我们在服务大量品牌客户的过程中发现,消费者对内容的“有感”程度,正日益成为影响品牌传播效率与转化率的核心变量。在生成式AI驱动的内容生成与推荐环境中࿰…...
【ROS】Nav2源码之nav2_behavior_tree-行为树节点列表
1、行为树节点分类 在 Nav2(Navigation2)的行为树框架中,行为树节点插件按照功能分为 Action(动作节点)、Condition(条件节点)、Control(控制节点) 和 Decorator(装饰节点) 四类。 1.1 动作节点 Action 执行具体的机器人操作或任务,直接与硬件、传感器或外部系统…...
3403. 从盒子中找出字典序最大的字符串 I
3403. 从盒子中找出字典序最大的字符串 I 题目链接:3403. 从盒子中找出字典序最大的字符串 I 代码如下: class Solution { public:string answerString(string word, int numFriends) {if (numFriends 1) {return word;}string res;for (int i 0;i &…...
【生成模型】视频生成论文调研
工作清单 上游应用方向:控制、速度、时长、高动态、多主体驱动 类型工作基础模型WAN / WAN-VACE / HunyuanVideo控制条件轨迹控制ATI~镜头控制ReCamMaster~多主体驱动Phantom~音频驱动Let Them Talk: Audio-Driven Multi-Person Conversational Video Generation速…...
20个超级好用的 CSS 动画库
分享 20 个最佳 CSS 动画库。 它们中的大多数将生成纯 CSS 代码,而不需要任何外部库。 1.Animate.css 一个开箱即用型的跨浏览器动画库,可供你在项目中使用。 2.Magic Animations CSS3 一组简单的动画,可以包含在你的网页或应用项目中。 3.An…...
RabbitMQ入门4.1.0版本(基于java、SpringBoot操作)
RabbitMQ 一、RabbitMQ概述 RabbitMQ RabbitMQ最初由LShift和CohesiveFT于2007年开发,后来由Pivotal Software Inc.(现为VMware子公司)接管。RabbitMQ 是一个开源的消息代理和队列服务器,用 Erlang 语言编写。广泛应用于各种分布…...
Razor编程中@Html的方法使用大全
文章目录 1. 基础HTML辅助方法1.1 Html.ActionLink()1.2 Html.RouteLink()1.3 Html.Display() / Html.DisplayFor()1.4 Html.Editor() / Html.EditorFor()1.5 Html.Label() / Html.LabelFor()1.6 Html.TextBox() / Html.TextBoxFor() 2. 表单相关辅助方法2.1 Html.BeginForm() …...
Golang——9、反射和文件操作
反射和文件操作 1、反射1.1、reflect.TypeOf()获取任意值的类型对象1.2、reflect.ValueOf()1.3、结构体反射 2、文件操作2.1、os.Open()打开文件2.2、方式一:使用Read()读取文件2.3、方式二:bufio读取文件2.4、方式三:os.ReadFile读取2.5、写…...
