第四章编程练习

2022/1/31 22:40:46

本文主要是介绍第四章编程练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.编写一个C++程序,如下述输出示例所示的那样请求并显示消息:
What is your first name? Betty Sue
What is your last name? Yewe
What letter grage do you deserve? B
What is your age? 22
Name: Yewe, Betty Sue
Grade : C
Age : 22
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。
假设用户请求A、B或C,所以不必担心D和F之间的空挡。

const int size = 20;
char first_name[size], last_name[size];
char grade;
int age;

cout << "What is your first name?";
cin.getline(first_name,size);
cout << "What is your last name?";
cin.getline(last_name, size);
cout << "What letter grade do you deserve?";
cin >> grade;
cout << "What is your age?";
cin >> age;

cout << "Name: " << last_name << ", " << first_name << endl;
cout << "Grade: " << (char)(grade + 1) << endl;
cout << "Age: " << age;

2.修改程序清单4.4,使用C++string类而不是char数组。

//程序清单4.4
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];

cout << "Enter your name: " << endl;
cin.getline(name, ArSize);
cout << "Enter your favourite dessert: " << endl;
cin.getline(dessert, ArSize);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";

修改后

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;
cout << " for you, " << name << ".\n";

3.编写一个程序,它要求用户首先输入其名,然后输入其姓,然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用char数组和头文件cstring中的函数。下面是该程序运行时的情形:
Enter your first name : Flip
Enter your last name : Fleming
Here’s the information in a single string: Fleming, Flip

const int SIZE = 20;
char first_name[SIZE];
char last_name[SIZE];
char full_name[2*SIZE];

cout << "Enter your first name: "; 
cin.getline(first_name, SIZE);
cout << "Enter your last name: ";
cin.getline(last_name, SIZE);

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;

4.编写一个程序,它要求用户首先输入其名,再输入其姓,然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行时的情形:
Enter your first name : Flip
Enter your last name : Fleming
Here’s the information in a single string: Fleming, Flip

string first_name, last_name, 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;

5.结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化为"Mocha Munch"、2.3和350。初始化应在声明snack时进行。最后程序显示snack变量的内容。

struct CandyBar
{
	char brand[20];
	float weight;
	unsigned int calorie;
};

int main()
{
	using std::cin;
	using std::cout;
	using std::endl;

	CandyBar snack = { "Mocha Munch", 2.3, 350};
	cout << "Brand: " << snack.brand << ", Weight: " << snack.weight << ", Calorie: " << snack.calorie << endl;

	return 0;
}

6.结构体CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将他们初始化为所选择的值,然后显示每个结构的内容。

CandyBar snack[3] = {{ "Mocha Munch", 2.3, 350 }, {"Name2", 3.2, 500}, {"Name3", 4.3, 600} };
	
cout << "1st Brand: " << snack[0].brand << ", ";
cout << "1st Weight: " << snack[0].weight << ", ";
cout << "1st Calorie: " << snack[0].calorie << endl;
cout << "---------------------------" << endl;

cout << "2nd Brand: " << snack[1].brand << ", ";
cout << "2nd Weight: " << snack[1].weight << ", ";
cout << "2nd Calorie: " << snack[1].calorie << endl;
cout << "---------------------------" << endl;

cout << "3th Brand: " << snack[2].brand << ", ";
cout << "3th Weight: " << snack[2].weight << ", ";
cout << "3th Calorie: " << snack[2].calorie << endl;

7.William Wingate从事比萨饼分析服务。对于每个比萨饼,他都需要记录下列信息:
比萨饼公司的名称,可以有多个单词组成。
比萨饼的直径。
比萨饼的重量。
请设计一个能够存储这些信息的结构体,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。

struct Pizza
{
	char company[20];
	float diameter;
	float weight;
};

int main()
{
	using std::cin;
	using std::cout;
	using std::endl;

	Pizza dinner;

	cout << "Please enter the Pizza's company: ";
	cin.getline(dinner.company, 20);
	cout << "Please enter the size of pizza in inches: ";
	cin >> dinner.diameter;
	cout << "Please enter the weight of pizza in pounds: ";
	cin >> dinner.weight;

	cout << "Pizza company:" << dinner.company << ", diameter in inches:" << dinner.diameter << ", weight in pounds:" << dinner.weight << ".\n";

	return 0;
}

8.完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司之前输入比萨饼的直径。

Pizza *pt = new Pizza;

cout << "Please enter the Pizza's company: ";
cin.getline(pt->company, 20);
cout << "Please enter the size of pizza in inches: ";
cin >> pt->diameter;
cout << "Please enter the weight of pizza in pounds: ";
cin >> pt->weight;

cout << "Pizza company:" << pt->company << ", diameter in inches:" << pt->diameter << ", weight in pounds:" << pt->weight << ".\n";

delete pt;

9.完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。

CandyBar* pt = new CandyBar[3];// = { "Mocha Munch", 2.3, 350 };
strcpy(pt[0].brand, "Mocha Munch");
pt[0].weight = 2.3;
pt[0].calorie = 350;

strcpy(pt[1].brand, "Name2");
pt[1].weight = 3.2;
pt[1].calorie = 500;

strcpy(pt[2].brand, "Name3");
pt[2].weight = 4.3;
pt[2].calorie = 600;

cout << "1st Brand: " << pt->brand << ", ";
cout << "1st Weight: " << pt->weight << ", ";
cout << "1st Calorie: " << pt->calorie << endl;
cout << "---------------------------" << endl;

cout << "2nd Brand: " << (pt + 1)->brand << ", ";
cout << "2nd Weight: " << (pt + 1)->weight << ", ";
cout << "2nd Calorie: " << (pt + 1)->calorie << endl;
cout << "---------------------------" << endl;

cout << "3th Brand: " << (pt + 2)->brand << ", ";
cout << "3th Weight: " << (pt + 2)->weight << ", ";
cout << "3th Calorie: " << (pt + 2)->calorie << endl;

delete[] pt;

10.编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩),并显示次数和 平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组。)

#include <iostream>
#include <array>

int main()
{
	using std::cin;
	using std::cout;
	using std::endl;
	using std::array;

	array<float, 3> record_list;
	float average;

	cout << "Please enter three records of 40 meters: " << endl;
	cout << "First record: ";
	cin >> record_list[0];
	cout << "Second record: ";
	cin >> record_list[1];
	cout << "Third record: ";
	cin >> record_list[2];

	cout << "1st: " << record_list[0] << "; " << "2nd: " << record_list[1] << "; " << "3rd: " << record_list[2] << endl;

	average = (record_list[0] + record_list[1] + record_list[2]) / 3;
	
	cout << "The average of records is: " << average << ".\n";

	return 0;
}


这篇关于第四章编程练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程