IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++ Primer Plus 第6版 第7章 编程练习答案 -> 正文阅读

[C++知识库]C++ Primer Plus 第6版 第7章 编程练习答案

1. 编写一个程序,不断要求用户输入两个数,直到其中一个为0。对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:调和平均数=2.0*x*y/(x+y)。

分析:子程序harmonic_mean()有两个输入参数,都是double类型,返回值一个,也是double类型,函数的作用是计算调和平均数,主函数main()的作用是提醒用户输入,判断是否为0,调用子函数,显示结果。

// 7-1 计算调和平均数
#include <iostream>
#include <cmath>
double harmonic_mean(double num1, double num2);

int main() {
	using namespace std;
	double num1, num2, res;
	cout << "Enter two numbers: ";
	while (1) {
		while (!(cin >> num1 >> num2)) {
			// bad input
			cin.clear();  // 清除标志位
			while (cin.get() != '\n')
				continue;
			cout << "Please input two numbers: ";
		}

		if (abs(num1 - 0) < 0.00000001 || abs(num2 - 0) < 0.00000001) {
			cout << "Input terminated\nQuit\n";
			break;
		}

		res = harmonic_mean(num1, num2);
		cout << "The harmonic mean of " << num1 << " and " << num2 << " is " << res << endl;
		cout << "Enter two numbers (0 to quit): ";
	}

	return 0;
}

double harmonic_mean(double x, double y) {
	double res;
	res = 2.0 * x * y / (x + y);
	return res;
}

2. 编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请使用三个数组处理函数来分别进行输入、显示和计算平均成绩。

分析:输入成绩函数input_scores(),要求用户输入最多10个成绩,且可以提前结束输入。显示成绩函数show_scores(),在一行上显示成绩。计算平均成绩函数ave_scores()计算平均成绩,最后main()函数报告平均成绩。

// 7-2 10个高尔夫成绩,报告平均成绩
#include <iostream>
const int Size = 10;
int input_scores(double* scores, int n);
void show_scores(const double* scores, int n);
double ave_scores(double* scores, int n);

int main() {
	using namespace std;
	double scores[Size];
	int len = input_scores(scores, Size);
	show_scores(scores, len);
	double ave = ave_scores(scores, len);
	cout << "The average score is " << ave << "." << endl;

	return 0;
}

int input_scores(double* scores, int n) {
	using namespace std;
	int len = 0;
	double score = 0.0;
	cout << "Please enter the golf scores (up to 10 and 'q' to quit): ";
	while (len < 10 && (cin >> score)) {
		scores[len] = score;
		len++;
	}
	return len;
}

void show_scores(const double* scores, int len) {
	using namespace std;
	cout << "The golf scores are: ";
	for (int i = 0; i < len; i++) {
		cout << scores[i] << " ";
	}
	cout << endl;
}

double ave_scores(double* scores, int len) {
	double ave = 0.0, sum = 0.0;
	for (int i = 0; i < len; i++)
		sum += scores[i];
	ave = sum / len;
	return ave;
}

3. 下面是一个结构声明:

struct box?

{

? ? ? ? char maker[40];

? ? ? ? float height;

? ? ? ? float width;

? ? ? ? float length;

? ? ? ? float volume;

};

a. 编写一个函数,按值传递box结构,并显示每个成员的值。

b. 编写一个函数,传递box结构的地址,并将volume成员设置为其他三维长度的成绩。

c. 编写一个使用这两个函数的简单程序。

分析:按值传递结构,显示值函数display()。按地址传递结构,并修改其中数据。

// 7-3 函数传递结构的应用
#include <iostream>
struct box {
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};
void display(box box_1);
void cal(box* box_ptr);

int main() {
	using namespace std;
	box box_1;
	cout << "Please enter the maker of the box(up to 39 characters): ";
	cin.getline(box_1.maker, 39);
	cout << "Please enter the height of the box: ";
	cin >> box_1.height;
	cout << "Please enter the width of the box: ";
	cin >> box_1.width;
	cout << "Please enter the length of the box: ";
	cin >> box_1.length;
//	cout << "Please enter the volume of the box: ";
//	cin >> box_1.volume;
	cal(&box_1);
	display(box_1);
	return 0;
}

void display(box box_1) {
	using namespace std;
	cout << "The maker of the box is " << box_1.maker << endl;
	cout << "The height of the box is " << box_1.height << endl;
	cout << "The width of the box is " << box_1.width << endl;
	cout << "The length of the box is " << box_1.length << endl;
	cout << "The volume of the box is " << box_1.volume << endl;
}

void cal(box* box_ptr) {
	box_ptr->volume = box_ptr->height * box_ptr->length * box_ptr->width;
}

4. 许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩法的变体。在这些玩法中,玩家从一组被称为域号码(field number)的号码中选择几个。例如,可以从域号码1~47中选择5个号码;还可以从第二区间(如1~27)选择一个号码(称为特选号码)。要赢得头奖,必须正确猜中所有的号码。中头奖的几率是选中所有域号码的几率和从27个号码中正确选择1个号码的几率的乘积。请计算中得这种彩票头奖的几率。

// 7-4 彩票中奖概率
#include <iostream>
long double probability(unsigned numbers, unsigned picks, unsigned special_numbers);

int main() {
	using namespace std;
	unsigned total, choices, special_nums;
	cout << "Enter the total number of choices on the game card and the number of picks allowed: ";
	while ((cin >> total >> choices) && (total >= choices)) {
		cout << "Enter the number of the special numbers: ";
		cin >> special_nums;
		cout << "You have one chance in " << probability(total, choices, special_nums) << " of winning.\n";
		cout << "Next two numbers (q to quit): ";
	}

	return 0;
}

long double probability(unsigned numbers, unsigned picks, unsigned special_numbers) {
	long double res = 1.0;  //浮点表示法防止溢出
	unsigned n;
	unsigned p;
	for (n = numbers, p = picks; p > 0; p--, n--) {
		res = res * n / p;
	}
	res *= special_numbers;
	return res;
}

5. 定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。0!=1。通用计算公式为:如果n大于零,则n! = n*(n-1)!。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。

// 7-5 计算阶乘
#include <iostream>
long recursion(unsigned n);

int main() {
	using namespace std;
	cout << "Enter the number: ";
	int num;
	while (1) {
		if (!(cin >> num)) {
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Please enter a number: ";
			continue;
		}
		if (num < 0) {
			cout << "Please enter a positive number: ";
			continue;
		}
		cout << "The value obtained by calculating the factorial is " << recursion(num) << endl;
		cout << "Enter the another number (q to quit): ";
	}

	return 0;
}

long recursion(unsigned n) {
	if (n == 0)  return 1;
	return n * recursion(n - 1);
}

6. 编写一个程序,它使用下列函数:

Fill_array()将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中。当数组被填满或者用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。

Show_array()将一个double数组的名称和长度作为参数,并显示该数组的内容。

Reverse_array()将一个double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。

程序将使用这些函数来填充数组,然后显示数组;反转数组,然后再显示数组;反转数组中第一个和最后一个元素之外的所有元素,然后显示数组。

// 7-6 翻转数组
#include <iostream>
const unsigned Size = 10;
int fill_array(double* arr, int n);
void show_array(const double* arr, int n);
void reverse_array(double* arr, int n);

int main() {
	using namespace std;
	double arr[Size];
	int len = fill_array(arr, Size);
	show_array(arr, len);
	cout << "Now we reverse the array: \n";
	reverse_array(arr, len);
	show_array(arr, len);
	reverse_array(arr, len);
	cout << "Now we reverse part of the array: \n";
	reverse_array(arr + 1, len - 2);
	show_array(arr, len);

	return 0;
}

int fill_array(double* arr, int n) {
	using namespace std;
	cout << "Please enter the numbers in the array (up to 10): ";
	double num;
	int len = 0;
	while (len < n && cin >> num) {
		arr[len] = num;
		len++;
	}
	return len;
}
void show_array(const double* arr, int n) {
	using namespace std;
	cout << "The number in the array is: ";
	for (int i = 0; i < n; i++)
		cout << arr[i] << " ";
	cout << endl;
}

void reverse_array(double* arr, int n) {
	double tmp = 0.0;
	for (int i = 0; i < n / 2; i++) {
		tmp = arr[i];
		arr[i] = arr[n - i - 1];
		arr[n - i - 1] = tmp;
	}
}

7. 修改程序清单7.7中的3个数组处理函数,使之使用两个指针参数来表示区间。fill_array()函数不返回实际读取了多少个数字,而是返回一个指针,该指针指向最后被填充的位置;其他的函数可以将该指针作为第二个参数,以标识数据结尾。

下面为程序清单7.7

// 程序清单7.7
#include <iostream>
const int Max = 5;
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n);
void revalue(double r, double ar[], int n);

int main() {
	using namespace std;
	double properties[Max];

	int size = fill_array(properties, Max);
	show_array(properties, size);
	if (size > 0) {  //判断数组里是否有数,很重要
		cout << "Enter revaluation factor: ";
		double factor;
		while (!(cin >> factor)) {
			// bad input
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; Please enter a number: ";
		}
		revalue(factor, properties, size);
		show_array(properties, size);
	}
	cout << "Done.\n";
	cin.get();
	cin.get();
	return 0;
}

int fill_array(double ar[], int limit) {
	using namespace std;
	double temp;
	int i;
	for (i = 0; i < limit; i++) {
		cout << "Enter value #" << (i + 1) << ": ";
		cin >> temp;
		if (!cin) {
			// bad input
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; input process terminated.\n";
			break;
		}
		else if (temp < 0)  //signal to terminate
			break;
		ar[i] = temp;
	}
	return i;
}

void show_array(const double ar[], int n) {
	using namespace std;
	for (int i = 0; i < n; i++) {
		cout << "Property #" << (i + 1) << ": $";
		cout << ar[i] << endl;
	}
}

// multiplies each element of ar[] by r
void revalue(double r, double ar[], int n) {
	for (int i = 0; i < n; i++)
		ar[i] *= r;
}

下面为双指针传递数组:

// 7-7 双指针传递数组
#include <iostream>
const int Max = 5;
double* fill_array(double* begin, double* end);
void show_array(double* begin, double* end);
void revalue(double r, double* begin, double* end);

int main() {
	using namespace std;
	double properties[Max];

	double* len_ptr = fill_array(properties, properties + Max - 1);
	show_array(properties, len_ptr);
	if (len_ptr != properties) {  //判断数组里是否有数,很重要
		cout << "Enter revaluation factor: ";
		double factor;
		while (!(cin >> factor)) {
			// bad input
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; Please enter a number: ";
		}
		revalue(factor, properties, len_ptr);
		show_array(properties, len_ptr);
	}
	cout << "Done.\n";
	cin.get();
	cin.get();
	return 0;
}

double* fill_array(double* begin, double* end) {
	using namespace std;
	double temp;
	double* ptr;
	for (ptr = begin; ptr != end+1; ptr++) {
		cout << "Enter value #" << (ptr - begin + 1) << ": ";
		cin >> temp;
		if (!cin) {
			// bad input
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; input process terminated.\n";
			break;
		}
		else if (temp < 0)  //signal to terminate
			break;
		*ptr = temp;
	}
	return ptr;
}

void show_array(double* begin, double* end) {
	using namespace std;
	for (double* ptr = begin; ptr != end; ptr++) {
		cout << "Property #" << (ptr - begin + 1) << ": $";
		cout << *ptr << endl;
	}
}

// multiplies each element of ar[] by r
void revalue(double r, double* begin, double* end) {
	for (double* ptr = begin; ptr != end; ptr++)
		(*ptr) *= r;
}

8. 在不使用array类的情况下完成程序清单7.15所做的工作。编写两个这样的版本:

a. 使用const char* 数组存储表示季度名称的字符串,并使用double数组存储开支。

b. 使用const char* 数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员——一个用于存储开支的double数组。这种设计与使用array类的基本设计类似。

程序清单7.15:

// 程序清单7.15 -- arrobj.cpp
#include <iostream>
#include <array>
#include <string>
using namespace std;

// const data
const int Seasons = 4;
const array<string, Seasons> Snames = { "Spring", "Summer", "Fall", "Winter" };

// function to modify array object
void fill(array<double, Seasons>* pa);
// function that uses array object without modifying it
void show(array<double, Seasons> da);

int main() {
	array<double, Seasons> expenses;
	fill(&expenses);
	show(expenses);
	return 0;
}

void fill(array<double, Seasons>* pa) {
	for (int i = 0; i < Seasons; i++) {
		cout << "Enter " << Snames[i] << " expenses: ";
		cin >> (*pa)[i];
	}
}

void show(array<double, Seasons> da) {
	double total = 0.0;
	cout << "\nEXPENSES\n";
	for (int i = 0; i < Seasons; i++) {
		cout << Snames[i] << ": $" << da[i] << endl;
		total += da[i];
	}
	cout << "Total Expenses: &" << total << endl;
}

a 版本:

// 7-8 a
#include <iostream>
// #include <array>
#include <string>
using namespace std;

// const data
const int Seasons = 4;
//const array<string, Seasons> Snames = { "Spring", "Summer", "Fall", "Winter" };
const char* Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };

// function to modify array object
void fill(double* pa, int n);
// function that uses array object without modifying it
void show(const double* da, int n);

int main() {
	double expenses[Seasons];
	fill(expenses, Seasons);
	show(expenses, Seasons);
	return 0;
}

void fill(double* pa, int n) {
	for (int i = 0; i < n; i++) {
		cout << "Enter " << Snames[i] << " expenses: ";
		cin >> pa[i];
	}
}

void show(const double* da, int n) {
	double total = 0.0;
	cout << "\nEXPENSES\n";
	for (int i = 0; i < n; i++) {
		cout << Snames[i] << ": $" << da[i] << endl;
		total += da[i];
	}
	cout << "Total Expenses: &" << total << endl;
}

b 版本

// 7-8 b
#include <iostream>
// #include <array>
#include <string>
using namespace std;

// const data
const int Seasons = 4;
//const array<string, Seasons> Snames = { "Spring", "Summer", "Fall", "Winter" };
const char* Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
struct expen {
	double d_expen[Seasons];
};

// function to modify array object
void fill(expen* pa);
// function that uses array object without modifying it
void show(expen da);

int main() {
	struct expen expenses;
	fill(&expenses);
	show(expenses);
	return 0;
}

void fill(expen* pa) {
	for (int i = 0; i < Seasons; i++) {
		cout << "Enter " << Snames[i] << " expenses: ";
		cin >> pa->d_expen[i];
	}
}

void show(expen da) {
	double total = 0.0;
	cout << "\nEXPENSES\n";
	for (int i = 0; i < Seasons; i++) {
		cout << Snames[i] << ": $" << da.d_expen[i] << endl;
		total += da.d_expen[i];
	}
	cout << "Total Expenses: &" << total << endl;
}

9. 这个练习让您编写处理数组和结构的函数。下面是程序的框架,请提供其中描述的函数,以完成该程序。

// 7-9
#include <iostream>
using namespace std;
const int SLEN = 30;
struct student {
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};
/*
  getinfo() has two arguments : a pointer to the first element of
  an array of student structures and an int representing the
  number of elements of the array. The function solicits and
  stores data about students. It terminates input upon filling
  the array or upon encountering a blank line for the student
  name. The function returns the actual number of array elements filled.
*/ 
int getinfo(student pa[], int n);

// display1() takes a student structure as an argument and displays its contents
void display1(student st);

// display2() takes the address of student structure as an
// argument and displays the structure's contents
void display2(const student* ps);

// display3() takes the address of the first element of an array
// of student structures and the number of array elements as
// arguments and displays the contents of the structures
void display3(const student pa[], int n);

int main() {
	cout << "Enter class size: ";
	int class_size;
	while (! (cin >> class_size)) {
		cin.clear();
		while (cin.get() != '\n')
			continue;
		cout << "Please enter number: ";
	}
	
	// important
	while (cin.get() != '\n')
		continue;
	student* ptr_stu = new student[class_size];
	int entered = getinfo(ptr_stu, class_size);
	for (int i = 0; i < entered; i++) {
		cout << "\n";
		cout << "The NO." << i + 1 << " stuent's message is \n";
		display1(ptr_stu[i]);
		cout << endl;
		display2(&ptr_stu[i]);
	}
	cout << endl;
	display3(ptr_stu, entered);
	delete[] ptr_stu;
	cout << "Done\n";
	return 0;
}

int getinfo(student pa[], int n) {
	int i = 0;
	for (i = 0; i < n; i++) {
		cout << "Enter the NO." << i + 1 << " student's message: \n";
		cout << "Fullname (blank line to quit): ";  // 输入空格停止这里有待改正
		cin.getline(pa[i].fullname, SLEN);
		cout << "Hobby: ";
		cin.getline(pa[i].hobby, SLEN);
		cout << "Ooplevel: ";
		cin >> pa[i].ooplevel;
		while (cin.get() != '\n')
			continue;
	}
	return i;
}

void display1(student st) {
	cout << "Fullname: " << st.fullname << endl;
	cout << "Hobby: " << st.hobby << endl;
	cout << "Ooplevel: " << st.ooplevel << endl;
}

void display2(const student* ps) {
	cout << "Fullname: " << ps->fullname << endl;
	cout << "Hobby: " << ps->hobby << endl;
	cout << "Ooplevel: " << ps->ooplevel << endl;
}

void display3(const student pa[], int n) {
	for (int i = 0; i < n; i++) {
		cout << "The NO." << i + 1 << " stuent's message is \n";
		cout << "Fullname: " << pa[i].fullname << endl;
		cout << "Hobby: " << pa[i].hobby << endl;
		cout << "Ooplevel: " << pa[i].ooplevel << endl;
	}
}

10. 设计一个名为calculate()的函数,它接受两个double值和一个指向函数的指针,而被指向的函数接受两个double参数,并返回一个double值。calculate()函数的类型也是double,并返回被指向的函数使用calculate()的两个double参数计算得到的值。例如,假设add()函数的定义如下:

double add(double x, double y){

? ? ? ? return x + y;

}

则下述代码中的函数调用将导致calculate()把2.5和10.4传递给add()函数,并返回add()的返回值(12.9):

double q = calculate(2.5, 10.4, add);

请编写一个程序,它调用上述两个函数和至少另一个与add()类似的函数。该程序使用循环来让用户成对地输入数字。对于每对数字,程序都使用calculate()来调用add()和至少一个其他的函数。如果读者爱冒险,可以尝试创建一个指针数组,其中的指针指向add()样式的函数,并编写一个循环,使用这些指针连续让calculate()调用这些函数。提示:下面是声明这种指针数组的方式,其中包含三个指针:

double (*pf[3]) (double, double);

可以采用数组初始化语法,并将函数名作为地址来初始化这样的数组。

(1)

// 7-10
#include <iostream>
using namespace std;
double add(double x, double y);
double multiple(double x, double y);
double calculate(double x, double y, double (*ptr)(double, double));


int main() {
	int x, y;
	double res1, res2;
	cout << "Enter two numbers: ";
	while (cin >> x >> y) {
		res1 = calculate(x, y, add);
		cout << "The result 1 is " << res1 << endl;
		res2 = calculate(x, y, multiple);
		cout << "The result 2 is " << res2 << endl;
		cout << "Enter two another numbers (q to quit): ";
	}

	return 0;
}

double add(double x, double y) {
	return x + y;
}

double multiple(double x, double y) {
	return x * y;
}

double calculate(double x, double y, double (*ptr)(double, double)) {
	return ptr(x, y);
}

(2)

// 7-10
#include <iostream>
using namespace std;
const int Size = 2;
double add(double x, double y);
double multiple(double x, double y);
double calculate(double x, double y, double (*ptr)(double, double));
double (*pf[Size]) (double, double) = { add, multiple };

int main() {
	int x, y;
	double res[Size];
	cout << "Enter two numbers: ";
	while (cin >> x >> y) {
		for (int i = 0; i < Size; i++) {
			res[i] = calculate(x, y, pf[i]);
			cout << "The result " << i + 1 << " is " << res[i] << endl;
		}
		cout << "Enter two another numbers (q to quit): ";
	}

	return 0;
}

double add(double x, double y) {
	return x + y;
}

double multiple(double x, double y) {
	return x * y;
}

double calculate(double x, double y, double (*ptr)(double, double)) {
	return (ptr)(x, y);
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-07 11:49:05  更:2021-08-07 11:50:09 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/9 19:22:07-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码