C++ Primer Plus第五版 第七章 編程練習答案

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
From : C++ Primer Plus第五版第七章編程練習 第1題  
Problem : 編寫一個程序,不斷要求用戶輸入兩個數,直到其中一個爲0。對於每兩個數,程序將使用一個函數來計算它們的 
調和平均數,並將結果返回給main(),而後者將報告結果。調和平均數指的是倒數平均值的倒數,計算公式如下: 
調和平均數 = 2.0 * x * y / (x + y) 
*******************************************************************************************************************/ 
#include<iostream>
using namespace std;
double harmonic_mean(double x,double y);
int main()
{
	cout << "Enter two numbers (0 represent end): ";
	double x,y;
	while(cin >> x >> y && x*y!=0)
	{
		cout << "The harmonic mean of " << x << " and " << y << " is " << harmonic_mean(x,y) << endl;
		cout << "Enter two numbers (0 represent end): ";
	}
	cout << "Bye! " <<endl;
	system("pause");
	return 0;
}
double harmonic_mean(double x,double y)
{
	return 2.0*x*y/(x+y);
}


/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
From : C++ Primer Plus第五版第七章編程練習 第2題  
Problem : 編寫一個程序,要求用戶輸入最多10個高爾夫成績,並將其存儲在一個數組中。程序允許用戶提早結束輸入,並在 
一行上顯示所有成績,然後報告平均成績。請使用3個數組處理函數來分別進行輸入、顯示和計算平均成績。請使用3個數組 
處理函數來分別  
*******************************************************************************************************************/ 
#include<iostream>
using namespace std;
const int SIZE = 10;
int input_scords(double scords[], int size);
void show(const double scords[],int size);
double mean(const double scords[],int size);

int main()
{
	cout << "Enter " << SIZE << " scords: ";
	double scords [10]={0};
	int num;
	num= input_scords(scords, SIZE);
	show(scords,num);
	cout << "The mean scords is " << mean(scords,num) <<endl;
	system("pause");
	return 0;
}
int input_scords(double scords[], int SIZE)
{
	double temp;
	int i=0;
	for(i=0;i<SIZE;i++)
	{
		cin>>temp;
		if(!cin)         //bad input
		{
			cin.clear();
			while(cin.get()!='\n')
				continue;
			cout<<"Bad input:input process terminated.\n";
			break;
		}
		else if(temp<0)
			break;
		scords[i]=temp;
	}
	return i;
}

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

double mean(const double scords[],int size)
{
	double sum=0;
	for (int i=0; i<size ;i++)
		sum+=scords[i];
	double mean_scords=sum/size;
	return mean_scords;
}

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
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 volum;
};
void show_box (const box box1);
void box_fun(box *p);
int main()
{
	box box1;
	cout << "Please enter the name of maker: ";
	cin.getline(box1.maker,40);
	cout << "Enter the height, width and length: ";
	cin >> box1.height >> box1.width >> box1.length ;
	box_fun(&box1);
	show_box(box1);


	system("pause");
	return 0;
}

void box_fun(box *p)
{
	p->volum = p->height*p->length*p->width;
}


void show_box (const box box1)
{
	cout << "The maker of the box is " << box1.maker << endl;
	cout << "The height, width and length of the box are " << box1.height << ", "<< box1.width << ", "<< box1.length << endl; 
	cout << "The volum of the box is " << box1.volum <<endl;
}

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
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;
long double probability(unsigned numbers1, unsigned picks1, unsigned numbers2, unsigned picks2);
int main()
{
	
	double total1, choices1, total2, choice2;
	cout << "Enter the total number of choices on the game card and\n"
		"the number of picks allowed of field number and Selected number:\n";
	while ((cin >> choices1 >> total1>> choice2 >> total2) && choices1 <= total1 && choice2 <= total2)
	{
		cout << "You have one chance in ";
		cout << probability(total1, choices1, total2, choice2);    
		cout << " of winning.\n";
		cout << "Next two numbers (q to quit): ";
	}
	cout << "bye\n";
	system("pause");
	return 0;
}

long double probability(unsigned numbers1, unsigned picks1, unsigned numbers2, unsigned picks2)
{
	long double result = 1.0; 
	long double n;
	unsigned p;

	for (n = numbers1, p = picks1; p > 0; n--, p--)
		result = result * n / p ; 
	for (n = numbers2, p = picks2; p > 0; n--, p--)
		result = result * n / p ; 
	return result;
}

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
From : C++ Primer Plus第五版第七章編程練習 第5題  
Problem : 定義一個遞歸函數,接受一個整型參數,並返回該參數的階乘。前面講過,3的階乘寫作3!,等於3 * 2!,以此類推: 
而0!被定義爲1.通用的計算公式是,如果n大於零 , 則n! = n * (n - 1)!。在程序中對該函數進行測試,程序使用循環讓用戶 
輸入不同的值,程序將報告這些值的階乘。 
*******************************************************************************************************************/ 
#include<iostream>
using namespace std;
long long times(int num);
int main()
{
	cout << "Enter an integer (negative represent the end): ";
	int num;
	while(cin >> num)
	{
		if (num < 0)
			break;
		cout << "The factorial of " << num << " is " << times(num) << endl;
		cout << "Enter another integer (negative represent the end): ";
	}
	system("pause");
	return 0;
}
long long times(int num)
{
	long long ans;
	if (num == 1 || num == 0)
		return 1;
	ans = num*times(num-1);
	return ans;
}

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
From : C++ Primer Plus第五版第七章編程練習 第6題  
Problem : 編寫一個程序,它使用下列函數: 
Fill_array()將一個double數組的名稱和長度作爲參數。它提示用戶輸入double值,並將這些值存儲到數組中。當數組被填滿或 
用戶輸入了非數字時,輸入將停止,並返回實際輸入了多少個數字。 
Show_array()將一個double數組的名稱和長度作爲參數,並顯示該數組的內容。 
Reverse-array()將一個double數組的名稱和長度作爲參數,並將存儲在數組中的值的順序反轉。 
程序將使用這些函數來填充數組,然後顯示數組;反轉數組,然後顯示數組;反轉數組中除第一個和最後一個元素之外的所有元素, 
然後顯示數組 
*******************************************************************************************************************/ 
#include<iostream>
using namespace std;
const int SIZE =10;
int Fill_array(double arr[],int SIZE);
void Show_array(double arr[],int size);
void Reverse_array(double arr[],int size);
int main()
{

	double arr[SIZE];
	int length;
	length=Fill_array(arr,SIZE);
	Show_array(arr,length);
	Reverse_array(arr,length);
	Show_array(arr,length);
	Reverse_array(arr+1,length-2);
	Show_array(arr,length);

	system("pause");
	return 0;

}

int Fill_array(double arr[],int SIZE)
{
	cout << "Enter double numbers(less than " << SIZE << ", q to quit):  \n";
	int i=0;
	double temp;
	while(cin >> temp)
	{
		if (i<SIZE)
		{
			arr[i]=temp;
			i++;
		}
		else break;
	}

	return i;
}
void Show_array(double arr[],int size)
{
	cout << "The array is ";
	for (int i=0;i<size;i++)
	{
		cout << arr[i] <<"  ";
	}
	cout<<endl;
}

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

/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
From : C++ Primer Plus第五版第七章編程練習 第7題  
Problem : 修改程序清單7.7中的3個數組處理函數,使之使用兩個指針參數來表示區間。file_array()函數不返回實際讀取了多少個 
數字,而是返回一個指針,該指針指向最後被填充的位置:其他的函數可以將該指針作爲第二個參數,以標識數據結尾。 
*******************************************************************************************************************/ 
#include <iostream>  
using namespace std;  
const int SIZE=10;
double *fill_array(double *a);  
void show_array(double *a , double *b);  
void revalue(double r , double *a , double *b);  
int main()  
{  
	double a[SIZE];  
	double *e = fill_array(a);  
	show_array(a , e);  
	revalue(0.5 , a , e);  
	show_array(a , e);  
	system("pause");
	return 0;  
}  

double *fill_array(double *a)  
{  	cout << "Enter double numbers(less than " << SIZE << ", q to quit):  \n";
	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 : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
From : C++ Primer Plus第五版第七章編程練習 第8題  
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;    
}    


/*******************************************************************************************************************  
Author : Cui mingyang 
Blog : cx_12586 
Time : 2017/10/28 
From : C++ Primer Plus第五版第七章編程練習 第9題
Problem: 設計一個名爲calculate()的函數,它接受兩個double值和一個指向函數的指針,而被指向的函數接受兩個double參數,
並返回一個double值。calculate()函數的類型也爲double,並返回被指向的函數使用calculate()的兩個double參數計算得到的值。
例如,架設add()函數的定義如下:
double add(double x, double y)
{
return x + y;
}
則下述代碼中的函數調用將導致calculate()把2.5和10.4傳遞給add()函數,並返回add()的返回值(12.9):
double q = calculate(2.5, 10.4, add);
請編寫一個程序,它調用上述兩個函數和至少另一個與add()類似的函數。該程序使用循環來讓用戶成對地輸入數字。對於每對數字,
程序都使用calculate()來調用add()和至少一個其他的函數。如果讀者愛冒險,可以嘗試創建一個指針數組,其中的指針指向add()
樣式的函數,並編寫一個循環,使用這些指針連續讓calculate()調用這些函數。提示:下面是聲明這樣指針數組的方式,其中包含
三個指針:
double (*pf[3])(double, double);
可以採用數組初始化語法,並將函數名作爲地址來初始化這樣的數組。
*******************************************************************************************************************/
#include <iostream>  
using namespace std;
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;
	cout << "Enter two double numbers(q two quit): ";
	while(cin >> a >> b)  
	{
		cout << "The sum of " << a << " and " << b << " is " << (*pf[0])(a, b) <<endl;
		cout << "The max of " << a << " and " << b << " is " << (*pf[1])(a, b) <<endl;  
		cout << "The min of " << a << " and " << b << " is " << (*pf[2])(a, b) <<endl;  
		cout << "Enter another two double numbers(q two quit): ";
	}
		
	system("pause");
	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? b : a;  
}  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章