C++ Primer Plus第七章编程练习答案

2.

int fill(int* begin, int*end);
void show(int scole[], int size);

int main()
{
	int scole[ArSize];
	cout << "Enter character to quit\n";
	int size = fill(scole, scole + ArSize);
	show(scole, size);
	system("pause");
	return 0;
}

int fill(int* begin, int*end)
{
	int*pt = begin;
	int temp;
	int count = 0;
	while (pt != end)
	{
		cout << "#" << (count + 1) << ": ";
		cin >> temp;
		if (!cin)
		{
			break;
		}
		*pt = temp;
		pt++;
		count++;
	}
	return count;
}

void show(int scole[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << scole[i] << " ";
	}
	cout << endl;
}

7

double* FillArray(double* begin, double* end);
void ShowArray(double* begin, double* end);
void Revalue(double r, double* begin, double* end);

int main()
{
	double properties[ARSIZE];
	double* termination = FillArray(properties, properties + ARSIZE);
	ShowArray(properties, termination);
	cout << "Enter revaluation factor: ";
	double factor;
	while (!(cin >> factor))
	{
		cin.clear();
		while (cin.get() != '\n')
		{
			continue;
		}
		cout << "Bad input\n"
			<< "Enter a number: ";
	}
	Revalue(0.8, properties, termination);
	ShowArray(properties, termination);
	system("pause");
	return 0;
}

double* FillArray(double* begin, double* end)
{
	double temp;
	double* pt;
	for (pt = begin; pt != end; pt++)
	{
		cout << "Enter value: ";
		cin >> temp;
		if (temp < 0 || !cin )
		{
			cout << "---------Done!--------\n";
			cin.clear();
			while (cin.get() != '\n')
			{
				continue;
			}
			break;
		}
		*pt = temp;
	}
	return pt;
}

void ShowArray(double* begin, double* end)
{
	double* p = begin;
	int count = 0;
	while (p != end)
	{
		cout << "Property " << "#" << (count + 1) << ": $" << *p << endl;
		p++;
		count++;
	}
}

void Revalue(double r, double* begin, double* end)
{
	for (double* pt = begin; pt != end; pt++)
	{
		*pt *= r;
	}
}

8
(a).

const int SEASONS = 4;
//const array<string, SEASONS> Sname = { "Spring", "Summer", "Fall", "Winter" };
const char* Snames[SEASONS] = { "Spring", "Summer", "Fall", "Winter" };

//void fill(ifstream& is, array<double, 4>* p);
//void show(const array<double, 4>* p);
void fill(ifstream& is, double pa[], int size);
void show(const double pa[], int size);

int main()
{

	/*array<double, 4> expenses;*/
	double expenses[SEASONS];
	ifstream fin;
	fin.open("donation.txt");
	if (fin.is_open())
	{
		cout << "成功打开文件!\n";
	}
	else
	{
		exit(EXIT_FAILURE);
	}
	//fill(fin, &expenses);
	//show(&expenses);
	fill(fin, expenses, SEASONS);
	show(expenses, SEASONS);
	fin.close();
	system("pause");
	return 0;
}

/*void fill(ifstream& is, array<double, 4>* p)
{
	for (int i = 0; i < SEASONS; i++)
	{
		is >> (*p)[i];
		cout << "Enter " << Snames[i] << " expenses: " << (*p)[i] << endl;
	}
}*/

void fill(ifstream& is, double pa[], int size)
{
	for (int i = 0; i < size; i++)
	{
		is >> pa[i];
		cout << "Enter " << Snames[i] << " expenses: " << pa[i] << endl;
	}
}

/*void show(const array<double, 4>* p)
{
	double total = 0;
	cout << "EXPENSES\n";
	for (int i = 0; i < SEASONS; i++)
	{
		cout << Snames[i] << ": $" << (*p)[i] << endl;
		total += (*p)[i];
	}
	cout << "Total: $" << total << endl;
}*/

void show(const double pa[], int size)
{
	double total = 0;
	cout << "EXPENSES\n";
	for (int i = 0; i < SEASONS; i++)
	{
		cout << Snames[i] << ": $" << pa[i] << endl;
		total += pa[i];
	}
	cout << "Total: $" << total << endl;
}

(b).

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

//void fill(ifstream& is, array<double, 4>* p);
//void show(const array<double, 4>* p);
void fill(ifstream& is, expense* pt_ex);
void show(const expense* pt_ex);

struct expense
{
	double dollar[SEASONS];
};

int main()
{
    /*array<double, 4> expenses;*/
	expense ex;
	ifstream fin;
	fin.open("donation.txt");
	if (fin.is_open())
	{
		cout << "成功打开文件!\n";
	}
	else
	{
		exit(EXIT_FAILURE);
	}
	fill(fin, &ex);
	show(&ex);
	fin.close();
	system("pause");
	return 0;
}

void fill(ifstream& is, expense* pt_ex)
{
	for (int i = 0; i < SEASONS; i++)
	{
		is >> pt_ex->dollar[i];
		cout << "Enter " << Snames[i] << " expenses: " << pt_ex->dollar[i] << endl;
	}
}

void show(const expense* pt_ex)
{
	double total = 0;
	cout << "EXPENSES\n";
	for (int i = 0; i < SEASONS; i++)
	{
		cout << Snames[i] << ": $" << pt_ex->dollar[i] << endl;
		total += pt_ex->dollar[i];
	}
	cout << "Total: $" << total << endl;
}

9

struct student
{
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};
int getinfo(student pa[], int size);
void display1(student st);
void display2(student* st);
void display3(student pa[], int n);

int main()
{
	cout << "Enter class size: ";
	int size_class;
	cin >> size_class;
	while (cin.get() != '\n')
	{
		continue;
	}							//等下要使用函数cin.getline()面向行输入,得先清空输入队列中的坏数据
	student* ptr_stu = new student[size_class];
	int entered = getinfo(ptr_stu, size_class);
	for (int i = 0; i < entered; i++)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	display3(ptr_stu, entered);
	delete[] ptr_stu;
	system("pause");
	return 0;
}

int getinfo(student pa[], int size)
{
	int i;
	char temp;
	cout << "-------Enter a blank line for student name to quit-------\n";
	for (i = 0; i < size; i++)
	{
		cout << "#" << (i + 1) << "student: \n";
		cout << "name: ";
		cin.getline(pa[i].fullname, SLEN);
		if (pa[i].fullname[0] == ' ' && pa[i].fullname[1] == ' ')
		{
			cout << "Bye!\n";
			break;
		}
		cout << "hobby: ";
		cin.getline(pa[i].hobby, SLEN);
		cout << "ooplevel: ";
		cin >> pa[i].ooplevel;
		while (cin.get() != '\n')
		{
			continue;
		}                           //read bad input,包括换行符。
	}
	return i;
}

void display1(student st)
{
	cout << "======This is display1======\n";
	cout << "fullname|hobby|ooplevel\n";
	cout << st.fullname << "  " << st.hobby << "  " << st.ooplevel << endl;
}

void display2(student* st)
{
	cout << "======This is display2======\n";
	cout << "fullname|hobby|ooplevel\n";
	cout << st->fullname << "  " << st->hobby << "  " << st->ooplevel << endl;
}

void display3(student pa[], int n)
{
	cout << "======This is display3======\n";
	for (int i = 0; i < n; i++)
	{	
		cout << "#" << (i + 1) << endl;
		cout << pa[i].fullname << "  " << pa[i].hobby << "  " << pa[i].ooplevel << endl;
	}
}

10
(1)

double calculate(double x, double y, double(*pf)(double, double));
double add(double x, double y);
double multiply(double x, double y);

int main()
{
	double x, y;
	while (cin >> x >> y)
	{
		cout << calculate(x, y, add) << endl;
		cout << calculate(x, y, multiply) << endl;
		if (!cin)
		{
			break;
		}
	}
	system("pause");
	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 multiply(double x, double y)
{
	return x * y;
}

(2)

	double(*pf[2])(double, double) = { add, multiply };
	for (int i = 0; i < 2; i++)
	{
		cout << calculate(2.5, 10.4, pf[i]) << endl;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值