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

/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/18
From : C++ Primer Plus第六版第七章编程练习 第1题 
Problem : 编写一个程序,不断要求用户输入两个数,直到其中一个为0。对于每两个数,程序将使用一个函数来计算它们的
调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:
	调和平均数 = 2.0 * x * y / (x + y)
*******************************************************************************************************************/
#include <iostream>
using namespace std;
double fun(double x , double y);
int main()
{
	cout << "请输入两个数: ";
	double a , b;
	while(cin >> a >> b && a && b){
		cout << fun(a , b) << endl;
		cout << "请输入两个数: ";
	}
	cout << "BYE!" << endl;
	return 0;
}

double fun(double x , double y)
{
	return 2.0 * x * y / (x + y);
}


/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/19
From : C++ Primer Plus第六版第七章编程练习 第2题 
Problem : 编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在
一行上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。请使用3个数组
处理函数来分别
*******************************************************************************************************************/
#include <iostream>
using namespace std;
int input(double *a);
void output(double *a , int n);
double fun(double *a , int n);
int main()
{
	double a[10];
	int num = input(a);
	output(a , num);
	return 0;
}

int input(double *a)
{
	int i;
	for(i = 0 ; i < 10 ; ++ i){
		cin >> a[i];
		if(a[i] <= 0)
			break;
	}
	return i;
}

void output(double *a , int n)
{
	for(int i = 0 ; i < n ; ++ i)
		cout << a[i] << " ";
	cout << fun(a , n) << endl;
}

double fun(double *a , int n)
{
	double sum = 0;
	for(int i = 0 ; i < n ; ++ i)
		sum += a[i];
	sum /= n;
	return sum;
}



/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/19
From : C++ Primer Plus第六版第七章编程练习 第3题 
Problem : 下面是一个结构声明:
struct box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};
a.编写一个函数,按值传递box结构,并显示每个成员的值
b.编写一个函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积。
c.编写一个使用这两个函数的简单程序。
*******************************************************************************************************************/
#include <iostream>
using namespace std;
struct box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};
void fun_a(box a);
void fun_b(box *a);
int main()
{
	box a = {
		"Yuuji",
		10,
		20,
		1,
		22
	};
	fun_a(a);
	fun_b(&a);
	return 0;
}

void fun_a(box a)
{
	cout << a.maker << endl << a.height << endl << a.width << endl << a.length << endl << a.volume << endl;
}

void fun_b(box *a)
{
	a->volume = a->height * a->width * a-> length;
	cout << a->volume << endl;
}


/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/19
From : C++ Primer Plus第六版第七章编程练习 第4题 
Problem : 许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩法的变体。在这些玩法中,玩家从一组被称为域号码
(field number)的号码中选择几个。例如,可以从域号码1~47中选择5个号码:还可以从第二个区间(如1~27)选择一个号码
(称为特选号码)。要赢得头奖,必须正确猜中所有的号码。中头奖的几率是选中所有域号码的几率与选中特选号码几率的乘积。
例如,在这个例子中,中头奖的几率是从47个号码中正确选取5个号码的几率与从27个号码中选择1个号码的几率的成绩。请修改
程序清单7.4,以计算中得这种彩票头奖的几率。
*******************************************************************************************************************/
#include <iostream>
using namespace std;
int main()
{
	double ans = 1;
	for(int i = 0 ; i < 5 ; i ++ )
		ans *= (double)(5 - i) / (double)(47 - i);
	ans *= double(1) / 27;
	cout << ans << endl;
	return 0;
}


/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/19
From : C++ Primer Plus第六版第七章编程练习 第5题 
Problem : 定义一个递归函数,接受一个整型参数,并返回该参数的阶乘。前面讲过,3的阶乘写作3!,等于3 * 2!,以此类推:
而0!被定义为1.通用的计算公式是,如果n大于零 , 则n! = n * (n - 1)!。在程序中对该函数进行测试,程序使用循环让用户
输入不同的值,程序将报告这些值的阶乘。
*******************************************************************************************************************/
#include <iostream>
using namespace std;
long long fun(int n);
int main()
{
	int n;
	while(cin >> n && n >= 0){
		long long ans = 0;
		if(!n)
			cout << "1" << endl;
		else{
			ans = fun(n);
			cout << ans << endl;
		}
	}
	return 0;
}

long long fun(int n)
{
	long long ans = 1;
	for( ; n ; --n)
		ans *= n;
	return ans;
}



/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/19
From : C++ Primer Plus第六版第七章编程练习 第6题 
Problem : 编写一个程序,它使用下列函数:
Fill_array()将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中。当数组被填满或
用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。
Show_array()将一个double数组的名称和长度作为参数,并显示该数组的内容。
Reverse-array()将一个double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。
程序将使用这些函数来填充数组,然后显示数组;反转数组,然后显示数组;反转数组中除第一个和最后一个元素之外的所有元素,
然后显示数组
*******************************************************************************************************************/

#include <iostream>
using namespace std;
int File_array(double *a , int len);
void Show_array(double *a , int len);
void Reverse_array(double *a , int len);
int main()
{
	double a[10];
	int num = File_array(a , 10);
	Show_array(a , num);
	Reverse_array(a , num);
	Show_array(a , num);
	Reverse_array(a + 1 , num - 2);
	Show_array(a , num);
	return 0;
}

int File_array(double *a , int len)
{
	int i;
	for(i = 0 ; i < len ; ++ i)
		cin >> a[i];
	return i;
}

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

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



/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/19
From : C++ Primer Plus第六版第七章编程练习 第7题 
Problem : 修改程序清单7.7中的3个数组处理函数,使之使用两个指针参数来表示区间。file_array()函数不返回实际读取了多少个
数字,而是返回一个指针,该指针指向最后被填充的位置:其他的函数可以将该指针作为第二个参数,以标识数据结尾。
*******************************************************************************************************************/
#include <iostream>
using namespace std;
double *fill_array(double *a);
void show_array(double *a , double *b);
void revalue(double r , double *a , double *b);
int main()
{
	double a[5];
	double *e = fill_array(a);
	show_array(a , e);
	revalue(0.5 , a , e);
	show_array(a , e);
	return 0;
}

double *fill_array(double *a)
{
	int i = 0;
	while(cin >> a[i++])
		if(i == 5)
			break;
	return &a[i];
}

void show_array(double *a , double *b)
{
	while(a != b){
		cout << *a << " ";
		 ++ a;
	}
	cout << endl;
}

void revalue(double r , double *a , double *b)
{
	while(a != b){
		(*a) *= r;
		++ a;
	}
}



/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/19
From : C++ Primer Plus第六版第七章编程练习 第8题 a小题
Problem : 在不使用array类的情况下完成程序清单7.15所做的工作。编写两个这样的版本:
a.使用const char *数组存储表示季度名称的字符串,并使用double数组存储开支。
b.使用const char *数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员——一个用于存储开支的double数组。
这种设计与使用array类的基本设计类似。
*******************************************************************************************************************/
#include <iostream>
using namespace std;
void fill_array(double *a , int len);
void show_array(double *a , int len , const char **p);
int main()
{
	const char *p[4] = {"Spring" , "Summer" , "Fall" , "Winter"};
	double a[4];
	fill_array(a , 4);
	show_array(a , 4 , p);
	return 0;
}

void fill_array(double *a , int len)
{
	for(int i = 0 ; i < len ;  ++ i)
		cin >> a[i];
}

void show_array(double *a , int len , const char **p)
{
	for(int i = 0 ; i < len ; ++ i)
		cout << *(p + i) << "\t";
	cout << endl;
	for(int i = 0 ; i < len ; ++ i)
		cout << a[i] << "\t";
	cout << endl;
}



/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/19
From : C++ Primer Plus第六版第七章编程练习 第8题 b小题
Problem : 在不使用array类的情况下完成程序清单7.15所做的工作。编写两个这样的版本:
a.使用const char *数组存储表示季度名称的字符串,并使用double数组存储开支。
b.使用const char *数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员——一个用于存储开支的double数组。
这种设计与使用array类的基本设计类似。
*******************************************************************************************************************/
#include <iostream>
using namespace std;
struct money{
	double a[4];
};
void fill_array(money *m , int len);
void show_array(money *m , int len , char **p);
int main()
{
	money t;
	char *p[4] = {"Spring" , "Summer" ,  "Fall"  , "Winter"};
	fill_array(&t , 4);
	show_array(&t , 4 , p);
	return 0;
}

void fill_array(money *m , int len)
{
	for(int i = 0 ; i < len ;  ++ i)
		cin >> m->a[i];
}

void show_array(money *m , int len , char **p)
{
	for(int i = 0 ; i < len ; ++ i)
		cout << *(p + i) << "\t";
	cout << endl;
	for(int i = 0 ; i < len ; ++ i)
		cout << m->a[i] << "\t";
	cout << endl;
}



/******************************************************************************************************************* 
Author : Yuuji 
Blog : blog.csdn.net/acm_yuuji 
Time : 2014/07/19
From : C++ Primer Plus第六版第七章编程练习 第9题
Problem : 这个练习让您编写处理数组和结构的函数。下面是程序的框架,请提供其中描述的函数,以完成该程序。
#include <iostream>
using namespace std;
const int SLEN =  30;
struct student {
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};
//getinfo() has two argumnets: a pointer to the first element of
//an array of student structures and an int representing the
//number of elemnets of the array. The function solicits and
//stores data about students. It terminates input upon filling
//the array or upon encountering a blank line for the student
//nmae. The function returns the actual number of array elemnets
//filled.
int getinfo(student pa[], int n);

//display1() takes a student structure as an argument
//and displays its contents
void display1(student st);

//display2() takes the address of student struture as an
//argument and displays the stucture's contents
void display2(const student * ps);

//display3() takes the address of the first elemnet of an array
//of student structures and the number of array elemnets as
//arguments and displays the contents of the structures
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;
}
*******************************************************************************************************************/
#include <iostream>
using namespace std;
const int SLEN =  30;
struct student {
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};
//getinfo() has two argumnets: a pointer to the first element of
//an array of student structures and an int representing the
//number of elemnets of the array. The function solicits and
//stores data about students. It terminates input upon filling
//the array or upon encountering a blank line for the student
//nmae. The function returns the actual number of array elemnets
//filled.
int getinfo(student pa[], int n);

//display1() takes a student structure as an argument
//and displays its contents
void display1(student st);

//display2() takes the address of student struture as an
//argument and displays the stucture's contents
void display2(const student * ps);

//display3() takes the address of the first elemnet of an array
//of student structures and the number of array elemnets as
//arguments and displays the contents of the structures
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)  
{  
    int count=0;  
    for(int i=0;i<n;i++)  
    {  
        cout<<"Please enter the fullname:";  
        cin>>pa[i].fullname;  
        cout<<"\nPlease enter the hobby:";  
        cin>>pa[i].hobby;  
        cout<<"\nPlease enter the ooplevel:";  
        cin>>pa[i].ooplevel;  
        count++;  
    }  
    cout<<"\nEnter end!";  
    return count;  
  
}  
  
void display1(student st) 
{  
    cout<<"\ndisplay1:FullName:"<<st.fullname<<"\nhobby:"<<st.hobby  
        <<"\nooplevel:"<<st.ooplevel<<endl;  
}  
  
void display2(const student *ps)
{  
    cout<<"\ndispaly2:FullName:"<<ps->fullname<<"\nhobby:"<<ps->hobby  
        <<"\nooplevel:"<<ps->ooplevel<<endl;  
  
}  
void display3(const student pa[],int n)  
{  
    cout<<"\ndispaly3:"<<endl;  
    for(int i=0;i<n;i++)  
        cout<<i<<"::FullName:"<<pa[i].fullname<<"\nhobby:"<<pa[i].hobby  
        <<"\nooplevel:"<<pa[i].ooplevel<<endl;  
}  


第10题 2015/08/05填坑 _(:з」∠)_

#include <iostream>

double calculate(double a, double b, double (*pf)(double a, double b));
double add(double a, double b);
double max(double a, double b);
double min(double a, double b);

int main()
{
	double a, b;
	double (*pf[3])(double a, double b);
	pf[0] = add;
	pf[1] = max;
	pf[2] = min;
	while(std::cin >> a >> b)
		for(int i = 0 ; i < 3 ; ++i)
			std::cout << (*pf[i])(a, b) << std::endl;
	return 0;
}

double calculate(double a, double b, double (*pf)(double a, double b))
{
	return (*pf)(a, b);
}

double add(double a, double b)
{
	return a + b;
}

double max(double a, double b)
{
	return a > b ? a : b;
}

double min(double a, double b)
{
	return a < b? a : b;
}


  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值