C++ Primer Plus第五版 第七章 编程练习答案
来源:互联网 发布:百度淘宝网购物商城 编辑:程序博客网 时间:2024/05/01 06:10
/******************************************************************************************************************* 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; }
阅读全文
0 0
- c++.primer.plus第五版第七章编程练习答案
- C++ Primer Plus第五版 第七章 编程练习答案
- C++.Primer.Plus第五版第五章编程练习答案
- C Primer Plus (第五版) 第五章 编程练习 答案
- C ++ Primer Plus 第六版 第七章编程练习答案
- C primer plus(第五版)编程练习第七章
- c++.primer.plus第五版第六章编程练习答案
- c++.primer.plus第五版第八章编程练习答案
- c++.primer.plus第五版第九章编程练习答案
- c++.primer.plus第五版第十章编程练习答案
- C Primer Plus (第五版) 第六章 编程练习 答案
- C++ Primer Plus第五版 第五章 编程练习答案
- C primer plus 第五章 编程练习个人答案
- C++ Primer Plus第六版 第七章 编程练习答案
- C++ Primer Plus第六版 第五章 编程练习答案
- C++ Primer Plus第五版 第四章 编程练习答案
- C++ Primer Plus第五版 第三章 编程练习答案
- C++ Primer Plus第五版 第二章 编程练习答案
- System类和Runtime类
- 程序员的朋友圈有毒
- 成为 Google 认证机器学习工程师,零基础也只需要 2 步!
- 5分钟回顾苹果发布会
- 程序员怎么追妹子?用代码呀~
- C++ Primer Plus第五版 第七章 编程练习答案
- 如果你报createSQLQuery is not valid without active transaction,请看这里
- 更改Android Studio的默认布局
- (一)K近邻算法及MNIST应用
- 关于使用corebench的学习笔记
- JFileChooser简单入门
- JAVA中的值传递和引用传递,String的值传递特性
- AR——增强现实技术
- 奇葩程序员遇上了奇葩公司