C++ primer plus (第6版)第七章编程练习答案

第一题

#include<iostream>

const double d_2_0 = 2.0;

double har_mony(double x, double y);
//double(*harmony)(double x, double y) = har_mony;

int main()
{
	using namespace std;
	double har,x,y;

	cout << fixed;
	cout.precision(2);
	cout.setf(ios_base::showpoint);

	while ((cin >> x >> y))
	{
		if (x == 0 || y == 0)
			break;
		//cout << harmony(x, y) << endl;
		cout << har_mony(x, y) << endl;
	}

	cout << "Done";
	return 0;
}

double har_mony(double x, double y)
{
	return d_2_0 * x * y / (x + y);
}

第二题

#include<iostream>
//using namespace std
const int num{ 10 };

int input(double[], int);//输入函数
void print(double[], int);//显示函数
void average(double[], int);//平均值函数


int main()
{
	using namespace std;
	double golf[num];
	int size = input(golf,num);
	print(golf, size);
	average(golf, size);

	return 0;
}

int input(double golf[], int size)
{
	int i;

	for (i = 0; i < size; i++)
	{
		if (!(std::cin >> golf[i]))
			break;
	}

	return i;
}

void print(double golf[], int size)
{
	for (int i = 0; i < size; i++)//while
	{
		std::cout << golf[i] << ' ';
	}
	std::cout << std::endl;
}

void average(double golf[], int size)
{
	double sum = 0;

	for (int i = 0; i < size; i++)
	{
		sum += golf[i];
	}
	std::cout << sum / size << std::endl;
}

第三题

#include<iostream>
using namespace std;

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

void mabox(box);
void ma_box(box*);

int main()
{
	box bo
	{
		"order",35.0,45.0,55.0,66.0
	};
	mabox(bo);
	ma_box(&bo);
	mabox(bo);
	return 0;
}

void mabox(box bo)
{
	cout << bo.maker << ' ' << bo.height
		 << ' ' << bo.width
		 << ' ' << bo.length
		 << ' ' << bo.volume << endl;
	return;
}

void ma_box(box* bo)
{
	bo->volume = bo->height * bo->width * bo->length;
}

第四题

#include<iostream>

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


long double probability(unsigned numbers, unsigned picks,unsigned num)
{
	long double result = 1.0;
	long double n;
	long double sum;
	unsigned p;

	for (n = numbers, p = picks; p > 0; n--, p--)
		result = result * n / p;
	sum = result * num;
	return sum;
}

第五题

#include<iostream>

int fun(int n);

int main()
{
	using namespace std;
	int n;

	cout << "Please enter an integer: ";
	while (cin >> n)
	{
		cout << n << "! = " << fun(n) << endl;
		cout << "Next enter an integer: ";
	}

	return 0;
}

int fun(int n)
{
	if (n == 0 || n == 1)
		return 1;

	return fun(n - 1) * n;
}

第六题

#include<iostream>
using namespace std;

unsigned Fillarray(double[], int);
void Show_array(double[], int);
void Reverse_array(double[], int);

int main()
{
	const int size = 10;
	double fill_array[size];
	int num;

	num = Fillarray(fill_array, size);
	Show_array(fill_array, num);
	Reverse_array(fill_array, num - 1);
	Show_array(fill_array, num);
	cout << num;
	return 0;
}

unsigned Fillarray(double fill_array[], int size)
{
	int count = 0,i = 0;

	while (i < size)
	{
		cout << "Please enter the "  << count + 1 <<" number: ";
		if (cin >> fill_array[i++])
			count++;
		else 
			break;

	}
	return count;
}

void Reverse_array(double fill_array[], int size)
{
	int i;
	int temp;

	for (i = 1; i < --size; i++)
	{
		temp = fill_array[i];
		fill_array[i] = fill_array[size];
		fill_array[size] = temp;
	}
}

void Show_array(double fill_array[], int size)
{
	for (int i = 0; i < size; i++)
		cout << fill_array[i] << ' ';
	cout << endl;
}

第七题

#include<iostream>
const int Max = 5;

double* fill_array(double ar[], double av[]);
void show_array(const double ar[], double av[]);
void revalue(double r, double ar[], double av[]);

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

	double* size_ar = fill_array(properties, properties + Max);
	show_array(properties, size_ar);
	if (properties != size_ar)
	{
		cout << "Enter revaluation factor: ";
		double factor;
		while (!(cin >> factor))
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; Please enter a number: ";
		}
		revalue(factor, properties, size_ar);
		show_array(properties, size_ar);
	}
	cout << "Done.\n";
	cin.get();
	cin.get();
	return 0;
}

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

	return ar;
}


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


void revalue(double r, double ar[], double av[])
{
	while(ar != av)
		*ar++ *= r;
}

第八题

a

#include<iostream>
#include<string>
//constant data
const int Seasons = 4;
const char* Snames[Seasons] =
{ "Spring","Summer","Fill","Winter" };

void fill(double pa[], double da[]);
void show(double da[], double pa[]);

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

void fill(double pa[], double da[])
{
	using namespace std;
	int i = 0;

	while (pa != da && i < Seasons)
	{
		cout << "Enter " << *(Snames + i++) << " expense: ";
		cin >> *pa;
		pa++;
	}
}

void show(double da[], double pa[])
{
	using namespace std;
	double total = 0.0;
	int i = 0;

	cout << "\nEXPENSE\n";
	while (da != pa)
	{
		cout << *(Snames + i++) << ": $" << *da << endl;
		total += *da++;
	}

	cout << "Total Expenses: $" << total << endl;
}

b

#include<iostream>
#include<string>
//constant data
const int Seasons = 4;
const char* Snames[Seasons] =
{ "Spring","Summer","Fill","Winter" };

struct dou
{
	double expense[Seasons];
};

void fill(dou* pa, int size);
void show(dou pa, int size);

int main()
{
	dou exp;
	fill(&exp, Seasons);
	show(exp, Seasons);
	return 0;
}

void fill(dou* pa, int size)
{
	using namespace std;
	int i = 0;

	while (i < size)
	{
		cout << "Enter " << Snames[i] << " expense: ";
		cin >> pa->expense[i++];
	}
}

void show(dou pa, int size)
{
	using namespace std;
	double total = 0.0;
	int i = 0;

	cout << "\nEXPENSE\n";
	while (i < size)
	{
		cout << Snames[i] << ": $" << pa.expense[i] << endl;
		total += pa.expense[i++];
	}

	cout << "Total Expenses: $" << total << endl;
}

第九题

#include<iostream>
using namespace std;
const int SLEN = 30;
struct student
{
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};
//getinfo() 有两个参数:一个指向学生结构数组的第一个元素的指针和一个表示数组元素数的 
//int。该函数请求并存储有关学生的数据。它在填写数组或遇到学生姓名的空行时终止输入。
//该函数返回填充的数组元素的实际数量
int getinfo(student pa[], int n);
//display1() 将学生结构作为参数并显示其内容
void display1(student st);
//display2() 将学生结构的地址作为参数并显示结构的内容
void display2(const student* ps);
//display3() 将学生结构数组的第一个元素的地址和数组元素的数量作为参数,并显示结构的内容
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 < entered; i++)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	display3(ptr_stu, entered);
	delete[]ptr_stu;
	cout << "Done\n";
	return 0;
}

int getinfo(student pa[], int n)
{
	int i = 0;
	int count = 0;

	while (i < n)
	{
		cout << "Please enter the student's name: ";
		//利用其cin的特性 这里不知道为什么不用 cin.getline方法的可以查看下cin读取字符串时的特性
		//输入的时候不要直接输入空格请先输入字符在输入空格
		cin >> pa[i].fullname;			
		if (cin.get() == ' ')			//查看输入队列里是否有空行 有直接停止输入跳出循环
			break;
		cout << "Please enter a hobby: ";
		cin.getline(pa[i].hobby, SLEN);//这里没有限制不需要碰到空行停止
		cout << "Ooplevel: ";
		cin >> pa[i++].ooplevel;
		cin.get();
		count++;
	}

	return count;
}

void display1(student st)
{
	cout << "display1:\n";
	cout << st.fullname << ' '
		 << st.hobby << ' '
		 << st.ooplevel << endl;
}

void display2(const student* ps)
{
	cout << "display2:\n";
	cout << ps->fullname << ' '
		 << ps->hobby << ' '
		 << ps->ooplevel << endl;
}

void display3(const student pa[], int n)
{
	cout << "display3:\n";
	for (int i = 0; i < n; i++)
	{
		cout << i + 1 << ": ";

		cout << pa[i].fullname << ' '
			 << pa[i].hobby << ' '
			 << pa[i].ooplevel << endl;
	}
}

第十题

#include<iostream>


const int SIZE = 3;
const char* str[SIZE]
{ "aaa = ","ass = ","add = " };

double aaa(double x, double y);
double ass(double x, double y);
double add(double x, double y);
double calculate(double x, double y, 
	double(*pf)(double, double));


int main()
{
	using namespace std;
	double x, y;
	double(*pf[SIZE])(double, double) {aaa,ass,add};

	while (cin >> x >> y)
	{
		for (int i = 0; i < SIZE; i++)
			cout << str[i] << calculate(x, y, pf[i]) << endl;
	}

	return 0;
}

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

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

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值