现在的位置: 首页 > 综合 > 正文

c++ primer 7.12编程练习

2018年02月20日 ⁄ 综合 ⁄ 共 2595字 ⁄ 字号 评论关闭

4.

/*2014. 01 .11  by Arby*/
/*c++ primer 7.12 编程练习*/
/*错误输入时的情形,要进行探讨*/
#include <iostream>
using namespace std;
long double probability(int arr[], unsigned special, int choice);

int main()
{
	/*初始化*/
	const int choice = 5;
	int field_number[choice];
	int special = 0;
	int i =0;
	long double result = 0;

	/*操作*/
	cout << "enter the number of five between 1 to 47: ";
	for(i = 0; i< choice; i++)
	{	
		while(!(cin>> field_number[i]) || (field_number[i]<1) || (field_number[i] > 47))
		{
			cin.clear();
			while(cin.get() != '\n')
				continue;
			cout << "enter wrong, please enter again: ";
		}
		cout << "Next number is: ";
	}

	cout << "enter the special number from 1 to 27:";
	cin >> special;

	result = probability(field_number, special, choice);
	cout << "the average is: " << result << endl;

	cin.get();
	cin.get();
	return 0;
}

long double probability(int arr[], unsigned special, int choice)
{
	long double result = 0;
	int sum = 0;
	for(int i = 0; i < choice; i++)
	{
		sum = sum + arr[i];
	}
	sum = sum + special;
	result = sum / (choice + 1);

	return result;
}

5.

/*2014. 01 .11  by Arby*/
/*c++ primer 7.12 编程练习*/
/*递归实现*/
#include <iostream>
using namespace std;
int jiecheng(int n);

int main()
{
	/*初始化*/
	int number = 0;
	int result = 0;
	/*操作*/
	cout << "enter the number you want to accu:" << endl;
	while(cin >> number)
	{
		result = jiecheng(number);
		cout << "the result is:" <<result << endl;
	}
	while(!(cin >> number))
	{
		cin.clear();
		while(cin.get() != '\n')
			continue;
		break;
	}
	cout << "bye bye!" << endl;

	cin.get();
	cin.get();
	return 0;
}

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

7

/*2014. 01 .11  by Arby*/
/*c++ primer 7.12 编程练习*/
/*利用指针指向数组区间*/
#include <iostream>
using namespace std;
double *fill_array(double *begin, double *end);
void show_array(double *begin, double *end);

int main()
{
	/*初始化*/
	const int arr_size = 5;
	double arr[arr_size];
	double *end;
	/*操作*/
	cout <<"enter the array:" << endl;
	end = fill_array(arr, (arr+arr_size));
	show_array(arr, end);

	cin.get();
	cin.get();
	return 0;
}

double *fill_array(double *begin, double *end)
{
	double *ps = begin;
	double number;
	while(ps != end)
	{
		cin>>number;
		if(!cin)    //bad input
		{
			cin.clear();
			while(cin.get() != '\n')
				continue;
			cout << "the wrong, being terminated" << endl;
			break;
		}
		*ps = number;
		ps++;
	}
	end = ps;
	return end;
}

void show_array(double *begin, double *end)
{
	cout << "the array is: " << endl;
	double *ps = begin;
	while(ps != end)
	{
		cout << *ps << " ";
		ps++;
	}
}

9.

/*2014. 01 .11  by Arby*/
/*c++ primer 7.12 编程练习*/
/*指针函数*/
#include <iostream>
using namespace std;
double add(double x, double y);
double mul(double x, double y);
double calculate(double x, double y, double (*ps) (double, double));


int main()
{
	/*初始化*/
	const int size = 2;
	double x, y;
	double (*ps[2])(double, double);

	/*操作*/
	ps[0] = add;
	ps[1] = mul;
	cin >> x >> y;
	for(int i = 0; i < size; i++)
	{
		cout << calculate(x, y, ps[i]) << " ";
	}

	cin.get();
	cin.get();
	return 0;
}

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

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

	double result = (*ps)(x, y);
	return result;
}

抱歉!评论已关闭.