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 << "
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值