C++PrimerPlus学习——第七章编程练习

感觉变困难了很多,必须要注意细节,不如就会出各种bug
7-1

#include <iostream>
double average(double a, double b);

int main()
{
	using namespace std;
	double a, b;
	a = b = 0;
	cout << "Enter ta number:"  << endl;
	cin >> a;
	cout << "Enter another number: " << endl;
	while (cin >> b && b != 0)
	{
		double c = average(a, b);
		cout << "调和平均为" << c << endl;
		cout << "Enter another number: " << endl;
		a = b;
	}
	return 0;
}

double average(double x, double y)
{
	double c;
	c = 2.0 * x * y / (x + y);
	return c;
}

7-2
发现while (std::cin >> si[length] && length <10)在输入10个值的时候会无法退出,改为添加了一个break终止输入

#include <iostream>
struct box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};
int scoreinput(double si[]);
void scoreoutput(double so[], int length);
void averagescore(double as[], int length);

int main()
{
	double score[10];
	int a = 0;
	a = scoreinput(score);
	scoreoutput(score, a);
	averagescore(score, a);
	return 0;
}

int scoreinput(double si[])
{
	int length = 0;
	std::cout << "Please enter the record: (non-number to stop)\n";
	while (std::cin >> si[length])
	{
		length++;
		if (length >= 10)
			break;
	}
	return length;
}
void scoreoutput(double so[], int length)
{
	std::cout << "The scores are ";
	for (int i = 0; i < length; i++)
		std::cout << so[i] << " ";
	std::cout << std::endl;
}
void averagescore(double as[], int length)
{
	double sum = 0;
	for (int i = 0; i < length; i++)
		sum += as[i];
	std::cout << "平均成绩为:" << sum / length << std::endl;
}

7-3

#include <iostream>
struct box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};
void displaybox(box boxs);
void setvolume(box* boxs);

int main()
{
	box *box1 = new box;
	std::cout << "Please enter box maker and box info(height, width and length): \n";
	std::cin.get(box1->maker,40);
	std::cin >> (*box1).height >> (*box1).width >> (*box1).length;
	setvolume(box1);
	displaybox(*box1);
	delete box1;
	return 0;
}
void displaybox(box boxs)
{
	std::cout << "The box maker is " << boxs.maker << ".\n";
	std::cout << "The height, width, and length of the box is " << boxs.height << ", " << boxs.width << ", and " << boxs.length << ".\n";
	std::cout << "The volume of the box is " << boxs.volume << ".\n";
}
void setvolume(box* boxs)
{
	boxs->volume = boxs->height * boxs->width * boxs->length;
}

7-4

#include <iostream>
long double probability(unsigned int numbers, unsigned int pick);

int main()
{
	using std::cout;
	using std::cin;
	unsigned int n1, n2, p1, p2;
	cout << "输入普通号码池数量:\n";
	cin >> n1;
	cout << "输入普通号码选择数量:\n";
	cin >> p1;
	cout << "输入特殊号码池数量:\n";
	cin >> n2;
	cout << "输入特殊号码选择数量:\n";
	cin >> p2;
	long double probability_1 = probability(n1, p1);
	long double probability_2 = probability(n2, p1);
	cout << "中头奖概率为:" << 1/(probability_1 * probability_2);
	return 0;
}
long double probability(unsigned int numbers, unsigned int pick)
{
	long double result = 1.0;
	long double n;
	unsigned p;
	for (n = numbers, p = pick; p > 0; n--, p--)
		result = result * n / p;
	return result;
}

7-5

#include <iostream>
long double factorial(unsigned int n);

int main()
{
	unsigned int n;
	std::cout << "输入一个正整数:\n";
	std::cin >> n;
	long double result;
	result = factorial(n);
	std::cout << n << "! = " << result << "\n";
	return 0;
}
long double factorial(unsigned int n)
{
	if (n == 0 || n == 1)
		return 1;
	else
		return n * factorial(n - 1);
}

7-6

#include <iostream>
int Fill_aray(double array[], int length);
void Show_array(double array[], int length);
void Reverse_array(double array[], int length);

int main()
{
	int n = 20;
	double array[20];
	n = Fill_aray(array, n);
	Show_array(array, n);
	Reverse_array(array, n);
	std::cout << "逆转后的";
	Show_array(array, n);
	Reverse_array(array, n);
	Reverse_array(array+1, n - 2);
	std::cout << "逆转除第一项和最后一项得到的";
	Show_array(array, n);
	return 0;
}
int Fill_aray(double array[], int length)
{
	int i = 0;
	std::cout << "请输入数字:(输入非数字终止)\n";
	while (std::cin >> array[i])
	{
		i++;
		if (i >= length)
		{
			std::cout << "数组已被填满\n";
			break;
		}
	}
	return i;
}
void Show_array(double array[], int length)
{
	std::cout << "数组为:";
	for (int i = 0; i < length; i++)
	{
		 std::cout << array[i] << " ";
	}
	std::cout << std::endl;
}
void Reverse_array(double array[], int length)
{
	double temp;
	for (int i = 0; i < length / 2; i++)
	{
		temp = array[i];
		array[i] = array[length - i - 1];
		array[length - i - 1] = temp;
	}
}

7-7
要理解指针的意义

#include <iostream>
const int Max = 5;
double* fill_array(double* begin, double* end);
void show_array(double* begin, double* end);
void revalue(double r, double* begin, double* end);

int main()
{
	using namespace std;
	double properties[Max];
	double* end = fill_array(properties, properties + Max);
	show_array(properties, end);
	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, end);
	show_array(properties, end);
	cout << "Done.\n";
	cin.get();
	cin.get();
	return 0;
}

double* fill_array(double* begin, double* end)
{
	using namespace std;
	double* i;
	int j = 0;
	for (i = begin; i != end; i++)
	{
		cout << "Enter value #" << (j + 1) << ":";
		cin >> *i;
		if (!cin)
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; input process terminated.\n";
			break;
		}
		else if (*i < 0)	
			break;
		j++;
	}
	return i;
}
void show_array(double* begin, double* end)
{
	using namespace std;;
	int j = 0;
	for (double* i = begin; i != end; i++)
	{
		cout << "Property#" << (j + 1) << ": $";
		cout << *i << endl;
		j++;
	}
}
void revalue(double r, double* begin, double* end)
{
	for (double* i = begin; i != end; i++)
		(*i) *= r;
}

7-8

//(a)
#include <iostream>
#include <array>
#include <string>

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

void fill(double ex[]);
void show(double ex[]);

int main()
{
	double expenses[4];
	fill(expenses);
	show(expenses);
	return 0;
}

void fill(double ex[])
{
	using namespace std;
	for (int i = 0; i < Seasons; i++)
	{
		cout << "Enter " << Snames[i] << " expenses: ";
		cin >> ex[i];
	}
}

void show(double ex[])
{
	using namespace std;
	double total = 0.0;
	cout << "\nEXPENSES\n";
	for (int i = 0; i < Seasons; i++)
	{
		cout << Snames[i] << ": $" << ex[i] << endl;
		total += ex[i];
	}
	cout << "Total Expenses: $" << total << endl;
}
//(b)
#include <iostream>
#include <array>
#include <string>

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

struct expense
{
	double exp[Seasons];
};

void fill(expense* ex);
void show(expense* ex);

int main()
{
	expense* totalExpenses = new expense;
	fill(totalExpenses);
	show(totalExpenses);
	delete totalExpenses;
	return 0;
}

void fill(expense* ex)
{
	using namespace std;
	for (int i = 0; i < Seasons; i++)
	{
		cout << "Enter " << Snames[i] << " expenses: ";
		cin >> ex->exp[i];
	}
}

void show(expense* ex)
{
	using namespace std;
	double total = 0.0;
	cout << "\nEXPENSES\n";
	for (int i = 0; i < Seasons; i++)
	{
		cout << Snames[i] << ": $" << ex->exp[i] << endl;
		total += ex->exp[i];
	}
	cout << "Total Expenses: $" << 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);
	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)
{
	using namespace std;
	int i;
	for (i = 0; i < n; i++)
	{
		cout << "Student # " << i + 1 << "\n";
		cout << "fullname: ";
		cin.getline(pa[i].fullname, SLEN);
		cout << "hobby: ";
		cin.getline(pa[i].hobby, SLEN);
		cout << "ooplevel: ";
		cin >> pa[i].ooplevel;
		cin.get();
		cout << endl;
	}
	return i;
}
void display1(student st)
{
	using namespace std;
	cout << "display1";
	cout << "\nStudent # " << ":";
	cout << " fullname: " << st.fullname;
	cout << " hobby: " << st.hobby;
	cout << " ooplevel: " << st.ooplevel;
	cout << endl;
}
void display2(const student* ps)
{
	using namespace std;
	cout << "display2";
	cout << "\nStudent #" << ":";
	cout << " fullname: " << ps->fullname;
	cout << " hobby: " << ps->hobby;
	cout << " ooplevel: " << ps->ooplevel;
	cout << endl;
}
void display3(const student pa[], int n)
{
	cout << "display3\n";
	for (int i = 0; i < n; i++)
	{
		cout << "Student #" << i + 1 <<":";
		cout << "\nfullname: " << pa[i].fullname;
		cout << "\nhobby: " << pa[i].hobby;
		cout << "\nooplevel: " << pa[i].ooplevel;
		cout << endl;
	}
}

7-10

#include <iostream>
const int n = 3;
double calculate(double x, double y, double (*pf)(double, double));
double add(double x, double y);
double substract(double x, double y);
double multi(double x, double y);

int main()
{
	using namespace std;
	double x, y;
	double(*pf[n])(double, double) = { add, substract, multi };
	cout << "Enter two numbers: \n";
	while (cin >> x >> y)
	{
		for (int i = 0; i < n; i++)
		{
			double result;
			result = calculate(x, y, pf[i]);
			cout << "第" << i + 1 << "个计算结果为:" << result << endl;
		}
	}
}

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

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

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

double multi(double x, double y)
{
	return x * y;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值