C++ Primer Plus 第六版(中文版)课后编程题----第七章

7.1

<span style="font-size:18px;">#include <iostream>
using namespace std;

double Harmonics(double, double);

int main()
{
	double a, b;
	cout << "Please enter two numbers: ";
	while (cin >> a >> b && a != 0 && b != 0)
	{
		cout << "The harmonics average of " << a << " and " << b
			<< " is " << Harmonics(a, b) << ".\n";
		cout << "Next two numbers: ";
	}
	cout << "Done!\n";
	system("pause");
	return 0;
}

double Harmonics(double x, double y)
{
	return (2.0*x*y / (x + y));
}</span>


7.2

<span style="font-size:18px;">#include <iostream>
using namespace std;
const int MaxSize = 10;

int InArr(double *, int);
void Display(const double *, int);
double Average(const double *, int);

int main()
{
	int k;
	double score[MaxSize];

	k = InArr(score, MaxSize);
	Display(score, k);
	cout << "The average score is: " << Average(score, k) << endl;
	system("pause");
	return 0;
}

int InArr(double * arr, int size)
{
	int i;
	cout << "Please enter the scores(q to quit): " << endl;
	for (i = 0; i < size; ++i)
	{
		if (!(cin >> arr[i]))
			break;
	}
	arr[i] = '\0';
	cout << "Enter done!!!\n";
	return i;
}

void Display(const double * arr, int size)
{
	cout << "Now display the array: \n";
	for (int i = 0; i < size; ++i)
		cout << arr[i] << " ";
	cout << endl;
}

double Average(const double * arr, int size)
{
	double ave = 0;
	for (int i = 0; i < size; ++i)
		ave += arr[i];
	ave = ave / size;
	return ave;
}</span>


7.3

<span style="font-size:18px;">#include <iostream>
using namespace std;

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

void Display(box);	//按值传递 box 结构并显示其成员值
void Modify(box *);	//按地址传递 box 结构并修改成员volume

int main()
{
	box box1;
	cout << "Please enter the information of the box: " << endl;
	cout << "Maker: ";
	cin.get(box1.maker, 40);
	cout << "Height: ";
	cin >> box1.height;
	cout << "Width: ";
	cin >> box1.width;
	cout << "Length: ";
	cin >> box1.length;
	cout << "Volume: ";
	cin >> box1.volume;

	cout << "The original box is: " << endl;
	Display(box1);

	Modify(&box1);
	cout << "Now the box is: " << endl;
	Display(box1);

	system("pause");
	return 0;
}

void Display(box a)
{
	cout << "The maker is: " << a.maker << endl
		<< "The height is: " << a.height << endl
		<< "The width is: " << a.width << endl
		<< "The length is: " << a.length << endl
		<< "The volume is: " << a.volume << endl;
}

void Modify(box *a)
{
	a->volume = a->height*a->width*a->length;
}</span>


7.4

#include <iostream>
using namespace std;

const int MaxSize = 5;
long double Probability(unsigned numbers, unsigned picks);

int main()
{
	unsigned total_field, total_special, choice_field, choice_special;

	cout << "Please enter the field number and field choice: ";
	while (!(cin >> total_field >> choice_field) || total_field < choice_field)
		cout << "Wrong enter, please again: ";
	cout << "Please enter the special number and choice: ";
	while (!(cin >> total_special >> choice_special) || total_special < choice_special)
		cout << "Wrong enter, please again: ";

	cout << "The probability of hitting the jackpot is: " 
		<< (Probability(total_field, choice_field)*Probability(total_special, choice_special)) << endl;
	system("pause");
	return 0;
}

long double Probability(unsigned numbers, unsigned picks)
{
	long double res = 1.0;
	for (int i = numbers, j = picks; j>0; --i, --j)
		res = res*i / j;
	return res;
}


7.5

#include <iostream>
using namespace std;

long factorial(int);

int main()
{
	int n;
	cout << "Please enter a number: ";
	while (!(cin >> n) || n < 0)
	{
		cout << "Wrong enter, please enter again: ";
		cin.clear();
		cin.sync();
	}
		
	cout << n << "! = " << factorial(n) << endl;
	system("pause");
	return 0;
}

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


7.6

#include <iostream>
#include <cctype>
using namespace std;

const int MaxSize = 5;
int Fill_array(double *, int);
void Show_array(const double *, int);
void Reverse_array(double *, int);

int main()
{
	double arr[MaxSize];
	int n = Fill_array(arr, MaxSize);
	cout << n << " elements wan entered!" << endl;
	Show_array(arr, n);

	Reverse_array(arr, n);
	Show_array(arr, n);

	if (n <= 2)
		cout << "The element is less than 3, we can not reverse it without the first and last one." << endl;
	else
	{
		Reverse_array(arr + 1, n - 2);
		Show_array(arr, n);
	}

	system("pause");
	return 0;	
}

int Fill_array(double * arr, int num)
{
	int i;
	cout << "Please enter numbers(q to quit): ";
	for (i = 0; i < num; ++i)
	{
		if (!(cin >> arr[i]))
			break;
	}
	arr[i] = '\0';
	return i;
}

void Show_array(const double * arr, int num)
{
	for (int i = 0; i < num; ++i)
		cout << arr[i] << " ";
	cout << endl;
}

void Reverse_array(double * arr, int num)
{
	double temp;
	for (int i = 0; i < num / 2; ++i)
	{
		temp = arr[i];
		arr[i] = arr[num - i - 1];
		arr[num - i - 1] = temp;
	}
}


7.7

#include <iostream>
using namespace std;

const int Max = 5;
double * fill_array(double *, double *);
void show_array(double *, double *);  // don't change data
void revalue(double, double *, double *);

int main()
{
	double properties[Max];

	double *p_size = fill_array(properties, properties + Max);
	show_array(properties, p_size);
	if (p_size > 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, p_size);
		show_array(properties, p_size);
	}
	cout << "Done.\n";

	system("pause");
	return 0;
}

double * fill_array(double *p, double *q)
{
	double *p_temp;
	int k = 1;
	for (p_temp = p; p_temp != q; ++p_temp)
	{
		cout << "Enter value #" << (k++) << ": ";
		cin >> *p_temp;
		if (!cin)    // bad input
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; input process terminated.\n";
			break;
		}
		else if (*p_temp < 0)     // signal to terminate
			break;
	}
	return p_temp;
}

// the following function can use, but not alter,
// the array whose address is ar
void show_array(double *p, double *q)
{
	int k = 1;
	for (double *p_temp = p; p_temp != q;++p_temp)
	{
		cout << "Property #" << (k++) << ": $";
		cout << *p_temp << endl;
	}
}

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


7.8

a:

#include <iostream>
#include <string>
using namespace std;

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

void fill(double *arr, int n);
void show(const double *arr, int n);
int main()
{
	double expenses[Seasons];

	fill(expenses, Seasons);
	show(expenses, Seasons);
	system("pause");
	return 0;
}

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

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


b:

#include <iostream>
#include <string>
using namespace std;

const int Seasons = 4;
const string Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
struct expenses
{
	double e;
};

void fill(expenses *arr, int n);
void show(const expenses *arr, int n);
int main()
{
	expenses arr[Seasons];

	fill(arr, Seasons);
	show(arr, Seasons);
	system("pause");
	return 0;
}

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

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


7.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);
	cout << endl;
	for (int i = 0; i < entered; ++i)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	cout << endl;
	display3(ptr_stu, entered);

	delete[] ptr_stu;
	cout << "Done!\n";
	system("pause");
	return 0;
}

int getinfo(student pa[], int n)
{
	int i;
	cout << "Please enter the informations of students: " << endl;
	for (i = 0; i < n; ++i)
	{
		cout << "Enter #" << i+1 << " student's information: \n";
		cout << "Fullname: ";
		cin.getline(pa[i].fullname, SLEN);
		if (strlen(pa[i].fullname) == 0)
			break;
		cout << "Hobby: ";
		cin.getline(pa[i].hobby, SLEN);
		cout << "Ooplevel: ";
		(cin >> pa[i].ooplevel).get();
	}
	
	return i;
}

void display1(student st)
{
	cout << "Fullname: " << st.fullname 
		<< "\tHobby: " << st.hobby 
		<< "\tOoplevel: " << st.ooplevel 
		<< endl;
}

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

void display3(const student pa[], int n)
{
	for (int i = 0; i < n; ++i)
		cout << "Fullname: " << pa[i].fullname
		<< "\tHobby: " << pa[i].hobby
		<< "\tOoplevel: " << pa[i].ooplevel
		<< endl;
}


7.10

关于函数指针还是陌生的很,写这个程序费了些周折,还是需要深入了解。

#include <iostream>
using namespace std;

double add(double x, double y);
double dev(double x, double y);
double mul(double x, double y);
double calculate(double x, double y, double (*p[])(double x, double y));

int main()
{
	double x, y;
	double (*p[3])(double, double) = { add, dev, mul };
	cout << "Please enter two numbers(q to exit): ";
	while (cin >> x >> y)
	{
		cout << "The result is: " << calculate(x, y, p) << endl;
		cout << "Next two numbers(q to exit): ";
	}
	cout << "Done!\n";

	system("pause");
	return 0;
}

double add(double x, double y)
{
	return x + y;
}
double dev(double x, double y)
{
	return x - y;
}
double mul(double x, double y)
{
	return x*y;
}

double calculate(double x, double y, double (*p[3])(double x, double y))
{
	return p[0](x, y) + p[1](x, y) + p[2](x, y);
}















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值