C++ Primer Pluse_7_课后题

#include <iostream>

using namespace std;

double Sum2(double x, double y)
{
	double sum = 0;

	if (x + y < 0.0000000001)
	{
		cout << "x, y 的调和数为无穷大;
";
		system("pause");
		exit(0);
	}
	sum = 2.0*x*y / (x + y);
	return sum;
}

void test7_1()
{
	double x;
	double y;
	double sum = 0;

	while ((cin>>x>>y) && x != 0 && y!=0)
	{
		sum = Sum2(x, y);
		cout << "x,y的调和为: " << sum << endl;
	}
}


/************************************************************************/
/* 2                                                                     */
/************************************************************************/
const int SIZE = 10;
int GetScores(double scores [],int *size)
{
	cout << "Enter the scores( press q to quit).
";
	int i = 0;
	for (i = 0; i < 10 && cin >> scores[i]; i++)
	{
		if (scores[i]<0)
		{
			cout << "score below zero, bad input.
";
			cout << "Input terminated.
";
			return -1;
		}
	}

	*size = i;
	return 0;
}

void Show(const double * scores, int size)
{
	int i = 0;
	for (i = 0; i < size; i++)
	{
		cout << scores[i] << " ";
	}
	cout << endl;
}

double Average(const double *scores, int size)
{
	double sum = 0;

	if (size == 0)
	{
		return 0;
	}
	for (int i = 0; i < size; i ++)
	{
		sum += scores[i];
	}

	return sum/size;
}
void test7_2()
{
	int size = 0;
	double scores[SIZE] = { 0 };

	int ret = 0;
	ret = GetScores(scores, &size);
	while (ret != 0)
	{
		cout << "Please input again.
";
		ret = GetScores(scores, &size);
	}

	Show(scores, size);
	double avg = Average(scores, size);
	cout << "平均成绩为:" << avg << endl;

}

/************************************************************************/
/* 3                                                                     */
/************************************************************************/
typedef struct Box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
}box;

void ShowBox(box b1)
{
	cout << "Showing the box......
";
	cout << b1.maker << endl;
	cout << b1.height << endl;
	cout << b1.width << endl;
	cout << b1.length << endl;
	cout << b1.volume << endl;
}

void GetVol(box * b1)
{
	b1->volume = b1->height *b1->length*b1->width;
}
void test7_3()
{
	box b1 = { "Cat", 10, 2, 3, 0 };
	ShowBox(b1);
	GetVol(&b1);
	ShowBox(b1);
}

/************************************************************************/
/*   4                                                                   */
/************************************************************************/

void test7_4()
{

}
/************************************************************************/
/* 5                                                                     */
/************************************************************************/
long GetFactorial(int n)
{
	if (n == 0 || n == 1)
	{
		return 1;
	}
	else
		return n*GetFactorial(n - 1);

}
void test7_5()
{
	int n = 0;
	cin >> n;
	if (n < 0)
	{
		cout << "bad input.
program terminated.
";
		return;
	}

	cout << n << "! = " << GetFactorial(n)<<endl;

}

void test7_6()
{

}

/************************************************************************/
/* 7                                                                     */
/************************************************************************/
const int Max = 5;
double * full_array(double ar[], int limit)
{
	double *ar_end = ar;
	double tmp = 0;
	for (int i = 0; i < limit; i++, ar_end++)
	{
		cout << "Enter value #" << (i + 1) << ": ";
		cin >> tmp;

		if (!cin)
		{
			cin.clear();
			while (cin.get()!='
')
			{
				continue;
			}

			cout << "Bad input;input process terminated.
";
			break;
		}
		else if (tmp < 0)
		{
			break;
		}
		*ar_end = tmp;
	}

	return --ar_end;
}

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

void revalue(double r, double *ar, double *ar_end)
{
	double *pt = ar;
	for (; pt <= ar_end ; pt++)
	{
		*pt *= r;

	}
}
 
void test7_7()
{
	double properties[Max];
	double *ar_end = NULL;
	ar_end = full_array(properties, Max);
	show_array(properties,ar_end);
	if (ar_end - properties > 0)
	{
		cout << "Enter revaluation factor: ";
		double factor;
		while (!(cin >> factor))
		{
			cin.clear();
			while (cin.get()!= '
')
			{
				continue;
			}
			cout << "Bad input. input process terminated.";
		}
		revalue(factor, properties, ar_end);
		show_array(properties, ar_end);
	}
	cout << "Done.
";
	cin.get();
	cin.get();
}


/************************************************************************/
/* 8                                                                     */
/************************************************************************/
const int Seasons = 4;
const char * Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
void fill(double expenses[], int Seasons)
{
	for (int i = 0; i < Seasons; i++)
	{
		cout << "Enter " << Snames[i] << " expense: ";
		cin >> expenses[i];
	}
}

void show(double expense[], int Seasons)
{
	double total = 0;
	cout << "EXPENSE
";
	for (int i = 0; i < Seasons; i++)
	{
		cout << Snames[i] << ": $" << expense[i] << endl;
		total += expense[i];
	}

	cout << "Total Expense: $" << total << endl;

}
void test7_8()
{
	double expense[Seasons] = { 0 };
	fill(expense, Seasons);
	show(expense, Seasons);
}

/************************************************************************/
/* 9                                                                     */
/************************************************************************/
const int Size = 30;
typedef struct Student{

	char fullname[Size];
	char hobby[Size];
	int ooplevel;

}student;


int getInfo(student pa[], int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		cout << "Student #" << i + 1 << ":
";
		cout << "Enter name: ";
		cin >> pa[i].fullname;
		if (!cin)
		{
			cin.clear();
			while (cin.get()!= '
')
			{
				continue;
			}
			cout << "bad input. input process terminated.
";
		}

		cout << "Enter hobby:";
		cin >> pa[i].hobby;
		cout << "Enter ooplevel:";
		cin >> pa[i].ooplevel;
	}

	return i;
}

void display1(student st)
{
	cout << "Student info:
";
	cout << "name: " << st.fullname << endl;
	cout << "hobby: " << st.hobby << endl;
	cout << "ooplevel" << st.ooplevel << endl;
}

void display2(student *ps)
{
	cout << "Student info:
";
	cout << "name: " << ps->fullname << endl;
	cout << "hobby: " << ps->hobby << endl;
	cout << "ooplevel" << ps->ooplevel << endl;
}

void display3(const student pa[], int n)
{
	int i = 0;
	for (; i < n; i++)
	{
		cout << "Student #" << i + 1<< "info:"<< endl;
		cout << "name: " << pa[i].fullname << endl;
		cout << "hobby: " << pa[i].hobby << endl;
		cout << "ooplevel" << (pa+i)->ooplevel << endl;
	}
}

void test7_9()
{
	cout << "Enter class size: ";
	int class_size;
	cin >> class_size;

	while (cin.get()!= '
')
	{
		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.
";
}
double add(double x, double y)
{
	return x + y;
}

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

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


void test7_10()
{
	double(*calculate[3])(double, double);

	calculate[0] = add;
	calculate[1] = sub;
	calculate[2] = mul;
	for (int i = 0; i < 3; i++)
	{
		cout << calculate[i] << ": "<<calculate[i](5, 2) << endl;
	}
}
int main()
{
	test7_10();
	system("pause");
	return 0;
}

  

原文地址:https://www.cnblogs.com/my-cat/p/6119508.html