c++primerplus(第六版)编程题——第4章(复合类型)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便。
(具体方式参见第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章(复合类型)的更多相关文章
- c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...
- c++primerplus(第六版)编程题——第3章(数据类型)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. 工程命名和文件命名可以命名成易识 ...
- c++primerplus(第六版)编程题——第5章(循环和关系表达式)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...
- 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)
第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...
- C程序设计(谭浩强)第五版课后题答案 第一章
大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...
- c primer plus(五版)编程练习-第七章编程练习
1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...
- PAT 基础编程题 4-11 求自定类型元素序列的中位数(希尔排序)
4-11 求自定类型元素序列的中位数 (25分) 本题要求实现一个函数,求N个集合元素A[]的中位数,即序列中第\lfloor N/2 +1\rfloor⌊N/2+1⌋大的元素.其中集合元素的类型 ...
- C++PrimerPlus第6版 第四章——复合类型
1,复合类型主要包含:数组.结构.联合.枚举.类.指针.引用等. 2,数组.长度必须确定.即编译阶段,数组的长度就得确定好.所以只能使用常量(#define.const)声明数组长度.如果使用变量声明 ...
- 中国MOOC_面向对象程序设计——Java语言_第4章 继承与多态_第4周编程题_将MP3媒体类型存放进Database
本周我们介绍了以继承方式实现的媒体资料库,在课程代码实现的基础上,请实现一个表达MP3的媒体类型,能和CD.DVD一样存放进这个Database.请提交这个MP3类的代码.如果你认为为了能存放MP3, ...
随机推荐
- Tomcat启动失败闪退
最近把电脑系统从win8升到了8.1(之前源于各种原因都没升外带升级失败),用都用了1个月了,突然发现tomcat启动不了,提示找不到什么什么- -,因为平时基本都是从开发工具里运行的服务器,都没有问 ...
- [leetcode]重建二叉树(先序和终须) 中序遍和后续
分割后长度相等,就是参数麻烦,p,先序的起始点, ib,ie 终须的结束和开始. /** * Definition for binary tree * public class TreeNode { ...
- Javascript数据类型——number类型
ECMAScript规范中使用IEEE754格式来表示整数和浮点数.支持十进制.八进制以及十六进制.有一点注意的是八进制数字在严格模式下是无效的,这可能会影响到程序的正常运行. 避免浮 ...
- phpstudy配置虚拟主机
配置 phpstudy 虚拟主机 1在httpd.conf中 把#Include conf/extra/httpd-vhosts.conf前面的#去掉 2在站点域名管理 添加 要配置的 虚拟主机 添 ...
- PAT 1080. Graduate Admission (30)
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...
- Session案例
用户登入案例: 按一般的网站登入实例,用户在页面登入页输入账号.密码,验证通过后,在首页显示其"欢迎回来,xxx". 首先完成登入页login.html <!DOCTYPE ...
- Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of ...
- Android怎样改动app不在多任务列表中显示
在实际开发中,我们希望某些activity或者应用程序不在多任务列表中显示,即长按Home键或者多任务button键不显示近期执行的程序,我们能够在对应应用程序的AndroidManifest.xml ...
- Kia's Calculation(贪心)
pid=4726">http://acm.hdu.edu.cn/showproblem.php? pid=4726 大致题意:给两个长度小于10^6且相等的合法的正整数.你能够随意组合 ...
- thinkphp连接oracle
配置文件中: //Oracle 测试环境 'DB_TYPE' => 'Oracle', // 数据库类型 'DB_HOST' => '1 ...