c++ primer plus第六版第四章课后编程练习

  1. 编写程序实现一问一答式输入输出,最后输出个人信息
//练习4.1	编写程序实现四次问答输入,再输出
#include<iostream>
#include<string>
using namespace std;
const int max = 30;
int main() {
	char str1[max], str2[max];
	char grade; int age;
	cout << "What is your first name?";
	cin.getline(str1,max);
	cout << "What is your last name?";
	cin.getline(str2,max);
	cout << "What letter grade do you deserve?";
	cin >> grade;
	cout << "What is your age?";
	cin >> age;
	grade++;
	
	cout << "Name:" << str2 << ',' << str1 << endl;
	cout << "Grade" << grade << endl;
	cout << "Age:" << age << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

  1. 改写程序清单4.4,使用string对象而非char数组
//练习4.2	以string改写程序清单4.4
#include<iostream>
#include<string>
using namespace std;
//const int max = 30;
int main() {
	string str1, str2;
	cout << "Enter your name:";
	getline(cin, str1);
	cout << "Enter your favourite dessert:";
	getline(cin, str2);
	cout << "I have some delicious " << str2 << " for you," << str1 << '.' << endl;
	system("pause");
	return 0;
}
  1. 使用char数组和cstring中的函数实现先输其名再输其姓并使用逗号和空格隔开存储,最后展示输出。
//练习4.3	分别输入姓和名,然后使用逗号和空格隔开,进行存储和显示
#include<iostream>
#include<cstring>
//#include<string>
using namespace std;
const int max = 10;
int main() {
	char surname[max], given_name[max];
	char spa[2]{ ' ' }; char si[2]{ ',' };
	char name[30];
	cout << "Enter your surname:" << endl;
	cin.getline(surname, max);
	cout << "Enter your given name:" << endl;
	cin.getline(given_name, max);
	strcpy(name, given_name);
	strcat(name, si); strcat(name, spa);
	strcat(name, surname);
	cout << "Here is the information in a single string: " << name << endl;
	system("pause");
	return 0;
}

在这个部分可以说充分体现数组处理字符串的满满恶意,并且vs2015还给你报备’strcpy’: This function or variable may be unsafe. Consider using strcpy_s instead这样的错误。
解决:
【项目属性】-【预处理】-添加_CRT_SECURE_NO_WARNINGS
这里只在vs2015进行调试,其他的还没试过,如果不符请见谅。
在这里插入图片描述

  1. 改写练习3,使用string对象和对应函数。
//练习4.4	分别输入名和姓,然后使用逗号和空格隔开,进行存储和显示,用string
#include<iostream>
//#include<cstring>
#include<string>
using namespace std;
//const int max = 10;
int main() {
	string surname, given_name, name;
	cout << "Enter your given name:" << endl;
	getline(cin, given_name);
	cout << "Enter your surname:" << endl;
	getline(cin, surname);
	name = given_name + ", ";
	name = name + surname;
	cout << "Here is the information in a single string: " << name << endl;
	system("pause");
	return 0;
}

和例题3一样的输入输出,就不展示结果了。

  1. 编写结构体CandyBar包含:糖块品牌、糖块重量和糖块卡路里含量,初始化成员snack:“Mocha Munch”、2.3和350,最后显示成员内容。
//练习4.5	定义结构体CandyBar包含品牌名、重量(浮点型)、卡路里(整型),创建变量snack并初始化输出。
#include<iostream>
//#include<cstring>
#include<string>
using namespace std;
//const int max = 10;
struct CandyBar {
	string brand;
	float weight;
	int calorie;
};
int main() {
	CandyBar snack {"Mocha Munch",2.3,350};
	cout << "The brand of the candy is:"<<snack.brand;
	cout << " and it is "<<snack.weight<<" grams and has "<<snack.calorie<<" calorie."<<endl;
	system("pause");
	return 0;
}

简单结构体成员初始化并显示信息,这里不做展示。

  1. 在习题5的基础上,成员添加至三个,初始化并显示
//练习4.6	定义结构体CandyBar包含品牌名、重量(浮点型)、卡路里(整型),创建结构体数组snack并初始化,用for循环输出。
#include<iostream>
//#include<cstring>
#include<string>
using namespace std;
//const int max = 10;
struct CandyBar {
	string brand;
	float weight;
	int calorie;
};
int main() {
	CandyBar snack[3]{ "Mocha Munch",2.3,350,"sweet",1.3,300,"c.c",3,400 };
	for (int i = 0; i < 3; i++){
		cout << "The brand of the candy is:" << snack[i].brand;
		cout << " and it is " << snack[i].weight << " grams and has " << snack[i].calorie << " calorie." << endl;
}
	system("pause");
	return 0;
}
  1. 编写存储披萨分析结构体,包含以下信息:披萨公司名称、披萨直径和披萨重量。用户可以进行信息输入,最后输出披萨信息。
//练习4.7	定义结构体PizzaData包含品牌名、重量(浮点型)、直径浮点型),创建结构体数组p[3]并初始化,用for循环输入输出。
#include<iostream>
//#include<cstring>
#include<string>
using namespace std;
//const int max = 10;
struct PizzaData {
	string brand;
	float diameter;
	float weight;
};
int main() {
	PizzaData p[3];
	for (int j = 0; j < 3; j++) {
		cout << "The brand of the pizza company:";
		cin >> p[j].brand;
		cout << "The diameter and the weight is:";
		cin >> p[j].diameter>>p[j].weight;
	}
	for (int i = 0; i < 3; i++){
		cout << "The brand of the pizza company is:" << p[i].brand;
		cout << " and it is " << p[i].diameter << " centimeters and has " << p[i].weight << " kilograms." << endl;
	}
	system("pause");
	return 0;
}
  1. 在习题7的基础上进行改动,建立动态结构数组并允许用户输入信息,最后输出披萨信息。
//练习4.8	定义结构体PizzaData包含品牌名、重量(浮点型)、直径浮点型),创建结构体数组p[3]并初始化,用for循环输入输出。
#include<iostream>
//#include<cstring>
#include<string>
using namespace std;
//const int max = 10;
struct PizzaData {
	string brand;
	float diameter;
	float weight;
};
int main() {
	PizzaData *p = new PizzaData[3];
	for (int i = 0; i<3; i++) {
		cout << "The brand of the pizza company:";
		getline(cin, (p + i)->brand);
		cout << "The diameter is:";
		cin >> (p + i)->diameter;
		cout << "The weight of the pizza is:";
		cin >> (p + i)->weight;
		cin.get();
	}
	for (int j = 0; j<3; j++) {
		cout << "The brand of the pizza company is:" << p[j].brand <<'.'<< endl;
		cout << "It is " << p[j].diameter << " centimeters and has " << p[j].weight << " kilograms." << endl;
	}
	delete[] p;
	system("pause");
	return 0;
}

注意动态结构的建立需要在后面释放空间,不然会导致系统失常。另外,字符串和数字交叉输入需要考虑空白的处理,不然很容易出现意料之外的输出。这也是我在第一个for循环结尾加上cin.get()的原因。后面的图片和代码实现会有点格式上的不同,因为我临时改了一下代码,但图片用原来的,将就着看吧啊。
在这里插入图片描述

  1. 重写练习6,使用动态数组来进行分配。
//练习4.9	用new来分配动态数组,重写4.6。
#include<iostream>
//#include<cstring>
#include<string>
using namespace std;
//const int max = 10;
struct CandyBar {
	string brand;
	double weight;
	int calorie;
};
int main() {
	CandyBar *p = new CandyBar[3]
	{
		{ "small",10.1,100 },
		{ "middle", 20.55, 200 },
		{ "big", 30.99, 300 },

	};
	for (int i = 0; i<3; i++) {
		cout << "The brand of the candy company is:" << p[i].brand<<'.'<<endl;
		cout << "It is " << p[i].weight << " grams and has " << p[i].calorie << " calorie." << endl;
	}
	delete[] p;
	system("pause");
	return 0;
}

在这里插入图片描述

  1. 用array类来创建array对象,用来存储用户输入的三次40米跑成绩,按次输出成绩和平均成绩。
//练习4.10	编写完成三次40米跑成绩,显示次数和平均成绩,用array实现。
#include<iostream>
#include<array>
using namespace std;

int main() {
	array<double, 3>grade;
	double temp = 0;
	for (int i = 0; i<3; i++) {
		cout << "Please enter the grade:";
		cin >> grade[i];
		temp = grade[i] + temp;
	}
	for (int j = 0; j < 3; j++) {
		cout << "The grade #" << j + 1 << ": " << grade[j] << endl;
	}
	temp = temp / 3;
	cout << "The average value of grades is :" << temp << endl;
	system("pause");
	return 0;
}

这一章对于我个人的重点还是穿插输入不同类型数据对于输入流和存储数据的影响,比如用cin.get()来处理空白,使用cin.getline()等来处理成行输入字符串,这都很重要。还有就是最后的动态结构体、动态数组的建立所对应的内存的使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值