声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便。

(具体方式参见第3章模板)

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std; void cprimerplus_exercise4_1()
{
string firstname, lastname;
char lettergrade;
int age;
cout << "What is you first name?";
getline( cin, firstname); cout << "What is your last name?";
getline(cin ,lastname); cout << "What letter grade do you deserve?";
cin >> lettergrade; cout << "What is your age?";
cin >> age; cout << "Name:" << lastname << "," << firstname << endl;
cout << "Grade:"<< char (lettergrade + 1) << endl;
cout << "Age:" << age << endl;
}

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void cprimerplus_exercise4_2()
{
string name;
string dessert;
cout << "Enter your name:" << endl;
getline( cin, name); cout << "Enter your favourite dessert:" << endl;
getline(cin ,dessert); cout << "I have some delicious " << dessert << " for you," << name << endl; }

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void cprimerplus_exercise4_3()
{
char firstname[20];
char lastname[20];
cout << "Enter your first name:";
cin.getline( firstname, 20); cout << "Enter your last name:";
cin.getline(lastname, 20); cout << "Here is the information in a single string:" << strcat(strcat( firstname,","), lastname ) << endl;
}

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void cprimerplus_exercise4_4()
{
string firstname;
string lastname; cout << "Enter your first name:";
getline(cin, firstname); cout << "Enter your last name:";
getline(cin, lastname); cout << "Here is the information in a single string:"\
<<lastname + "," + firstname << endl;
}

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void cprimerplus_exercise4_5()
{
struct CandyBar
{
char name[20];
double weight;
int calary;
}; CandyBar snack = {
"Mocha Munch",
2.3,
350
};
cout << "Name:" << snack.name << endl;
cout << "Weight:" << snack.weight << endl;
cout << "calary:" << snack.calary << endl;
}

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std; void cprimerplus_exercise4_6()
{
struct CandyBar
{
char name[20];
double weight;
int calary;
}; CandyBar snack[3] = {
{"Mocha Munch", 2.3, 350},\
{"Mocha Munch", 2.3, 350},\
{"Mocha Munch", 2.3, 350},\
};
for (int i = 0; i < 3; i++)
{
cout << "Name:" << snack[i].name << endl;
cout << "Weight:" << snack[i].weight << endl;
cout << "calary:" << snack[i].calary << endl;
cout << endl;
} }

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

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

(2)披萨饼的直径。

(3)披萨饼的重量。

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std; void cprimerplus_exercise4_7()
{
struct pizzamessage{
string name;
double diameter;
double weight;
}; pizzamessage message;
cout << "Please input the message of pizza!"<<endl;
cout << "Name:";
getline( cin, message.name); cout <<"Diameter:";
cin >> message.diameter; cout << "Weight:";
cin >> message.weight; cout << "the message of the record is:"<< endl;
cout << "Name:" << message.name;
cout << "Diameter:" << message.diameter;
cout << "Weight:" << message.weight;
}

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std; void cprimerplus_exercise4_8()
{
struct pizzamessage{
string name;
double diameter;
double weight;
}; int num;
cout << "How many pieces of pizzamessage you want to be record?";
cin >> num;
cin.get(); pizzamessage *message = new pizzamessage[num]; for (int i = 0; i < num; i++)
{
cout << "Please input the pizza message of the " << i + 1 << "th record:"<< endl;
cout << "Name:";
getline(cin,message[i].name); cout <<"Diameter:";
cin >> message[i].diameter; cout << "Weight:";
cin >>message[i].weight;
cout << endl; cin.get(); } for (int i = 0; i < num; i++)
{
cout << "the message of the "<< i+1 << "record is:"<< endl;
cout << "Name:" << message[i].name << endl;
cout << "Diameter:" << message[i].diameter << endl;
cout << "Weight:" << message[i].weight << endl;
cout << endl;
} }

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

#include <iostream>
#include <string>
#include <cstring>
using namespace std; void cprimerplus_exercise4_9()
{
struct CandyBar
{
char name[20];
double weight;
int calary;
}; cout << "How many do you want to create?";
int num;
cin >> num;
cin.get(); CandyBar *candies = new CandyBar[num]; for (int i = 0; i < num; i++ )
{
cout << "please input the message:" << endl;
cout << "name:";
cin >> candies[i].name; cout << "weight:";
cin >> candies[i].weight; cout << "calary:";
cin >> candies[i].calary;
}
cout << endl; for (int i = 0; i < num; i++)
{
cout << "the message:" << endl;
cout << "name:" << candies[i].name << endl;
cout << "weight:" << candies[i].weight << endl;
cout << "calary:" << candies[i].calary << endl; cout << endl;
}
}

10. 编写一个程序,让用户输入三次40码跑的成绩(也可以让用户输入),并显示次数和平均成绩。请使用一个array对象来储存数据(也可以使用数组)。

#include <iostream>
#include <string>
#include <cstring>
using namespace std; void cprimerplus_exercise4_10()
{
cout << "How many times do you want to record?"<< endl;
int num;
cin >> num; double * times = new double[num]; for (int i = 0; i < num; i++)
{
cout << i+1 << " th input:" << endl;
cin >> times[i];
} double sum = 0;
for (int i = 0; i < num; i++)
{
sum +=times[i];
} cout << "you have " << num << "records" << endl;
cout << " your average score is " << sum / num << endl;
}

c++primerplus(第六版)编程题——第4章(复合类型)的更多相关文章

  1. c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  2. c++primerplus(第六版)编程题——第3章(数据类型)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. 工程命名和文件命名可以命名成易识 ...

  3. c++primerplus(第六版)编程题——第5章(循环和关系表达式)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  4. 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)

    第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...

  5. C程序设计(谭浩强)第五版课后题答案 第一章

    大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...

  6. c primer plus(五版)编程练习-第七章编程练习

    1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...

  7. PAT 基础编程题 4-11 求自定类型元素序列的中位数(希尔排序)

    4-11 求自定类型元素序列的中位数   (25分) 本题要求实现一个函数,求N个集合元素A[]的中位数,即序列中第\lfloor N/2 +1\rfloor⌊N/2+1⌋大的元素.其中集合元素的类型 ...

  8. C++PrimerPlus第6版 第四章——复合类型

    1,复合类型主要包含:数组.结构.联合.枚举.类.指针.引用等. 2,数组.长度必须确定.即编译阶段,数组的长度就得确定好.所以只能使用常量(#define.const)声明数组长度.如果使用变量声明 ...

  9. 中国MOOC_面向对象程序设计——Java语言_第4章 继承与多态_第4周编程题_将MP3媒体类型存放进Database

    本周我们介绍了以继承方式实现的媒体资料库,在课程代码实现的基础上,请实现一个表达MP3的媒体类型,能和CD.DVD一样存放进这个Database.请提交这个MP3类的代码.如果你认为为了能存放MP3, ...

随机推荐

  1. node begining

    node begining */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Consola," ...

  2. hdoj 2074 叠筐

    叠筐 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  3. SQL 主键和外键约束

    SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...

  4. Excel异常Cannot get a text value from a numeric cell

    POI操作Excel时数据Cell有不同的类型,当我们试图从一个数字类型的Cell读取出一个字符串并写入数据库时,就会出现Cannot get a text value from a numeric ...

  5. requireJS(二)

    一.前言 requireJS(一) 本篇主要整理requirejs的一些用法,相对比较零散. 实例目录 二.优化 requirejs建议我们给每一个模块书写一个js文件.但是这样会增加网站的http请 ...

  6. Android M(6.0) 权限爬坑之旅

    坑一:用Android5.0编译的apk,在Android6.0上运行完全没有问题. 在Android6.0以上才需要在运行时请求权限,在旧Android版本上保留原有逻辑,安装时授予权限. 用旧版本 ...

  7. Regular Expressions in Grep Command with 10 Examples --reference

    Regular expressions are used to search and manipulate the text, based on the patterns. Most of the L ...

  8. Java Stax操作XML简介

    使用stax操作xml 非常的简单,它的读取过程像是一个光标在移动.针对不同的节点做不同的处理. 先看一个基于光标的模型处理xml: public class StaxTest { @Test pub ...

  9. javascript split() 正则表达式

    路由匹配 http.createServer(function(req, res) { var items = req.url.split('/'); if (items.length < 3 ...

  10. MVC缓存,使用数据层缓存,添加或修改时让缓存失效

    在"MVC缓存01,运用控制器缓存或数据层缓存"中,在数据层中可以设置缓存的有用时刻.但这个还不够"智能",常常期望在修改或创立的时分使缓存失效,加载新的数据. ...