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

(具体方式参见第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. How Do I Deploy a Windows 8 App to Another Device for Testing?

    If your developing a new Windows 8 app and you want to test it on another device (e.g. Surface), you ...

  2. 1 weekend110的复习 + hadoop中的序列化机制 + 流量求和mr程序开发

    以上是,weekend110的yarn的job提交流程源码分析的复习总结 下面呢,来讲weekend110的hadoop中的序列化机制 1363157985066      13726230503  ...

  3. Shell数组:shell数组的定义、数组长度

    Shell在编程方面比Windows批处理强大很多,无论是在循环.运算. bash支持一维数组(不支持多维数组),并且没有限定数组的大小.类似与C语言,数组元素的下标由0开始编号.获取数组中的元素要利 ...

  4. DevExpress 用户控件 分页(下)

    分页控件调用 (1)初始化时: this.pageCtrl1.pageSize = 4; (2)数据绑定时: 从数据库中获取实时的 Public void LoadData(){ //这是只写有关分页 ...

  5. 【设计模式 - 21】之空对象模式(Null Object)

    1      模式简介 在空对象模式中,一个空对象取代NULL对象的实例的检查.NULL对象不是检查空值,而是反映一个不做任何动作的关系.这样的NULL对象也可以在数据不可用的时候提供默认的行为. 在 ...

  6. js 设置cookie

    function GetCookieVal(offset) // 获得Cookie解码后的值 { var endstr = document.cookie.indexOf(";", ...

  7. ABAP更改现有程序

    语法: READ REPORT <prog> INTO <itab>. INSERT REPORT <prog> FROM  <itab>. 假定下列简 ...

  8. sublimeText3安装package control和禁止弹出更新下载弹窗

    1.sublimeText3安装package control import urllib.request,os; pf = 'Package Control.sublime-package'; ip ...

  9. BA Practice Lead Handbook 1 - Why Is Business Analysis Taking The World By Storm?

    The articles in this series are focused on individual Business Analysts and their managers. https:// ...

  10. angularjs ngrepeat filter

    angularjs ng-repeat filter演示样例 地址