第四章

1. 编写一个C++程序,如下述输出示例所示的那样请求并显示信息:

What is your first name? Betty Sue

What is your last name? Yewe

What letter grade do you deserve? B

What is your age? 22

Name: Yewe, Betty Sue

Grade:

Age: 22

******比较简单的做法,但是不符合题目要求

#include<string>using namespace std;int main(){const int SIZE = 20;int age;char first_name[SIZE];char last_name[SIZE];char grade;cout << "What is your first name ?";cin.getline(first_name, SIZE);cout << "What is your last name?";cin.getline(last_name, SIZE);cout << "What letter grade do you deserve?";cin >> grade;cout << "What is your age?";cin >> age;cout << "Name:" << last_name << ", " << first_name << endl;cout << "Grade:" << char(grade + 1)<< endl;cout << "Age:" << age << endl;system("pause");return 0;}//What is your first name ? Betty Sue//What is your last name ? Yeme//What letter grade do you deserve ? B//What is your age ? 22//Name : Yeme, Betty Sue//Grade : C//Age : 22//请按任意键继续. . .
#include<iostream>#include<string>using namespace std;int main(){string first_name;string last_name;int age;char grade;cout << "What is your first name ?";cin>>first_name;cout << "What is your last name ?";cin>>last_name;cout << "What letter grade do you deserve ?";cin>>grade;cout << "What is your age?";cin >> age;cout << "Name:" << last_name << ", " << first_name << endl;cout << "Grade:" << char(grade + 1)<< endl;cout << "Age:" << age << endl;system("pause");return 0;}//What is your first name ? Betty_Sue//What is your last name ? Yeme//What letter grade do you deserve ? A//What is your age ? 22//Name : Yeme, Betty_Sue//Grade : B//Age : 22//请按任意键继续. . .

PS:输入的名字的时候不要有空格,string类型的有空格就会显示不出来(具体原因我也不太清楚)

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

#include<iostream>#include<string>using namespace std;int main(){string name;string dessrt;cout << "Enter your name ?" << endl;getline(cin, name);//cin>>name;cout << "Enter your favorite dessert?" << endl;getline(cin, dessrt);//cin >> dessrt;cout << "I have some delicious " << dessrt;cout << " for you," << name << endl;system("pause");return 0;}//Enter your name ?//Dirk Hammernose//Enter your favorite dessert ?//Radish Torte//I have some delicious Radish Torte for you, Dirk Hammernose//请按任意键继续. . .

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

Enter your first name: Flip

Enter your last name: Fleming

Here's the information in a single string: Fleming, Flip

#include<iostream>using namespace std;int main(){char first_name[20];char last_name[20];cout << "Enter your first name:";cin >> first_name;cout << "Enter your last name:";cin >> last_name;cout << "Here is the information in a single string:" <<last_name <<", "<<first_name<< endl;system("pause");return 0;}//Enter your first name : Flep//Enter your last name : Fleming//Here is the information in a single string : Fleming, Flep//请按任意键继续. . .
#include<iostream>using namespace std;int main(){const int size = 20;char first_name[size];char last_name[size];char full_name[size+10];cout << "Enter your first name:";cin >> first_name;cout << "Enter your last name:";cin >> last_name;strcpy_s(full_name, last_name);strcat_s(full_name, ", ");strcat_s(full_name, first_name);cout << "Here is the information in a single string:" << full_name << endl;system("pause");return 0;}//Enter your first name : Flep//Enter your last name : Fleming//Here is the information in a single string : Fleming, Flep//请按任意键继续. . .
#include<iostream>#include<cstring>#include<string>using namespace std;int main(){const int size = 20;char first_name[size];char last_name[size];char full_name[size];cout << "Enter your first name:";cin.getline(first_name,size);cout << "Enter your last name:";cin.getline(last_name,size);strcpy_s(full_name, last_name);strcat_s(full_name, ", ");strcat_s(full_name, first_name);cout << "Here is the information in a single string:" << full_name << endl;system("pause");return 0;}

在编译器里面需要使用strcpy_s,strcat_s函数,不然在IDE会产生报错,我在Ubuntu20.04下面用g++编译的时候使用strcpy,strcat没问题,但是在vs2017上面不行。

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

Enter your first name: Flip

Enter your last name: Fleming

Here's the information in a single string: Fleming, Flip

#include<iostream>#include<string>using namespace std;int main(){string first_name, last_name;cout << "Enter your first name:";getline(cin, first_name);//cin >> first_name;cout << "Enter your last name:";getline(cin, last_name);//cin >> last_name;last_name += ", ";cout<< "Here is the information in a single string:" << last_name << first_name << endl;//cout << "Here is the information in a single string:" << last_name << ", " << first_name << endl;system("pause");return 0;}//Enter your first name:Flip//Enter your last name : Fleming//Here is the information in a single string : Fleming, Flip//请按任意键继续. . .
#include<iostream>#include<string>using namespace std;int main(){string first_name, last_name, full_name;cout << "Enter your first name:";getline(cin, first_name);//cin >> first_name;cout << "Enter your last name:";getline(cin, last_name);//cin >> last_name;full_name = last_name + ", " + first_name;cout<< "Here is the information in a single string:" << full_name << endl;system("pause");return 0;}//Enter your first name:Flip//Enter your last name : Fleming//Here is the information in a single string : Fleming, Flip//请按任意键继续. . .

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

#include<iostream>using namespace std;struct inflatable{char brand[20];float weight;double energy;};int main(){inflatable CandyBox ={"Mocha Munch",2.3,350};inflatable snack;snack = CandyBox;cout << "Snack brand:" << snack.brand << endl;cout << "Snack weight:" << snack.weight << endl;cout << "Snack price:" << snack.energy << endl;system("pause");return 0;}//Snack brand : Mocha Munch//Snack weight:2.3//Snack price : 350//请按任意键继续. . .
#include<iostream>using namespace std;struct CandyBox{char brand[20];float weight;unsigned int calories;};int main(){CandyBox snack ={"Mocha Munch",2.3,350};cout << "Snack brand:" << snack.brand << endl;cout << "Snack weight:" << snack.weight << endl;cout << "Snack price:" << snack.calories << endl;system("pause");return 0;}//Snack brand : Mocha Munch//Snack weight:2.3//Snack price : 350//请按任意键继续. . .

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

#include<iostream>using namespace std;struct CandyBox{char brand[20];float weight;unsigned int calories;};int main(){CandyBox snack[3] ={{"Mocha Munch",2.3,350},{"Dove",3.6,400},{"Ferrero",1.0,360}};//输出第一个默认值cout << "Snack brand 1:" << snack[0].brand << endl;cout << "Snack weight 1:" << snack[0].weight << endl;cout << "Snack price 1:" << snack[0].calories << endl;//输出第二个默认值cout << "Snack brand 2:" << snack[1].brand << endl;cout << "Snack weight 2:" << snack[1].weight << endl;cout << "Snack price 2:" << snack[1].calories << endl;//输出第三个默认值cout << "Snack brand 3:" << snack[2].brand << endl;cout << "Snack weight 3:" << snack[2].weight << endl;cout << "Snack price 3:" << snack[2].calories << endl;system("pause");return 0;}//Snack brand 1:Mocha Munch//Snack weight 1:2.3//Snack price 1 : 350//Snack brand 2 : Dove//Snack weight 2:3.6//Snack price 2 : 400//Snack brand 3 : Ferrero//Snack weight 3:1//Snack price 3 : 360//请按任意键继续. . .

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

- 披萨饼公司的名称,可以有多个单词组成。

- 披萨饼的直径。

- 披萨饼的重量。

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

#include<iostream>using namespace std;struct pizzacompany{char company_name[20];float diameter;float weight;};int main(){pizzacompany dinner;cout << "Enter thr pizza company information:";cout << "Pizza's company:";cin.getline(dinner.company_name,20);cout << "Enter the pizza's diameter:";cin >> dinner.diameter;cout << "Enter the pizza's weight:";cin >> dinner.weight;cout << "Company name:" << dinner.company_name <<" food company"<< endl;cout << "Pizza's diameter:" << dinner.diameter <<" cm."<< endl;cout << "Pizza's weight:" << dinner.weight <<" pounds."<< endl;system("pause");return 0;}////Enter thr pizza company information : Pizza's company:William Wingate//Enter the pizza's diameter:10//Enter the pizza's weight:0.5//Company name : William Wingate food company//Pizza's diameter:10 cm.//Pizza's weight:0.5 pounds.//请按任意键继续. . .

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

#include<iostream>using namespace std;struct pizza{char company[20];float diameter;float weight;};int main(){pizza *ppizza = new pizza;cout << "Enter the company information:" << endl;cout << "Enter the company name:";cin.getline(ppizza->company,20);cout << "Enter the pizza diameter:";cin >> ppizza->diameter;cout << "Enter the pizza weight:";cin >> ppizza->weight;cout << "Company: " << ppizza->company <<" food company."<<endl;cout << "Pizza's diameter: " << ppizza->diameter << " cm." << endl;cout << "Pizza's weight: " << ppizza->weight << " pounds." << endl;system("pause");return 0;}//Enter the company information ://Enter the company name : KFC//Enter the pizza diameter ://20//Enter the pizza weight : 2//Company : KFC food company.//Pizza's diameter: 20 cm.//Pizza's weight: 2 pounds.//请按任意键继续. . .

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

*****************没有满足题目要求***********************************

#include<iostream>using namespace std;struct CandyBox{char brand[20];float weight;double energy;};int main(){CandyBox *CCandyBox = new CandyBox;cout << "Enter the snack brand:";cin.get(CCandyBox->brand, 20);cout << "Enter the snack weight:";cin >> CCandyBox->weight;cout << "Enter the snack energy:";cin >> (*CCandyBox).energy;cout << "Snack Brand:" << CCandyBox->brand <<" food company."<< endl;cout << "Snack Weight:" << CCandyBox->weight <<" gram."<<endl;cout << "Snack energy:" << CCandyBox->energy <<" calories."<< endl;system("pause");return 0;}//Enter the snack brand : Mocha Munch//Enter the snack weight : 2.3//Enter the snack energy : 50//Snack Brand : Mocha Munch food company.//Snack Weight : 2.3 gram.//Snack energy : 50 calories.//请按任意键继续. . .

************************************************************

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

#include<iostream>#include<vector>#include<array>using namespace std;int main(){float average_1, average_2;vector<float> grade_1(3);array<float, 3> grade_2;cout << "Enter the first grade of 40 meters:";cin >> grade_1[0];cin >> grade_2[0];cout << "Enter the second grade of 40 meters:";cin >> grade_1[1];cin >> grade_2[1];cout << "Enter the third grade of 40 meters:";cin >> grade_1[2];cin >> grade_2[2];average_1 = (grade_1[0] + grade_1[1] + grade_1[2]) / 3;average_2 = (grade_2[0] + grade_2[1] + grade_2[2]) / 3;cout << "Average_1 Grade = " << average_1 << endl;cout << "Average_2 Grade = " << average_1 << endl;system("pause");return 0;}//Enter the first grade of 40 meters:5.6//4.5//Enter the second grade of 40 meters : 4.8//5.2//Enter the third grade of 40 meters : 5.1//4.7//Average_1 Grade = 5.16667//Average_2 Grade = 5.16667//请按任意键继续. . .
#include<iostream>#include<array>using namespace std;int main(){array<float, 3> record_list;float average;cout << "Please input three record of 40 miles:" << endl;cout << "First record:";cin >> record_list[0];cout << "Second record:";cin >> record_list[1];cout << "Third record:";cin >> record_list[2];cout << "OK,you input:\n1." << record_list[0] << "\n2." << record_list[1] << "\n3." << record_list[2] << endl;average = (record_list[0] + record_list[1] + record_list[2]) / 3;cout << "Your average performance is " << average << " ." << endl;system("pause");return 0;}//Please input three record of 40 miles://First record : 5.6//Second record : 4.8//Third record : 5.1//OK, you input ://1.5.6//2.4.8//3.5.1//Your average performance is 5.16667 .//请按任意键继续. . .

C++ Primer Plus(第六版)第4章 编程练习答案详解相关推荐

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

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

  2. C Primer Plus第六版第七章编程题目与参考答案⭐

    1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数.换行符数和所有其他字符的数量. #include <stdio.h> #define STOP '#' #define SP ...

  3. C++ Primer Plus(第六版)第6章 编程练习答案详解

    1. 编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列). #include<iostream& ...

  4. C++primer plus第六版第四章编程题代码

    1. #include "stdafx.h" #include <iostream> #include <string> #include <vect ...

  5. C Primer Plus第六版第四章编程题目与参考答案⭐

    1.编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印出来. #include <stdio.h>int main() {char firstname[20] ...

  6. 跟着c++Primer Plus学编程--- 4.13编程练习答案详解】

    跟着c++Primer Plus学编程--- 4.13编程练习答案详解 1.编写一个C++程序,如下述输出事例所示那样请求并显示信息. 2.修改程序清单4.4,使用C++ string 类而不是 ch ...

  7. 《C Prime Plus》(第六版) 第03章 编程练习 7 英寸转换成厘米

    C Prime Plus 第六版 编译器版本:Mac Xcode 11.6  第03章 数据和C 编程练习 7 7. 1英寸相当于2.54厘米.编写一个程序,提示用户输入身高(/英寸),然后以厘米为单 ...

  8. 《C Prime Plus》(第六版) 第03章 编程练习 8 品脱/盎司/大汤勺/茶勺单位换算

    C Prime Plus 第六版 编译器版本:Mac Xcode 11.6  第03章 数据和C 编程练习 8 在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于 ...

  9. C Primer Plus(第六版)第三章 数据和C

    笔记记录 1.float 类型可以储存带小数的数字. 2.printf()中使用%f来处理浮点值.%.2f中的.2用于精确控制输出,指定输出的浮点数只显示小数点后面两位. 3.scanf()函数用于读 ...

最新文章

  1. Android SpannableString 给TextView 设置颜色,删除线等
  2. Javascript年月日联动
  3. MySQL 索引与优化
  4. MarkDown常用技巧总结
  5. 2016级算法期末上机-F.中等·AlvinZH's Fight with DDLs II
  6. LoadRunner培训初级教程
  7. cass坡度土方计算案例_【九天教您南方cass 9.1】 12 道路断面土方计算
  8. vue取url路径传参_vue不通过路由直接获取url中参数的方法示例
  9. 【BZOJ】3238: [Ahoi2013]差异
  10. linux内核C -- 第05课:零长度数组
  11. django的orm获取字段去重值
  12. sourceMap到底是个啥玩意?
  13. 数据预处理01_脏数据产生的原因
  14. python开发平台介绍_【Python 入门】简介与搭建开发环境
  15. 风火牙疼,紧急止痛、快速治疗的真实历程
  16. mathtype公式减号变短格式重置调整
  17. 《软技能(代码外的生存指南)》读书笔记——持续更新
  18. 人体信号基线漂移问题
  19. Java并发插件_五分钟,轻松掌握Java并发编程!
  20. 人工智能培训就业,主要有什么特点?

热门文章

  1. joycon 连不上_JoyCon手柄怎么跟switch配对 详细Joy-Con与主机配对教程
  2. AIOps,未来正来
  3. 【操作系统】进程-吸烟者问题
  4. hibernate QBC和QBE精讲与案列分析(中)
  5. 【数据分析】使用pandas和numpy分析美国大选献金项目
  6. 抽象数据类型线性表的定义与实现
  7. 带外数据:TCP紧急模式分析
  8. 《Real-Time Rendering 4th Edition》读书笔记--简单粗糙翻译 第七章 阴影 Shadows
  9. python爬虫从入门到实战笔记——第四章Scrapy框架
  10. 百度360搜索关键字