博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ Primer Plus 编程练习4
阅读量:3968 次
发布时间:2019-05-24

本文共 6842 字,大约阅读时间需要 22 分钟。

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

What is your first name? Betty SueWhat is your last name? YeweWhat letter grade do you deserve? BWhat is your age? 22Name: Yewe, Betty SueGrade: CAge: 22

注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档。

#include "iostream"using namespace std;const int nsize = 20;struct student{
char name[nsize]; char last_name[nsize]; char grade; int age;};void display(student);int main(){
cout << "what is your name? " ; student dyg; cin.getline(dyg.name, nsize); cout << "what is your last_name? " ; cin.getline(dyg.last_name, nsize); cout << "what is your grade? "; cin >> dyg.grade; cout << "what is your age? " ; cin >> dyg.age; display(dyg); return 0;}void display(student lz){
cout << "name: " << lz.name <<"," << lz.last_name << endl; cout << "grade: " << char(lz.grade + 1) << endl; cout << "age: " << lz.age << endl;}

在这里插入图片描述

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

//程序清单4.4:// instr2.cpp -- reading more than one word with getline#include 
int main(){
using namespace std; const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter your name:\n"; cin.getline(name, ArSize); // reads through newline cout << "Enter your favorite dessert:\n"; cin.getline(dessert, ArSize); cout << "I have some delicious " << dessert; cout << " for you, " << name << ".\n"; return 0; }
#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,"<
<

在这里插入图片描述

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

Enter your first name: FlipEnter your last name: FlemingHere’s the information in a single string: Fleming, Flip
#include "iostream"#include "cstring"using namespace std;const int nsize = 20;int main(){
char fname[nsize]; char lname[nsize]; char name[2*nsize+1]; cout << "enter your first name: "; cin.getline(fname, nsize); cout << "enter your last name: "; cin.getline(lname, nsize); strncpy(name, fname, nsize); strcat(name, ","); strncat(name,lname,nsize); name[2*nsize] = '\0'; cout << "here's the information in a single string: " << name << endl; return 0;}

在这里插入图片描述

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

Enter your first name: FlipEnter your last name: FlemingHere’s the information in a single string: Fleming, Flip
#include "iostream"#include "string"using namespace std;int main(){
string fname, lname, add, fullname; cout << "enter your first name: "; getline(cin, fname); cout << "enter your last name: "; getline(cin, lname); add = ","; fullname = fname + add + lname; cout << "here's the information in a single string: " << fullname << endl; return 0;}

在这里插入图片描述

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

#include "iostream"using namespace std;const int asize = 20;struct candybar {
char brand[asize]; double weight; int calory;};int main(){
candybar snack = {
"Mocha Munch", 2.3, 350}; cout << "here's the information of snack:" << endl; cout << "brand: " << snack.brand << endl; cout << "weight: " << snack.weight << endl; cout << "calory: " << snack.calory ; return 0;}

在这里插入图片描述

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

#include "iostream"using namespace std;const int asize = 20;struct candybar {
char brand[asize]; double weight; int calory;};int main(){
candybar snack[3] = {
{
"A", 1.2, 3}, {
"B", 4.5, 6}, {
"C", 7.8, 9} }; for (int i = 0; i < 3;i++) {
cout <<" brand:"<< snack[i].brand << endl <<" weight:"<< snack[i].weight << endl <<" calory:"<< snack[i].calory << endl<

在这里插入图片描述

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

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

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

#include "iostream"#include "string"using namespace std;const int asize = 20;struct pizza{
char company[asize]; double diameter; double weight;};int main(){
pizza pie; cout << "what's the name of the pizza company?"; cin.getline(pie.company, asize); cout << "what's the diameter of the pizza?"; cin >> pie.diameter; cout << "what's the weight of the pizza?"; cin >> pie.weight; cout << "company: " << pie.company << endl; cout << "diameter: " << pie.diameter << endl; cout << "weight: " << pie.weight << endl; return 0;}

在这里插入图片描述

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

#include "iostream"#include "string"using namespace std;const int asize = 20;struct pizza{
char company[asize]; double diameter; double weight;};int main(){
pizza *pie = new pizza; cout << "what's the diameter of pizza? "; cin >> pie->diameter; cin.get(); cout << "what's the name of the pizza company? "; cin.get(pie->company, asize); cout << "what's the weight of the pizza ? "; cin >> pie->weight; cout << "company: " << pie->company << endl; cout << "diameter(inches): " << pie->diameter << endl; cout << "weight(ounce): " << pie->weight << endl; delete pie; return 0;}

在这里插入图片描述

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

#include "iostream"#include "string"using namespace std;struct candybar{
string brand; double weight; int calory;};int main(){
candybar *snack = new candybar[3]; snack[0].brand = "A"; snack[0].weight = 1.1; snack[0].calory = 600; snack[1].brand = "C"; snack[1].weight = 6.9; snack[1].calory = 900; snack[2].brand = "X"; snack[2].weight = 12.6; snack[2].calory = 1900; for (int i = 0; i < 3;i++) {
cout << "brand: " << snack[i].brand << endl << "weight: " << snack[i].weight << endl << "calory: " << snack[i].calory << endl << endl; } delete[] snack; return 0;}

在这里插入图片描述

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

#include "iostream"#include "array"using namespace std;int main(){
array
grade = {
0}; cout << "输入三次40米跑成绩: " << endl; cin >> grade[0] >> grade[1] >> grade[2] ; cout << "成绩1: " << grade[0] << endl; cout << "成绩2: " << grade[1] << endl; cout << "成绩3: " << grade[2] << endl; grade[3] = (grade[0] + grade[1] + grade[2]) / 3; cout << "平均成绩:"<
<< endl; return 0;}

在这里插入图片描述

转载地址:http://pfcki.baihongyu.com/

你可能感兴趣的文章
Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
查看>>
Android MediaCodec小结
查看>>
YUV格式说明
查看>>
MediaCodec and Camera: colorspaces don't match
查看>>
android adb 读写模式 挂载文件系统
查看>>
onTouchEvent方法的使用
查看>>
Android详细解释键盘和鼠标事件
查看>>
迷茫的大学生(刘润,微软全球技术中心经理 )
查看>>
Linux自动运行程序五法
查看>>
让人敬佩的程序员
查看>>
WORD 域
查看>>
安装tomcat5时停在jvm.dll的解决
查看>>
Lucene 基础指南
查看>>
浏览器地址栏中加入ico图标
查看>>
Struts2文件的上传和下载
查看>>
Eclipse文件改变编码插件
查看>>
FreeMarker使用小结
查看>>
acegi-security-samples-contacts分析
查看>>
Acegi 设计概述
查看>>
让页面变得更快一点-HTML解析原理
查看>>