C++ Primer第七章课后编程题

来源:互联网 发布:淘宝店越来越少 编辑:程序博客网 时间:2024/04/30 05:18
1、编写一个程序,不断要求用户输入两个数,直到其中的一个为0.对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:
代码
#include<iostream>double avg(int x, int y);int main(){    using namespace std;    int a;    int b;    cout << "Enter two number:\n";    while((cin >> a >> b) && a != 0 && b != 0)    {        cout << "result: " << avg(a, b) << endl;        cout << "Enter next two number:\n";    }    cout << "Bey!\n";    return 0;}double avg(int x, int y){    double value = 2.0*x*y/(x+y);    return value;}
运行结果

2、编写一个以,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请合3个数组处理函数来分别进行输入,显示和计算平均成绩。
代码
#include<iostream>const int limit = 10;int fill_array(double ar[], int limit);double show_array(double ar[], int size);void avg_array(double sum, int size);int main(){    using namespace std;    double ar[limit];    int size = fill_array(ar, limit);    double sum = show_array(ar, size);    avg_array(sum,size);    return 0;}int fill_array(double ar[], int limit){    using namespace std;    double temp;    int i;    for (i=0; i<limit; i++)    {        cout << "Enter value #" << (i+1) << ": ";        cin >> temp;        if(!cin)        {            cin.clear();            while(cin.get() != '\n');                continue;            cout << "Bad input:\n";            break;        } else if(temp < 0)            break;        ar[i] = temp;    }    return i;}         double show_array(double ar[], int size){    using namespace std;    int i;    double sum;    cout << "所有成绩: ";    for(i=0; i< size; i++)    {        sum += ar[i];        cout << ar[i] << " ";    }    return sum;}   void avg_array(double sum, int size){    using namespace std;    cout << "平均值为: " << sum/size <<endl;}
运行结果


3、下面是一个结构声明:
  struct box
  {
     char maker[40];
     float height;
     float width;
     float length;
     float volume;
  }; 
a.编写一个函数,按值传递box结构,并显示每个成员的值。
b.编写一个函数,传递box结构的地址,并将volume成员设置为
其他三维长度的乘积。

c.编写一个使用这两个函数的简单程序。

代码
#include<iostream>struct box{    char maker[40];    float height;    float width;    float length;    float volume;};void show_box(box b);void set_box(box *pb);int main(){    box b = {"Guugle Gu", 2, 3,5, 1};    show_box(b);    set_box(&b);    show_box(b);    return 0;}void show_box(box b){    using namespace std;    cout << "Box maker: " << b.maker <<endl;    cout << "Box height: " << b.height <<endl;    cout << "Box width: " << b.width <<endl;    cout << "Box length: " << b.length <<endl;    cout << "Box volume: " << b.volume <<endl;}void set_box(box *pb){    using namespace std;    pb->volume = pb->height * pb->width * pb->length;}
运行结果


4、许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩法的变体。在这些玩法中,玩家从一组被称为域号码(field number)的号码中选择几个。例如,可以从域号码1~47中选择5个号码:还可以从第二个间(如1~27)中选择一个号码(称为特选号码)。要赢得头等奖,必须正确猜中所有的号码。中头奖的几率是选中所有域号码几率与选中特选号码几率的乘积。请修改程序清单7.4,以计算中得这种彩票头奖的赃证。
代码
#include<iostream>const int n1=5;const int num1 = 47;const int n2=1;const int num2 = 27;long double odds(unsigned num, unsigned n);int main(){    using namespace std;    double odds1 = odds(num1, n1);    double odds2 = odds(num2, n2);    cout << "中头奖的几率为: " << odds1*odds2 <<endl;    return 0;}long double odds(unsigned num, unsigned n){    using namespace std;    long double res=1;    for(num, n; n>0; n--,num--)       res = res * n/num;    return res;}
运行结果


5、定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序员将报告这些值的阶乘。
代码
#include<iostream>int factorial(int n);int main(){    using namespace std;    int n;    cout << "Enter a number:";    cin >> n;    cout << factorial(n) <<endl ;    return 0;}int factorial(int n){    int res = 1;    if (n > 1)        res = n * factorial(n-1);    else        res = res * 1;    return res;}
运行结果


6.编写一个程序,它使用下列函数:
Fill_array()将一个double数组的名称和长度作为参数,它提示用户输入double值,并将这些值存储到数组中。当数组被填满或用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。
Show_array()将一个double数组 的名称和长度作为参数,并显示该数组的内容。
Reverse-array()将一个double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。程序将使用这些函数来填充数组,然后显示数组;反转数组,然后示数组;反转数组中除第一个和最后一个元素之外的所有元素,然后显示数组。
代码
#include<iostream>int fill_array(double ar[], int limit);void show_array(double ar[], int size);void reverse_array(double ar[], int size);using namespace std;int main(){    int n;    cout << "输入数组的元素个数:";    cin >> n;    double ar[n];    int size = fill_array(ar, n);    show_array(ar, size);    reverse_array(ar, size);    cout << "---------反转后结果----------\n";    show_array(ar, size);    return 0;}int fill_array(double ar[], int limit){    double temp;    int i;    for (i=0; i<limit; i++)    {        cout << "Enter value#" << (i+1) << " \n";        cin >> temp;        if (!cin)        {            cin.clear();            cout << "bad input:\n";            break;        }else            ar[i] = temp;    }    return i;}void show_array(double ar[], int size){    for(int i=0; i<size; i++)        cout << "ARR_RES" << (i+1) << ": " << ar[i] <<endl;;}void reverse_array(double ar[], int size){    int i,j;    for (i=1, j=size-i-1; i<j; i++, j--)    {        double temp = ar[i];        ar[i] = ar[j];        ar[j] = temp;    }}
运行结果


9、设计一个名为calculate()的函数,它接受两个double值和一个指向函数的指针,而被指向的函数接受两个double参数,并返回一个double值、calculate()函数的类型也是double,并返回被指向的函数使用calculate()的两个double参数计算得到的值。例如,假如add()函数的定义如下:
   double add(double x,double y)
   {
  return x + y;
   }
   则下述代码中的函数调用:
   double q = calculate(2.5,10.4,add);
将导致calculate()把2.5和10.4传递给add()函数,
并返回add()的返回值(12.9).请编写一个程序,它调用上述两个函数和至少另一个与add()类似的数。如果读者爱冒险,可以尝试创建一个指针数组,其中的指针指向add()样式的函数,并编写一个循环,使用这些指针连续让calculate()调用这些函数。
提示:下面是声明这种指针数组的方式,其中包含3个指针:double (*pf[3]) (double,double);可以采用数组初始化句法,并将函数名作为地址来初始化这样的数组。
代码
#include<iostream>double add(double x, double y);double sub(double x, double y);double mean(double x, double y);double calculate(double x, double y, double (*pf)(double, double));int main(){    using namespace std;    double (*pf[3])(double, double) = {add, sub, mean};    const char (*guugle[3]) = {"sum", "difference", "mean"};    double a,b;    cout << "Enter pairs of numbers (q to quit):";    int i;    while (cin >> a >> b)    {        cout << calculate(a, b, add) << "= sum\n";        cout << calculate(a, b, mean) << "= mean\n";        for(i=0; i<3 ; i++)            cout << calculate(a, b, pf[i]) << " = " << guugle[i] << "\n";    }    return 0;}double calculate(double x, double y, double (*pf)(double, double)){    return (*pf)(x, y);}double add(double x, double y){    return x + y;}double sub(double x, double y){    return x - y;}double mean(double x, double y){    return (x+y)/2.0;}
运行结果



2 0