C++ Prime Plus 第7章编程练习答案和运行结果

1. 第1题

#include <iostream>

using namespace std;

double haravg(double n1, double n2);

int main()
{
	double n1, n2;//输入的两个数
	double result;//调和平均数
	cout << "请输入两个数字:";
	//确保输入的是数字,并且只要两个值均不为0且不为负数时才进行求调和平均值
	while ((cin >> n1 >> n2) && (n1 > 0) &&  (n2 > 0))
	{
		result = haravg(n1, n2);
		cout << n1 << "和" << n2 << "的调和平均数:" << result << endl;
		cout << "请输入两个数字:";
	}
	cout << "结束!" << endl;
	return 0;
}

//求调和平均数
double haravg(double n1, double n2)
{
	double avg = (2.0*n1*n2) / (n1 + n2);
	return avg;
}

注意

  1. 不能计算两个负数的调和平均数,因此我让两个数都是大于0的,不然计算公式的分母就为0了!
  2. while的循环条件:cin>>n1>>n2 保证输入的是数字。

结果图:
在这里插入图片描述

2 第2题

#include <iostream>

const int MaxSize = 10;//数组元素最多为10

using namespace std;

int Input(double ar[], int MaxSize);
void Show(const double ar[],int n);
double Calavg(const double ar[],int n);

int main()
{
	int num;//个数
	double avg;//平均数
	double sorce[MaxSize];//成绩数组

	cout << "最多输入" << MaxSize << "个高尔夫成绩:" << endl;
	num = Input(sorce, MaxSize);//返回数组输入个数
	Show(sorce, num);//显示
	avg = Calavg(sorce, num);
	cout << "这" << num << "个成绩的平均值为:" << avg << endl;

	return 0;
}

//输入
int Input(double ar[], int MaxSize)
{
	int i;//计数
	double temp;//暂存输入的值
	for (i = 0; i < MaxSize; i++)
	{
		cout << "#" << i + 1<<":";
		cin >> temp;
		//如果输入的不是数字
		if (!temp)
		{
			cin.clear();//重置
			while (cin.get() != '\n')
				continue;
			cout << "输入的不是数字!" << endl;
			break;
		}
		//使用负数来结束输入
		else if (temp < 0)
			break;

		ar[i] = temp;
	}
	return i;//返回数组长度
}

//显示
void Show(const double ar[],int n)
{
	cout << "高尔夫成绩:";
	for (int i = 0; i < n; i++)
	{
		cout << ar[i] << "\t";
	}
	cout << endl;
}

//计算平均值
double Calavg(const double ar[],int n)
{
	double total = 0;
	double avg;
	for (int i = 0; i < n; i++)
	{
		total = total + ar[i];
	}
	avg = total / n;
	return avg;
}

注意

  1. 3个函数中输入函数是最难的,因为他要考虑多个方面,首先是输入的是不是数字,这里使用!temp 来判断;其次输入的数字是不是为负数,当输入为负数时,就结束输入;
  2. 输入函数的参数:输入函数,因为输入的个数是不确定的,因此我们最好把输入的个数作为返回参数,这个返回参数之后作为显示和计算平均值函数的输入参数。

结果图:
在这里插入图片描述

3 第3题

#include<iostream>

using namespace std;

struct box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};

void Show(const box b);
void Calvolume(box *p);

int main()
{
	box b1 = { "MUJI",50,40,100, 200000 };
	box b2 = { "MUJI",50,40,100};//未初始化volume值

	cout << "用值传递:" << endl;
	Show(b1);
	cout << endl;
	cout << "使用指针:" << endl;
	Calvolume(&b2);

	return 0;
}

//使用值传递
void Show(const box b)
{
	cout << "maker:" << b.maker << endl;
	cout << "height:" << b.height << endl;
	cout << "width:" << b.width << endl;
	cout << "length:" << b.length << endl;
	cout << "volume:" << b.volume << endl;
}

//使用指针
void Calvolume(box *p)
{
	p->volume = (p->height)*(p->length)*(p->width);//计算volume
	cout << "maker:" << p->maker << endl;
	cout << "height:" << p->height << endl;
	cout << "width:" << p->width << endl;
	cout << "length:" << p->length << endl;
	cout << "volume:" << p->volume << endl;
}


注意

  1. 输入参数是否要用const:Show()函数因为只是显示其中的内容,最好是不修改结构的内容,因此在输入参数上加上const;而Calvolume()函数是要单独计算体积这个成员变量的,会改变结构的值,因此输入参数不能加const
  2. 使用指针访问结构成员不能用句点运算符,需要用箭头运算符。

结果图:
在这里插入图片描述

4 第4题

#include<iostream>

using namespace std;

long double probability(unsigned numbers, unsigned picks);

int main()
{
	unsigned numbers1, picks1;
	unsigned numbers2, picks2;
	long double p1, p2, p;

	cout << "请输入第一组域号码的总数和你要选择的个数:";

	if (cin >> numbers1 >> picks1 && picks1 < numbers1)
	{
		p1 = probability(numbers1, picks1);
		cout << "域号码1~" << numbers1 << "中正确选取" << picks1 << "个号码的概率为:" << p1 << endl;
	}

	cout << "请输入第二组域号码的总数和你要选择的个数:";

	if (cin >> numbers2 >> picks2 && picks2 < numbers2)
	{
		p2 = probability(numbers2, picks2);
		cout << "域号码1~" << numbers2 << "中正确选取" << picks2 << "个号码的概率为:" << p2 << endl;
	}
	p = p1 * p2;
	cout << "中头奖的概率为:" << p1 << endl;

	return 0;
}

long double probability(unsigned numbers, unsigned picks)
{
	long double result = 1.0;
	for (int i = numbers, j = picks; j > 0; i--, j--)
	{
		result = result * j / i;//书上是错的,没有取倒数!!!
	}
	return result;
}

注意

  1. 书上求概率的函数是错的,甚至书上的运行结果也是错的!哪有概率会大于1的。太荒唐了,也不知道这本书的作者是当时没睡醒还是怎么的,我觉得很不可思议。其实书上还有很多错误,小伙伴们不要被迷惑了。

结果图:

在这里插入图片描述

5 第5题

#include<iostream>

using namespace std;

long long factorial(int n);

int main()
{
	int n;
	long long fac;//阶乘
	cout << "请输入你要求阶乘的整数:";
	while (cin >> n && n >= 0)
	{
		fac = factorial(n);
		cout << n << "! =  " << fac << endl;
		cout << "请输入你要求阶乘的整数:";
	}
	cout << "结束!" << endl;

	return 0;
}

long long factorial(int n)
{
	long long result;
	if (n == 0)
		result = 1;
	else
		result = factorial(n - 1)*n;
	return result;
}

注意

  1. 首先要保证你输入的数是数字并且不为负数;
  2. 递归调用factorial();

结果图:
在这里插入图片描述

6 第6题

#include <iostream>

using namespace std;

int Fill_array(double ar[], int len);
void Show_array(const double ar[], int n);
void reverse_array(double ar[], int n);

const int ArrSize = 10;//数组大小

int main()
{
	double arr[ArrSize];
	int n = Fill_array(arr, ArrSize);
	cout << "原数组为:" << endl;
	Show_array(arr, n);
	reverse_array(arr, n);
	cout << "反转后的数组为:" << endl;
	Show_array(arr, n);
	return 0;
}

int Fill_array(const double ar[], int len)
{
	int i = 0;//计数
	double temp;//暂时存储输入的值
	cout << "请输入double值:" << endl;
	cout << "#" << i + 1 << ": ";
	while (cin >> temp && i != len)
	{
		ar[i] = temp;
		i++;
		cout << "#" << i + 1 << ": ";
	}
	cout << "输入结束!" << endl;
	return i;
}

//显示
void Show_array(double ar[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << ar[i] << "\t";
	}
	cout << endl;
}

void reverse_array(double ar[], int n)
{
	//反转范围从第二个元素到倒数第二个元素
	for (int i = 1, j = n - 2; i < j; i++, j--)
	{
		double temp = ar[i];
		ar[i] = ar[j];
		ar[j] = temp;
	}
}

注意点

  1. Fill_array()中的如何控制数组被填满或者输入非数字后,输入停止:我这里是while(cin>>temp && n != len) 前一个是保证输入是数字,后一个确保计数i不超过len。
  2. reverse_array()中注意反转数组的范围,因为第一个元素和最后一个元素是不反转的。

结果图:

在这里插入图片描述

7 第7题

#include <iostream>

using namespace std;

double * fill_array(double *ar, double *limit);
void show_array(const double *ar, double *end);//const是为了不改变数组的内容
void revalue(double r,double *ar, double *end);

const int Max = 5;

int main()
{
	double properties[Max];
	double *end = fill_array(properties, properties + Max);
	show_array(properties, end);
	//输入元素不为0时
	if (end != properties)
	{
		cout << "Enter revalueation factor:";
		double factor;
		while (!(cin >> factor))
		{
			cin.clear();
			while (!(cin.get() != '\n'))
				continue;
			cout << "Bad input;Please enter a number:";
		}
		revalue(factor, properties, end);//
		show_array(properties, end);
	}
	cout << "Done." << endl;
	cin.get();
	cin.get();
	return 0;
}

double * fill_array(double *ar, double *limit)
{
	double temp;//暂时存放输入的数据
	double *p;//跟随元素增加而移动的指针
	int i;
	for (p = ar,i = 0; p != limit; p++, i++)
	{
		cout << "Enter value #" << (i + 1) << ": ";
		cin >> temp;
		if (!cin)
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input;input process terminated." << endl;
			break;
		}
		else if (temp < 0)
			break;
		*p = temp;
	}
	return p;
}

void show_array(const double *ar, double *end)
{
	const double *p;
	int i;
	for (p = ar,i = 0; p != end; p++,i++)
	{
		cout << "Property #" << (i + 1) << ": $";
		cout << *p << endl;
	}
}

void revalue(double r, double *ar, double *end)
{
	const double *p;
	int i;
	for (p = ar, i = 0; p != end; p++, i++)
	{
		*(ar + i) *= r;
	}
}

注意点

  1. fill_array()函数返回的是指针,因此注意返回的类型应该为double *;
  2. double *end = fill_array(properties, properties + Max); 用到“超尾"的概念,properties + Max 是指向最后一个元素后面的指针!
  3. 其余两个函数中需要用到for循环,注意循环的条件,只要p != end 就表明还未循环到最后一个元素。详细可以看书上220页的内容。

结果图:
在这里插入图片描述

8. 第8题

  1. 第一种版本:
#include <iostream>

using namespace std;

const int Seasons = 4;
const char* Snames[Seasons] = { "Spring","Summer","Fall","Winter" };

void fill(double *arr, int limit);
void show(const double *arr, int limit);

int main()
{
	double expenses[Seasons];//double数组用来存储开支数组
	fill(expenses, Seasons);
	show(expenses, Seasons);
	return 0;
}

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

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

结果图:
第一种做法的结果图2.第二种版本:

#include <iostream>

using namespace std;

const int Seasons = 4;
const char* Snames[Seasons] = { "Spring","Summer","Fall","Winter" };
struct Expenses
{
	double expenses[Seasons];//开支数组
};

void fill(Expenses * e, int limit);
void show(const Expenses * e, int limit);

int main()
{
	Expenses expenses;
	fill(&expenses, Seasons);
	show(&expenses, Seasons);
	return 0;
}

void fill(Expenses * e, int limit)
{
	for (int i = 0; i < limit; i++)
	{
		cout << "Enter " << *(Snames + i) << " expenses: ";
		cin >> e->expenses[i];
	}
}

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

结果图:
在这里插入图片描述

9. 第9题

#include<iostream>

using namespace std;

const int SLEN = 30;
struct student
{
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};

int getinfo(student pa[], int n);//用户输入结构体的信息,并返回实际输入的结构体数组的元素个数
void display1(student st);//显示一个结构体的内容
void display2(const student * ps);//显示一个结构体的内容
void display3(const student pa[], int n);//显示一个结构体数组的内容

int main()
{
	cout << "Enter class size: ";
	int class_size;
	cin >> class_size;
	while (cin.get() != '\n')
		continue;
	student * ptr_stu = new student[class_size];
	int entered = getinfo(ptr_stu, class_size);
	for (int i = 0; i < class_size; i++)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	display3(ptr_stu, entered);
	delete[] ptr_stu;
	cout << "Done." << endl;
	return 0;
}

int getinfo(student pa[], int n)
{
	int count = 0;//计数
	for (int i = 0; i < n; i++)
	{
		cout << "请输入#" << i + 1 << "个学生的信息:" << endl;
		cout << "fullname: ";
		cin.getline(pa[i].fullname,SLEN);
		cout << "hobby: ";
		cin.getline(pa[i].hobby,SLEN);
		cout << "ooplevel: ";
		cin >> pa[i].ooplevel;
		cin.get();//防止混合输入数字和字符串出现问题
		count++;
	}
	cout << "输入结束!" << endl;
	return count;
}

void display1(student st)
{
	cout << "display1的显示结果:" << endl;
	cout << "fullname: " << st.fullname << endl;
	cout << "hobby: " << st.hobby << endl;
	cout << "ooplevel: " << st.ooplevel << endl;
}

void display2(const student * ps)
{
	cout << "display2的显示结果:" << endl;
	cout << "fullname: " << ps->fullname << endl;
	cout << "hobby: " << ps->hobby << endl;
	cout << "ooplevel: " << ps->ooplevel << endl;
}

void display3(const student pa[], int n)
{
	cout << "display3的显示结果:" << endl;
	for (int i = 0; i < n; i++)
	{
		cout << "#" << i + 1 << ": " << endl;
		cout << "fullname: " << pa[i].fullname << endl;
		cout << "hobby: " << pa[i].hobby << endl;
		cout << "ooplevel: " << pa[i].ooplevel << endl;
		cout << endl;
	}
}

结果图:
在这里插入图片描述

10.第10题

#include<iostream>

using namespace std;

double calculate(double d1, double d2, double(*p)(double, double));
double add(double x, double y);
double sub(double x, double y);
double multi(double x, double y);
int main()
{
	double(*pf[3])(double d1, double d2) = { add,sub,multi };
	double d1, d2, value;
	for (int i = 0; i < 3; i++)
	{
		cout << "请输入一对数字:";
		//输入非数字时,提示错误
		while (!(cin>>d1>>d2))
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad Input!" << endl;
		}
		value = calculate(d1, d2, *pf[i]);
		cout << d1 << "和" << d2 << "的运算结果为:" << value << endl;
	}
	return 0;
}

double calculate(double d1, double d2, double(*p)(double, double))
{
	double value = (*p)(d1, d2);
	return value;
}

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

double sub(double x, double y)
{
	return x - y;
}

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

结果图:
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值