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, ...
随机推荐
- 各个平台 如何安装 Ruby 和 RubyGems
原文地址:http://cloudfoundry-doc.csdn.net/frameworks/ruby/installing-ruby.html Last Updated: 2012-11-01 ...
- bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...
- 初学python(print使用、条件分支、循环、模块引用)
import random """ #查看源代码日后爬虫用 import urllib.request # coding=utf-8 url = "http:/ ...
- 30分钟Git命令入门到放弃
git 现在的火爆程度非同一般,它被广泛地用在大型开源项目,团队开发,以及独立开发者,甚至学生之中. 初学者非常容易被各种命令,参数吓哭.但实际上刚上手你并不需要了解所有命令的用途.你可以从掌握一些简 ...
- webapp 慎用setInterval、setTimeout
其实这片文章刚开始我啥也没写的,但也有20多的访问量,所以觉得大家还是比较关注这个问题,所以找机会写下. 问题的引出: 为什么我说的时webapp中慎用setInterval.setTimeout, ...
- Android学习路线图
第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和StringBuffer的使用.正则表达式. 3.面向对象的抽象,封装,继承,多态,类与对象,对象初始化 ...
- KVM虚拟化技术
KVM虚拟化技术 Qemu-kvm kvm virt-manager VNC Qemu-kvm创建和管理虚拟机 一.KVM简介 KVM(名称来自英语:Kernel-basedVirtual Machi ...
- freemaker的基本语法
先来解释一下freemaker的基本语法了,<# ... > 中存放所有freemaker的内容,之外的内容全部原样输出.<@ ... /> 是函数调用两个定界符内的内容中,第 ...
- 编码实战Web端联系人的增删改查
首先画出分析图 实现效果如图 项目下的包如图: 实体包 package com.contactSystem.entiey; public class Contact { private String ...
- 关于T-SQL重编译那点事,WITH RECOMPILE和OPTION(RECOMPILE)区别仅仅是存储过程级重编译和SQL语句级重编译吗
本文出处:http://www.cnblogs.com/wy123/p/6262800.html 在考虑重编译T-SQL(或者存储过程)的时候,有两种方式可以实现强制重编译(前提是忽略导致重编译的 ...