C++ Primer Plus习题及答案-第四章

习题选自:C++ Primer Plus(第六版)
内容仅供参考,如有错误,欢迎指正 !
文章首发微信公众号:iDoitnow

  • cin使用空白(空格、制表符和换行符)来确定字符串的结束位置(空格、制表符和换行符仍会留在输入队列)。(这意味着cin在获取字符数组输入时只读取一个单词,读取该单词后,cin将该字符串放到数组中,并自动在结尾添加空字符。注意当输入的是数字的时候,输入流中会自动忽略空格回车等控制字符。只有当输入的是字符时,才会保留’\n’在输入流中。)
  • getline()函数每次读取一行,他通过换行符来确定行尾,但不保存换行符,在存储的字符串的时候,它用空字符替换换行符(换行符不会留在输入队列)。
  • get()具体工作方式与getline()类似,但get并不在读取并丢弃换行符,而是将其留在输入队列中。(幸运的是get()有另外一种变体,使用不带任何参数的cin.get()调用读取下一个字符(即使是换行符),因此可以用它来处理换行符)

复习题

1.如何声明下述数据?

a. actor是由30个char组成的数组
b. betsie是由100个short组成的数组
c. chuck是由13个float组成的数组
d. dipsea是由64个long double组成的数组

char actor[30];
short betsie[100];
float chuck[13];
long double dipsea[64];
2.使用模板类array而不是数组来完成1的问题。
array<char,30>actor;
array<short,100>betsie;
arry<float,13>chuck;
array<long double,64>dipsea;
3.声明一个包含5个元素的int数组,并将他们初始化为前5个正奇数。
int a[5]={1,3,5,7,9};
4.编写一条语句,将问题3中数组第一个元素和最后一个元素的和赋值给变量even。
int a[5]={1,3,5,7,9};
int even=a[0]+a[4];
5.编写一条语句,显示float数组ideas中的第二个元素的值。
cout<<ideas[1]<<endl;
6.声明一个char的数组,并将其初始化为字符串“cheeseburger”。
char a[]="cheeseburger";
7.声明一个string对象,并将其初始化为字符串“Waldorf Salad”。
string a="Waldorf Salad";
8.设计一个描述鱼的结构声明,结构中应当包括品种、重量(整数,单位为盎司)和长度(英寸,包括小数)。
struct fish{
    char kind[20];
    int weight;
    float length;
}
9.声明8中定义的结构变量,并对她进行初始化。
fish Xfish=
{
    "xiaoyu";
    10;
    20.5;
}
10.用enum定义一个名为Response的类型,它包含Yes、No和Maybe等枚举量,其中Yes的值为1,No为0,Maybe为2.
enum Response{No,Yes,Maybe};
11.假设ted是一个double变量,请声明一个指向ted的指针,并使用该指针来显示ted的值。
double *p=&ted;
cout<<*p<<endl;
12.假设treacle是一个包含10个元素的float数组,请声明一个指向treacle的第一个元素的指针,并使用该指针来显示数组的第一元素和最后一个元素。
float *p=treacle;
cout<<p[0]<<" "<<p[9]<<endl;
//or cout<<*p<<"  "<<*(p+9) ;
13.编写一段代码,要求用户输入一个正整数,然后创建一个动态int数组,其中包含的元素数目等于用户的输入值,首先使用new来完成这项任务,再使用vector对象完成这项任务。
//use new
int num;
cout<<"Please input a positive integer: ";
cin>>num;
int *shu=new int[num];

//use vector
int num;
cout<<"Please input a number: ";
cin>>num;
vector<int>shu(num);
14.下面的代码是否有效,如果有效,他将输出什么结果。

cout<<(int*)"Home of the jolly bytes";
有效,输出该字符串的地址。

15.编写一段代码,给问题8中的结构动态分配内存,再读取该结构的成员的值。
fish yu=new fish{xiaoyu,10,20.5};
cout<<yu.name<<endl;
cout<<yu.weight<<endl;
cout<<yu.length<<endl;
16.程序清单4.6指出了混合输入数字和一行字符串存储的问题,如果将下面的代码:

cin.getline(adress,80);替换为:cin>>address;将对程序带来什么影响?
使用cin>>address 将使得程序跳过空白,直到找到给空白字符为止。然后它将读取字符,直到再次遇到空白为止。因此,它将跳过数字输入的换行符,从未避免这种问题。另一方面,它值读取一个单词,而不是整行。

17.声明一个vector对象和一个array对象,他们都包含10个string对象。指出所需的头文件,但不要使用using,使用const来指定要包含的string对象数。
#include<vector>
#include<array>
#include<string>
const int str_num=10;
std::vector<std::string>vstr(str_num);
std::array<<std::string,str_num>astr;

编程练习

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

What is your first name? Betty Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name:Yewe,Betty Sue
Grade:C
Age:22

(注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档。)
#include<iostream>
#include<string>
int main()
{
	using namespace std;
	cout << "What your first name? ";
	string first_name;
	getline(cin,first_name) ;

	cout << "What your last name? ";
	string last_name;
	getline(cin,last_name);

	cout << "What letter grade do you deserve? ";
	char  my_grade;
	cin >> my_grade ;

	cout << "What is you age? ";
	int age;
	cin >> age ;

	cout << "Name: " << last_name << "," << first_name << endl;
	cout << "Grade: " << ++my_grade<< endl;
	cout << "Age: " << age << endl;

	system("pause");
	return 0;
}
2.修改程序清单4.4,使用c++string类而不是char数组。
#include <iostream>
#include<string>
int main()
{
	using namespace std;
	string name;
	string dessert;

	cout << "Enter your name:\n";
	getline(cin, name);  // reads through newline
	cout << "Enter your favorite dessert:\n";
	getline(cin,dessert);
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";

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

Enter your first name: Flip
Enter your last name: Fleaming
Here's the information in a single string: Fleming, Flip

#include<iostream>
#include<cstring>
//#pragma warning(disable:4996)  //vs2017需要加上这句话,忽略4996警告
const int Strlen = 20;
int main()
{
	using namespace std;
	char first_name[Strlen];
	char last_name[Strlen];
	char full_name[2 * Strlen];

	cout << "Enter your first name: ";
	cin.get(first_name,Strlen).get();

	cout << "Enter your last name:";
	cin.get(last_name,Strlen).get();

	strcpy(full_name,last_name);
	strcat(full_name,", ");
	strcat(full_name, first_name);

	cout << "Here's the information in a single string: " << full_name << endl;
	system("pause");
	return 0;
}
4.编写一个程序,他要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和一个空格将姓和名组合起来,并存储和显示结合效果,请使用string对象和文件string中的函数。下面是该程序的运行时的情形:

Enter your first name: Flip
Enter your last name: Fleaming
Here's the information in a single string: Fleming, Flip

#include<iostream>
#include<string>
int main()
{
	using namespace std;
	string first_name;
	string last_name;
	string full_name;
	
	cout << "Enter your first name: ";
	getline(cin,first_name);

	cout << "Enter your last name:";
	getline(cin, last_name);

	full_name = last_name + ", " + first_name;

	cout << "Here's the information in a single string: " << full_name << endl;
	system("pause");
	return 0;
}
5.结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandBar变量,并将其成员分别初始化为“Mocha Munch”、2.3和350。初始化在声明snack时进行。最后,程序显示snack变量的内容。
#include<iostream>
#include<string>
struct CandyBar {
	std::string brand;
	float weight;
	int calorie;
};
int main()
{
	using namespace std;
	CandyBar snack = {"Mocha Munch",2.3,350};
	cout << "brand: " << snack.brand << endl;
	cout << "weight: " << snack.weight << endl;
	cout << "calorie: " << snack.calorie << endl;
	
	system("pause");
	return 0;
}
6.结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将他们初始化为所选择的值,然后显示每个结构的内容。
#include<iostream>
#include<string>
struct CandyBar {
	std::string brand;
	float weight;
	int calorie;
};
int main()
{
	using namespace std;
	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 << "brand: " << snack[i].brand << endl;
		cout << "weight: " << snack[i].weight << endl;
		cout << "calorie: " << snack[i].calorie << endl;
	}
	
	system("pause");
	return 0;
}
7.William Wingate从事披萨分析服务。对于每个披萨饼,他都需要记录下列信息:
  • 披萨饼公司的名称,可以有多个单词组成。
  • 披萨饼的直径
  • 披萨饼的重量
    请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。
#include<iostream>
#include<string>
struct Pizza {
	std::string company;
	double diameter;
	double weight;
};
int main()
{
	using namespace std;
	Pizza pizza;
	cout << "Please input pizza's company: ";
	getline(cin, pizza.company);
	cout << "Please input pizza's diameter: ";
	cin>>pizza.diameter;
	cout << "Please input pizza's weight: ";
	cin>> pizza.weight;
	
	cout << "Company: " << pizza.company << endl;
	cout << "Diameter: " <<pizza.diameter << endl;
	cout << "Weight: " << pizza.weight<< endl;
	
	system("pause");
	return 0;
}
8.完成编程练习7,但使用new来为结构动态分配内存,而不是声明一个结构变量。另外,让程序在请求输入披萨名称之前输入披萨直径。
#include<iostream>
#include<string>
struct Pizza {
	std::string company;
	double diameter;
	double  weight;
};
int main()
{
	using namespace std;
	Pizza *p=new Pizza;

	cout << "Please input p's diameter: ";
	cin >> p->diameter;
	cin.get();
	cout << "Please input p's company: ";
	getline(cin, p->company);
	cout << "Please input p's weight: ";
	cin>>p->weight;
	
	cout << "Company: " << p->company << endl;
	cout << "Diameter: " <<p->diameter << endl;
	cout << "Weight: " << p->weight<< endl;
	
	
	system("pause");
	return 0;
}
9.完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar的数组。
#include<iostream>
#include<string>
struct CandyBar {
	std::string brand;
	double weight;
	int calorie;
};
int main()
{
	using namespace std;
	CandyBar *snack = new CandyBar[3];
	for (int i = 0; i < 3; i++)
		snack[i]={"Mocha Munch", 2.3, 350};
		
	for (int i = 0; i < 3; i++)
	{
		cout << "brand: " << snack[i].brand << endl;
		cout << "weight: " << snack[i].weight << endl;
		cout << "calorie: " << snack[i].calorie << endl;
	}
	delete[] snack;
	system("pause");
	return 0;
}
10.编写一个程序,让用户输入三次40码跑的成绩(如果你愿意,也可以让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)。
#include<iostream>
#include<array>
int main()
{
	using namespace std;
	array<double, 3>grade;

	cout << "Please input your first grade:";
	cin >> grade[0];
	cout << "Please input your second grade:";
	cin >> grade[1];
	cout << "Please input your third grade:";
	cin >> grade[2];

	cout << "Average three times:" << (grade[0] + grade[1] + grade[2]) / 3.0 << endl;
	system("pause");
	return 0;
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艰默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值