C++ Primer Plus第四章编程练习

2024-09-03 04:32

本文主要是介绍C++ Primer Plus第四章编程练习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1

编写一个C++程序,如下述输出示例所示的那样请求并显示信息:
在这里插入图片描述
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档。

#include <iostream>
#include <string>using namespace std;
struct people{string first_name;string last_name;char grade;int age;
};int show_people(struct people p)
{cout<<"**********************************************\n";cout<<"Name: "<<p.last_name<<", "<<p.first_name<<"\n";cout<<"Grade: "<<p.grade<<"\n";cout<<"Age: "<<p.age<<endl;return 0;
}
int main()
{struct people p1;cout<<"What is your first name? ";getline(cin, p1.first_name);cout<<"What is your last name? ";getline(cin, p1.last_name);cout<<"What letter grade do you deserve? ";cin>>p1.grade;p1.grade = p1.grade + 1;cout<<"What is your age? ";cin>>p1.age;show_people(p1);return 0;
}

2

修改程序清单4.4,使用C++ string类而不是char数组。

#include <iostream>
#include <string>int main()
{using namespace std;string name, dessert;cout<<"Enter your name:\n";getline(cin, name);cout<<"Enter your favorite dessert:\n";getline(cin, dessert);cout<<"I have some delicious "<<dessert;cout<<" for you, "<<name<<".\n";return 0;
}

下图为程序运行结果:
程序结果

3

编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用char数组和头文件cstring中的函数。下面是该程序运行时的情形:
在这里插入图片描述

#include <iostream>
#include <cstring>int main()
{using namespace std;const int Size = 20;char first_name[Size], last_name[Size], name[2*Size];cout<<"Enter your first name: ";cin.getline(first_name, Size);cout<<"Enter your last name: ";cin.getline(last_name, Size);strcpy(name, last_name);strcat(name, ", ");strcat(name, first_name);cout<<"Here's the information in a single string: "<<name<<endl;return 0;
}

4

编写一个程序,它要求用户首先输入其名,再输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行时的情形:
在这里插入图片描述

#include <iostream>
#include <string>int main()
{using namespace std;string first_name, last_name, name;cout<<"Enter your first name: ";getline(cin, first_name);cout<<"Enter your last name: ";getline(cin, last_name);name = first_name + ", " + last_name;cout<<"Here's the information in a single string: "<<name<<endl;return 0;
}

5

结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化为“Mocha Munch”、2.3和350。初始化应在声明snack时进行。最后,程序显示snack变量的内容。

#include <iostream>
#include <string>using namespace std;
struct CandyBar{string brand;double weight;int calorie;
};int main()
{CandyBar snack = {"Mocha Munch", 2.3, 350};cout<<"Brand: "<<snack.brand<<endl;cout<<"Weight: "<<snack.weight<<endl;cout<<"Calorite: "<<snack.calorie<<endl;return 0;
}

6

结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化为所选择的值,然后显示每个结构的内容。

#include <iostream>
#include <string>using namespace std;
struct CandyBar{string brand;double weight;int calorie;
};int main()
{CandyBar snack[3] = {{"Mocha Munch", 2.3, 350},{"Coca Cola", 1.4, 420},{"Green Arrow", 2.7, 260}};int i;for(i=0; i<3; i++){cout<<"\nsnack["<<i+1<<"]:\n";cout<<"Brand: "<<snack[i].brand<<endl;cout<<"Weight: "<<snack[i].weight<<endl;cout<<"Calorite: "<<snack[i].calorie<<endl;}return 0;
}

7

William Wingate从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:

  • 披萨饼公司的名称,可以有多个单词组成。
  • 披萨饼的直径。
  • 披萨饼的重量。

设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。

#include <iostream>const int Size = 50;
struct pizza{char name[Size];double diameter;double weight;
};
int main()
{using namespace std;pizza p1;cout<<"Please input the name of pizza company: ";cin.getline(p1.name, Size);cout<<"Please input the diameter of pizza: ";cin>>p1.diameter;cout<<"Please input the weight of pizza: ";cin>>p1.weight;cout<<"Company: "<<p1.name<<"\n";cout<<"Diameter: "<<p1.diameter<<"\n";cout<<"Weight: "<<p1.weight<<"\n";return 0;
}

以下为程序运行结果:在这里插入图片描述

8

完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司名称之前输入比萨饼的直径。

#include <iostream>const int Size = 50;
struct pizza{char name[Size];double diameter;double weight;
};
int main()
{using namespace std;pizza *p1 = new pizza;cout<<"Please input the diameter of pizza: ";cin>>p1->diameter;cin.get();cout<<"Please input the name of pizza company: ";cin.getline(p1->name, Size);cout<<"Please input the weight of pizza: ";cin>>p1->weight;cout<<"Company: "<<p1->name<<"\n";cout<<"Diameter: "<<p1->diameter<<"\n";cout<<"Weight: "<<p1->weight<<"\n";delete p1;return 0;
}

以下为程序运行结果:
在这里插入图片描述

9

完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。

#include <iostream>
#include <string>using namespace std;
struct CandyBar{string brand;double weight;int calorie;
};int main()
{CandyBar *snack = new CandyBar[3];snack[0].brand = "Mocha Munch";snack[0].weight = 2.3;snack[0].calorie = 350;snack[1].brand = "Caca Cola";snack[1].weight = 1.4;snack[1].calorie = 420;snack[2].brand = "Green Arrow";snack[2].weight = 2.7;snack[2].calorie = 260;int i;for(i=0; i<3; i++){cout<<"\nsnack["<<i+1<<"]:\n";cout<<"Brand: "<<snack[i].brand<<endl;cout<<"Weight: "<<snack[i].weight<<endl;cout<<"Calorite: "<<snack[i].calorie<<endl;}delete [] snack;return 0;
}

以下为程序运行结果:
在这里插入图片描述

10

编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)。

#include <iostream>
#include <array>int main()
{using namespace std;array<double, 3> time;int i;double average, sum = 0;for(i=0; i<3; i++){cout<<"Please input No."<<i+1<<" time: ";cin>>time[i];sum = sum + time[i];}average = sum / 3;for(i=0; i<3; i++){cout<<"Time "<<i+1<<": "<<time[i]<<endl;}cout<<"Average = "<<average<<endl;return 0;
}

以下为程序运行结果:
在这里插入图片描述

这篇关于C++ Primer Plus第四章编程练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1132028

相关文章

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

mybatis-plus如何根据任意字段saveOrUpdateBatch

《mybatis-plus如何根据任意字段saveOrUpdateBatch》MyBatisPlussaveOrUpdateBatch默认按主键判断操作类型,若需按其他唯一字段(如agentId、pe... 目录使用场景方法源码方法改造首先在service层定义接口service层接口实现总结使用场景my

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

AOP编程的基本概念与idea编辑器的配合体验过程

《AOP编程的基本概念与idea编辑器的配合体验过程》文章简要介绍了AOP基础概念,包括Before/Around通知、PointCut切入点、Advice通知体、JoinPoint连接点等,说明它们... 目录BeforeAroundAdvise — 通知PointCut — 切入点Acpect — 切面

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

C#异步编程ConfigureAwait的使用小结

《C#异步编程ConfigureAwait的使用小结》本文介绍了异步编程在GUI和服务器端应用的优势,详细的介绍了async和await的关键作用,通过实例解析了在UI线程正确使用await.Conf... 异步编程是并发的一种形式,它有两大好处:对于面向终端用户的GUI程序,提高了响应能力对于服务器端应