C++ Primer Plus 第四章练习题
2018-01-17 23:07
330 查看
4.7.1 编写一个C++程序,如下述输出示例所示的那样请求并显示信息:
What is your first name? Beety Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name: Yewe, Betty Sue
Grade: C
Age: 22
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档.
4.7.2修改程序清单4.4,使用C++ string类而不是char数组 .
4.7.3编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用char数组和头文件cstring中的函数。下面是该程序运行时的情形:
Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming , Flip
4.7.4编写一个程序,它要求用户首先输入其名,再输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行时的情形:
Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip
4.7.5结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员 存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化 为“Mocha Munch”、2.3和350。初始化应在声明snack时进行。最后,程序显示snack变量的内容。
4.7.6结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化 为所选择的值,然后显示每个结构的内容。
4.7.7William Wingate从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
披萨饼公司的名称,可以有多个单词组成
披萨饼的直径
披萨饼的质量
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin和cout
4.7.8完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司名称之前输入比萨饼的直径 .
4.7.9完成编程练习6,但是用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。
4.7.10编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩)。并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)
What is your first name? Beety Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name: Yewe, Betty Sue
Grade: C
Age: 22
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档.
#include <iostream> #include <string> using namespace std; int main() { string first_name, last_name; cout<<"What is your first name? "; getline(cin, first_name); cout<<"What is your last name? "; getline(cin, last_name); cout<<"What letter grade do you deserve? "; char grade; cin>>grade; cout<<"What is your age? "; unsigned int age; cin>>age; cout<<"Name: "<<last_name<<", "<<first_name<<endl <<"Grade: "<<(char)(grade+1)<<endl <<"Age: "<<age<<endl; return 0; }
4.7.2修改程序清单4.4,使用C++ string类而不是char数组 .
#include <iostream> #include <string> using namespace std; int main() { string name, dessert; cout<<"Enter your name: "<<endl; getline(cin, name); cout<<"Enter your favorite dessert: "<<endl; getline(cin, dessert); cout<<"I have some delicious "<<dessert; cout<<" for you, "<<name<<endl; return 0; }
4.7.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> #include <cstring> using namespace std; int main() { const int ArrSize = 20; char first_name[ArrSize]; char last_name[ArrSize]; cout<<"Enter your first name: "; cin.get(first_name, ArrSize).get(); cout<<"Enter your last name: "; cin.get(last_name, ArrSize).get(); cout<<"Here's the information in a single string: " <<strcat(strcat(last_name, ", "), first_name)<<endl; return 0; }
4.7.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); cout<<"Enter your last name: "; getline(cin ,last_name); cout<<"Here's the information in a single string: " <<last_name + ", " + first_name<<endl; return 0; }
4.7.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; unsigned int calorie; }; int main() { CandyBar snack = { "Mocha Munch", 2.3, 350 }; cout<<"The snack's brand is: "<<snack.brand<<endl <<"The snack's weight is: "<<snack.weight<<endl <<"The snack's calorie is: "<<snack.calorie<<endl; return 0; }
4.7.6结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化 为所选择的值,然后显示每个结构的内容。
#include <iostream> #include <string> using namespace std; struct CandyBar { string brand; double weight; unsigned int calorie; }; int main() { CandyBar snack[3] = { { "Mocha Munch", 2.3, 350 }, { "Apple Tar", 2.5, 300 }, { "Orange Too", 2.6, 900 } }; cout<<"The snack[0]'s brand is: "<<snack[0].brand <<" weight is: "<<snack[0].weight <<" calorie is: "<<snack[0].calorie<<endl; cout<<"The snack[1]'s brand is: "<<snack[1].brand <<" weight is: "<<snack[1].weight <<" calorie is: "<<snack[1].calorie<<endl; cout<<"The snack[2]'s brand is: "<<snack[2].brand <<" weight is: "<<snack[2].weight <<" calorie is: "<<snack[2].calorie<<endl; return 0; }
4.7.7William Wingate从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
披萨饼公司的名称,可以有多个单词组成
披萨饼的直径
披萨饼的质量
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin和cout
#include <iostream> #include <string> using namespace std; struct Pizza { string company_name; double diameter; double weight; }; int main() { Pizza pizza; cout<<"Please input the pizza's company name: "; cin>>pizza.company_name; cout<<"Please input the pizza's diameter: "; cin>>pizza.diameter; cout<<"Please input the pizza's weight: "; cin>>pizza.weight; cout<<"The pizza's company name is "<<pizza.company_name<<endl <<"The pizza's diameter is "<<pizza.diameter<<endl <<"The pizza's weight is "<<pizza.weight<<endl; return 0; }
4.7.8完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司名称之前输入比萨饼的直径 .
#include <iostream> using namespace std; struct Pizza { string company_name; double diameter; double weight; }; int main() { Pizza *pizza = new Pizza; cout<<"Please input the pizza's diameter: "; cin>>pizza->diameter; cout<<"Please input the pizza's company name: "; cin>>pizza->company_name; cout<<"Please input the pizza's weight: "; cin>>pizza->weight; cout<<"The pizza's company name is "<<pizza->company_name<<endl <<"The pizza's diameter is "<<pizza->diameter<<endl <<"The pizza's weight is "<<pizza->weight<<endl; delete pizza; return 0; }
4.7.9完成编程练习6,但是用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。
#include <iostream> #include <string> using namespace std; struct CandyBar { string brand; double weight; unsigned int calorie; }; int main() { CandyBar *snack = new CandyBar[3]; cout<<"The snack[0]'s brand is: "<<snack[0].brand <<" weight is: "<<snack[0].weight <<" calorie is: "<<snack[0].calorie<<endl; cout<<"The snack[1]'s brand is: "<<snack[1].brand <<" weight is: "<<snack[1].weight <<" calorie is: "<<snack[1].calorie<<endl; cout<<"The snack[2]'s brand is: "<<snack[2].brand <<" weight is: "<<snack[2].weight <<" calorie is: "<<snack[2].calorie<<endl; delete [] snack; return 0; }
4.7.10编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩)。并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)
#include <iostream> #include <array> using namespace std; int main() { array<double, 3> ai; double first_score, second_score, third_score; cout<<"Please input the first score: "; cin>>first_score; cout<<"Please input the second score: "; cin>>second_score; cout<<"Please input the third score: "; cin>>third_score; ai[0] = first_score; ai[1] = second_score; ai[2] = third_score; cout<<"The number of times is: "<<ai.size()<<endl <<"The average score is: "<<(ai[0] + ai[1] + ai[2]) / 3.0<<endl; return 0; } /*我使用的IDE是codeblocks,我在编译时出现了这个错误: This file requires compiler and library support for the \ ISO C++ 2011 standard.其实就是说array数组需要C++ 2011 标准。 也就是说编译选项应该选上C++ 2011标准,具体步骤如下: Settings->Compiler->Compiler Flags ->Check:"Have g++ follow the C++11 ISO C++ language standard" 如果你使用的是其他IDE或者linux等系统,出现这个错误可以在网上寻找答案。 */
相关文章推荐
- 【第四章】C++ Primer plus 的编程练习题(选取部分)
- C++ primer plus(第六版)第四章练习题
- C++ Primer Plus 第六版(中文版)课后编程题----第四章
- 《C++ Primer Plus(第六版)》(3)(第四章 复合类型 笔记)
- C++回炉之_C++PrimerPlus_第四章 复合类型(一)
- C++ Primer Plus学习:第四章
- C++ Primer Plus学习:第四章 复合类型(4)
- C++ Primer Plus(第六版)第四章编程练习参考答案
- C++回炉之_C++PrimerPlus_第四章 复合类型(二)
- C++ primer plus 第六版课后作业和题——第四章——关于字符串、结构体、共同体、枚举的知识
- C++ Primer Plus学习:第四章 复合类型(2)
- C++ Primer Plus第五版 第四章 编程练习答案
- C++ Primer Plus(第六版)--学习杂记(第四章)
- 【第六章】C++ Primer plus 的编程练习题(选取部分)
- C++ Primer Plus 学习笔记 第四章 02
- 【第九章】C++ Primer plus 的编程练习题
- 【第二章】C++ Primer plus 的编程练习题(选取部分)
- C++ Primer Plus学习:第四章 复合类型(3)
- 【第八章】C++ Primer plus 的编程练习题(选取部分)
- C++ Primer Plus第四章课后编程答案