C++ Primer Plus 第七章编程题练习

C++ Primer Plus 第七章编程题练习

第一题

题目描述

编写一个程序,不断要求用户输入两个数,直到其中的一个为0。对于每两个数,程序将使用一个 函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均 值的倒数,计算公式如下: 调和平均数=2.0*x*y/(x+y)

代码片段
#include <iostream>
#include <cmath>
using namespace std;

double conver(double x, double y);
int main()
{
    cout << "Please enter two numbers: ";

    double x, y;
    while (1)
    {

        if (!(cin >> x >> y)||fabs(x+y)<1e-6)
        {
            cin.clear();
            cin.sync();
            cout << "Invalid input! Please enter again.\n";
            continue;
        }
        else if (x == 0 || y == 0)
        {
            cout << "Done!\n";
            break;
        }
        else
        {
            double result = conver(x, y);
            cout << "The result is " << result << endl;
            cout << "Next input: ";
        }
    }
    while (cin.get() != EOF)
        ;
    return 0;
}
double conver(double x, double y)
{
    return 2.0 * x * y / (x + y);
}
/* 编写一个程序,不断要求用户输入两个数,直到其中的一个为0。对于每两个数,程序将使用一个
函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均
值的倒数,计算公式如下:
调和平均数=2.0*x*y/(x+y) */


第二题

题目描述

编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提 早结束输入,并在一行 上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、 显示和计算平均成绩。

代码片段
#include <iostream>
#include <array>

using namespace std;

int input(float *);
void output(float *, int);
float ave(float *, int);

int main()
{
    float a[10];

    int size = input(a);
    output(a,size);
    float aver=ave(a,size);
    cout<<"The average is "<<aver<<endl;
    while (cin.get() != EOF)
        ;
    return 0;
}
int input(float *a)
{
    int size;
    cout<<"You can input no more than 10 numbers and you can input what is not numbers to quit!\n";
    for(size=0;cin>>a[size];size++);
    cin.clear();
    cin.sync();
    return size;
}
void output(float *a,int size)
{
    cout<<"All the grades are:\n";
    for(int i=0;i<size;i++)
        cout<<a[i]<<' ';
    cout<<endl;
    return ;
} 
float ave(float *a,int size)
{
    float sum=0;
    for(int i=0;i<size;i++)
    sum+=a[i];
    sum/=size;
    return sum;
}
/* 编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提
早结束输入,并在一行 上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、
显示和计算平均成绩。 */


第三题

题目描述

3.下面是一个结构声明:
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 print(box);
void set(box*);

int main()
{
    cout<<"Please enter the product's maker, height, width and length.\n";
    box a;a.volume=0;
    cout<<"Now! Please enter the maker: ";cin.getline(a.maker,40);
    cout<<"Now, you can input height, width and length orderly.\n";
    cin>>a.height>>a.width>>a.length;
    cout<<"You inputed information just now are there: \n";
    print(a);
    cout<<"The volume has been setted:\n";
    set(&a);
    print(a);
    cout<<"Done!";
    while(cin.get()!=EOF);
    return 0;
}
void print(box a)
{
    cout<<"The maker: "<<a.maker<<"\nThe height: "<<a.height<<"\nThe width: "<<a.width<<"\nThe length: "<<a.length<<"\nThe volume: "<<a.volume<<endl;
    return;
}
void set(box* a)
{
  a->volume=a->height*a->length*a->width;
}
/* struct box
{
char maker[40] ;
float height;
float width;
float length; 
float volume ;
};
a.编写一个函数,按值传递box结构,并显示每个成员的值。
b.编写一个函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积。
c.编写一个使用这两个函数的简单程序。*/


第四题

题目描述

许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩法的变体。在这些玩法中,玩家从 一组被称为域号码( field number)的号码中选择几个。例如,可以从域号码1~47中选择5个号码:还可 以从第二个区间(如1~27)选择一个号码(称为特选号码)。要赢得头奖,必须正确猜中所有的号码。中 头奖的几率是选中所有域号码的几率与选中特选号码几率的乘积。例如,在这个例子中,中头奖的几率是 从47个号码中正确选取5个号码的几率与从27个号码中正确选择1个号码的几率的乘积。请修改程序清 单7.4,以计算中得这种彩票头奖的几率。

代码片段
#include <iostream>

using namespace std;

int main()
{
    long double mu=27;
    for(int i=43;i-43<=4;i++)
        mu*=i;
    long double zi=1;
    for(int i=2;i<=5;i++)
        zi*=i;
    long double p=zi/1.0/mu;
    cout<<fixed;cout.precision(10);
    cout<<"P = "<<p<<'%'<<endl; 
    while(cin.get()!=EOF);
    return 0;
}
/* 许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩法的变体。在这些玩法中,玩家从
一组被称为域号码( field number)的号码中选择几个。例如,可以从域号码1~47中选择5个号码:还可
以从第二个区间(如1~27)选择一个号码(称为特选号码)。要赢得头奖,必须正确猜中所有的号码。中
头奖的几率是选中所有域号码的几率与选中特选号码几率的乘积。例如,在这个例子中,中头奖的几率是
从47个号码中正确选取5个号码的几率与从27个号码中正确选择1个号码的几率的乘积。请修改程序清
单7.4,以计算中得这种彩票头奖的几率。 */


第五题

题目描述

定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。前面讲过,3的阶乘写作3!,等于 3*2!,依此类推:而0!被定义为1,通用的计算公式是,如果n大于零,则n!=n* (n-1) 在程序中对该 函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。

代码片段
#include <iostream>

using namespace std;

long double conver(int);
int main()
{
    int a;
    cout<<fixed;cout.precision(0);
    cout << "Please enter a number, i will give you its factorial:\n(When your input less than 0, it's invalid, and when you don't input digital, it will quit!)\n";
    while (1)
    {
        if (!(cin >> a))
        {
            cin.clear();
            cin.sync();
            cout << "Done!" << endl;
            break;
        }
        else if (a < 0)
        {
            cout << "Invalid Input! Please enter again!\n";
            continue;
        }
        else
            cout << "The factorial of " << a << " is " << conver(a) << '.' << endl<<"Next input:\n";
    }
    while (cin.get() != EOF)
        ;
    return 0;
}
long double conver(int a)
{
    if(a==0)
        return 1;
    else
        return a*conver(a-1);
}
/* 定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。前面讲过,3的阶乘写作3!,等于
3*2!,依此类推:而0!被定义为1,通用的计算公式是,如果n大于零,则n!=n* (n-1) 在程序中对该
函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。 */


第六题

题目描述

编写一个程序,它使用下列函数: Fill_array( )将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到 数组中。当数组被填满或用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。 Show_array( )将一个 double数组的名称和长度作为参数,并显示该数组的内容。 Reverse-array( )将一个 double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。 程序将使用这些函数来填充数组,然后显示数组,反转数组,然后显示数组,反转数组中除第一个和 最后一个元素之外的所有元素,然后显示数组。

代码片段
#include <iostream>

using namespace std;

#define ize 10
int Fill_array(double *, int);
void Show_array(double *, int);
void Reverse_array(double *, int);
int main()
{
    double a[ize];

    int size = Fill_array(a, ize);
    Show_array(a, size);
    Reverse_array(a,size);
    Show_array(a, size);
    Reverse_array(a+1,size-2);
    Show_array(a, size);
    while (cin.get() != EOF)
        ;
    return 0;
}
int Fill_array(double *a, int n)
{
    int size;
    cout << "You can input no more than 10 numbers and you can input what is not numbers to quit!\n";
    for (size = 0; cin >> a[size] && size < n; size++)
        ;
    cin.clear();
    cin.sync();
    return size;
}
void Show_array(double *a, int size)
{
    cout << "All the digitals are:\n";
    for (int i = 0; i < size; i++)
        cout << a[i] << ' ';
    cout << endl;
    return;
}
void Reverse_array(double *a, int size)
{
    for(int i=0;i<size-1-i;i++)
    {
        double temp=a[i];
        a[i]=a[size-1-i];
        a[size-1-i]=temp;
    }
}
/* 编写一个程序,它使用下列函数:
Fill_array( )将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到
数组中。当数组被填满或用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。
Show_array( )将一个 double数组的名称和长度作为参数,并显示该数组的内容。
Reverse-array( )将一个 double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。
程序将使用这些函数来填充数组,然后显示数组,反转数组,然后显示数组,反转数组中除第一个和
最后一个元素之外的所有元素,然后显示数组。 */


第七题

题目描述

修改程序清单7.7中的3个数组处理函数,使之使用两个指针参数来表示区间。fill_ array( )函数不 返回实际读取了多少个数字,而是返回一个指针,该指针指向最后被填充的位置;其他的函数可以将该指 针作为第二个参数,以标识数据结尾。

代码片段
#include <iostream>

const int Max = 5;

double *fill_array(double ar[], int limit);
void show_array(const double ar[], double *end);
void revalue(double r, double ar[], double* end);

int main()
{
    using namespace std;
    double properties[Max];
    double* size = fill_array(properties, Max);
    show_array(properties, size);
    if (size > 0)
    {
        cout << "Enter revaluation factor: ";
        double factor;
        while (!(cin >> factor))
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input; Please enter a number: ";
        }
        revalue(factor, properties, size);
        show_array(properties, size);
    }
    cout << "Done. \n";
    cin.get();
    cin.get();
    return 0;
}
double *fill_array(double ar[], int limit)
{
    using namespace std;
    double temp;
    int i;
    for (i = 0; i < limit; i++)
    {
        cout << "Entervalue#" << (i + 1) << ":";
        cin >> temp;
        if (!cin)
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input; input process terminated. \n";
            break;
        }
        else if (temp < 0)
            break;
        ar[i] = temp;
    }
    return ar + i;
}
void show_array(const double ar[], double* end)
{
    using namespace std;
    for (int i = 0;ar+i < end; i++)
    {
        cout << "Property #" << (i + 1) << ": $";
        cout << ar[i] << endl;
    }
}
void revalue(double r, double ar[], double* end)
{
    for (int i = 0; ar+i < end; i++)
        ar[i] *= r;
}


第八题

题目描述

在不使用array类的情况下完成程序清单7.15所做的工作。编写两个这样的版本: a.使用const char *数组存储表示季度名称的字符串,并使用double数组存储开支。 b.使用const char *数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员一 个用于存储开支的double数组。这种设计与使用array类的基本设计类似。

代码片段
#include <iostream>

using namespace std;

#define Seasons 4
const char Snames[Seasons][20] = {"Spring", "Summer", "Fall", "Winter"};

void a(double (*pa)[Seasons]);
void show(double da[Seasons]);

int main()
{

    double expenses[Seasons];
    a(&expenses);
    show(expenses);
    while (cin.get() != EOF)
        ;
    return 0;
}
void a(double (*pa)[Seasons])
{
    for (int i = 0; i < Seasons; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> (*pa)[i];
    }
    return;
}
void show(double da[Seasons])
{

    double total = 0.0;
    cout << "\nEXPENSES\n";
    for (int i = 0; i < Seasons; i++)
    {
        cout << Snames[i] << ": $" << da[i] << endl;
        total += da[i];
    }
    cout << "Total Expenses: $" << total << endl;
    return;
}
/* 在不使用array类的情况下完成程序清单7.15所做的工作。编写两个这样的版本:
a.使用const char *数组存储表示季度名称的字符串,并使用double数组存储开支。
b.使用const char *数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员一
个用于存储开支的double数组。这种设计与使用array类的基本设计类似。*/

#include <iostream>

using namespace std;

#define Seasons 4
const char Snames[Seasons][20] = {"Spring", "Summer", "Fall", "Winter"};

typedef struct conver{
    double m[Seasons];
} T;
void a(T*);
void show(T);

int main()
{

    conver expenses;
    a(&expenses);
    show(expenses);
    while (cin.get() != EOF)
        ;
    return 0;
}
void a(T* pa)
{
    for (int i = 0; i < Seasons; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> pa->m[i];
    }
    return;
}
void show(T da)
{

    double total = 0.0;
    cout << "\nEXPENSES\n";
    for (int i = 0; i < Seasons; i++)
    {
        cout << Snames[i] << ": $" << da.m[i] << endl;
        total += da.m[i];
    }
    cout << "Total Expenses: $" << total << endl;
    return;
}
/* 在不使用array类的情况下完成程序清单7.15所做的工作。编写两个这样的版本:
a.使用const char *数组存储表示季度名称的字符串,并使用double数组存储开支。
b.使用const char *数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员一
个用于存储开支的double数组。这种设计与使用array类的基本设计类似。*/

由于在std空间中fill()函数已经被定义,不能再次定义,以上改为了a()函数。



第九题

题目描述

这个练习让您编写处理数组和结构的函数。下面是程序的框架,请提供其中描述的函数,以完成该 程序。

代码片段
#include <iostream>

using namespace std;

const int SLEN = 30;

struct student
{
    char fullname[SLEN];
    char hobby[SLEN];
    int ooplevel;
};

int getinfo(student pa[], int n);
void display1(student st);
void display2(const student *ps);
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);
    cout << entered << " is the size.\n";
    for (int i = 0; i < entered; i++)
    {
        cout << "Stufent #" << i + 1 << endl;
        display1(ptr_stu[i]);
        display2(&ptr_stu[i]);
    }
    display3(ptr_stu, entered);
    delete[] ptr_stu;
    cout << "Done\n";
    while (cin.get() != EOF)
        ;
    return 0;
}
int getinfo(student pa[], int n)
{
    int count = 0;
    for (; count < n; count++)
    {
        cout << "Student#" << count + 1 << ":\nFullname: ";
        cin.getline(pa[count].fullname, SLEN);
        if (pa[count].fullname[0] == '\0')
            break;
        cout << "Hobby: ";
        cin.getline(pa[count].hobby, SLEN);
        cout << "Ooplevel: ";
        (cin >> pa[count].ooplevel).get();
    }
    return count;
}
void display1(student st)
{
    cout << "Fullname: " << st.fullname;
    cout << "\nHobby: " << st.hobby;
    cout << "\nOoplevel: " << st.ooplevel << endl;
}
void display2(const student *ps)
{
    cout << "Fullname: " << ps->fullname;
    cout << "\nHobby: " << ps->hobby;
    cout << "\nOoplevel: " << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << "Student #" << i + 1 << endl;
        cout << "Fullname: " << pa[i].fullname;
        cout << "\nHobby: " << pa[i].hobby;
        cout << "\nOoplevel: " << pa[i].ooplevel << endl;
    }
}


第十题

题目描述

设计一个名为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 add(double a,double b){return a+b;}
double multiple(double a,double b){return a*b;}
double min(double a,double b){return (a<b?a:b);}
double max(double a,double b){return (a>b?a:b);}
double calculate(double a,double b,double (*pa)(double,double)){return pa(a,b);}

int main()
{
    double (*pa[4])(double,double)={add,multiple,min,max};
    const string name[4]={"add","multiple","min","max"};
    double a,b;
    while(cin>>a>>b)
        for(int i=0;i<4;i++)
            cout<<name[i]<<"# = "<<(*(pa[i]))(a,b)<<endl;

    cin.clear();
    cin.sync();
    while(cin.get()!=EOF);
    return 0;
}
/* 设计一个名为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}; 
可以采用数组初始化语法,并将函数名作为地址来初始化这样的数组。 */


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_南明_离火_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值