c++ primer plus编程练习-第七章

1、每输入两个数字计算一次调和平均数

//7.1 cpp program
#include<iostream>
using namespace std;
double harmonicMean(double a, double b)
{
    return 2.0*a*b / (a + b);
}

int main()
{
    double a, b;
    double result;
    cout << "please input two data,and this program will calculate the harmonic mean for u:";
    while (cin >> a >> b)
    {
        if (a == 0 || b == 0)
            break;
        result = harmonicMean(a, b);
        cout << "this result is :" << result << endl;
        cout << "please input two data,and this program will calculate the harmonic mean for u:";
    }
    while (1);
    return 0;
}

心得:
1)利用cin>>a>>b时,程序将调用一个函数,该函数返回一个istream值。因此,整个while循环的测试表达式的最终结果为cin,而cin被用于测试表达式时将根据输入是否成功,被转换为bool值(true or false)

2)假如main函数前的函数原型为

double harmonicMean(double, double)

则main函数后的函数定义为
double harmonicMean(double a, double b)
{
return 2.0*a*b / (a + b);
}

3)优化:0==a||0==b可以防止赋值运算符和==的错用,因为0是常量,不能被赋值。

4)函数原型不要求提供变量名,有类型列表就够了


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

//7.2 cpp program
#include<iostream>
using namespace std;

const int Max = 10;

int inputData(double score[], int Max);
void showData(const double [], int count);
double calAverage(const double [], int count);

int main()
{
    double score[Max];
    int count = inputData(score, Max);
    showData(score, count);
    cout << "the average of these " << count << " score is :" << calAverage(score, count) << endl;
    while (1);
    return 0;
}

int inputData(double score[], int Max){
    int count = 0;
    for (int i = 0; i < Max; ++i)
    {
        cout << "score #" << i + 1 << " :";
        cin >> score[i];
        if (score[i] < 0)
            break;
        count++;
    }
    return count;
}

void showData(const double score[],int count){
    for (int i = 0; i < count; ++i)
    {
        cout << score[i] << " ";
    }
    cout << endl;
}

double calAverage(const double score[], int count){
    double sum = 0;
    for (int i = 0; i < count; ++i)
    {
        sum += score[i];
    }
    return sum / count;
}

心得:
1)在函数原型中,一维数组的形参可以缺省标识符,于是有

void showData(const double [], int count);

2)若选择的数目为正,则可以将这个变量声明为unsigned int类型

3)这里我用了一个count来计算输入的数据个数,更高级的方法是,定义一个返回指向double类型数据的指针,该指针指向数组的第一个元素,于是有函数原型:

double* Input(double[], unsigned int);

调用时:

double *pScore = Input(Score, Size);

于是,就可以把pscore当成score来作为数组名来用了


3、有一结构声明

struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

**a.写一个函数,按值传递box结构,并显示每个成员的值
b.写一个函数,计算volume的值**

//7.3 cpp program
#include<iostream>
using namespace std;

struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

void ShowByValue(const box);
double calVolumeByaddress(box *);

int main()
{
    box boxByValue = {
        "Microsoft",
        0.09,
        0.265,
        0.324
    };
    cout << "the struct information of the box is : \n";
    ShowByValue(boxByValue);
    cout << "The volume of Xbox is: " << calVolumeByaddress(&boxByValue) << " m^3";
    while (1);
    return 0;
}

void ShowByValue(const box boxByValue)
{
    cout << "Maker: " << boxByValue.maker << " m" << endl;
    cout << "Height: " << boxByValue.height << " m" << endl;
    cout << "Width: " << boxByValue.width << " m" << endl;
    cout << "Length: " << boxByValue.length << " m" << endl;
}

double calVolumeByaddress(box *pbox){
    return pbox->volume = pbox->height*pbox->length*pbox->width;
}

心得:
1)首先是结构体的原型,不要忘记后面的分号

2)其次是结构体的初始化,用逗号隔开

3)显示结构体每个成员的值时,由于我不希望改变结构体的成员的值,所以用了const修饰符,这里也省略了结构体变量名

4)由于要传递结构的地址而不是整个结构以节省时间和空间。所以调用函数时,需要将结构体的地址(&box)而不是结构本身(box)传递给函数形参,将形参声明为指向box结构体的指针,即(box *)类型。
由于形参是指针而不是结构,因此应使用间接成员符(->)而不是成员运算符(.)


5、计算阶乘

//7.5 cpp program
#include<iostream> 
using namespace std;

long double Factorials(int);
int main()
{
    int Num;
    cout << "Enter the number's factorial you want to compute: ";
    while (cin >> Num)
    {
        if (Num < 0) { cout << "Invalid input, the program is terminated."; break; }
        else
        {
            cout << "The factorial of " << Num << " is:" << Factorials(Num) << endl;
        }
        cout << "Enter the number's factorial you want to compute: ";
    }
    while(1);
    return 0;
}

long double Factorials(int Num)
{
    long double Result = 1.;
    if (0 == Num || 1 == Num)
        Result = 1.;
    else
    {
        for (size_t i = 2; i <= Num; ++i)
            Result *= i;
    }
    return Result;
}

递归的话只要把else里面的for改成return num*Factorials(num-1)
前面的有关于Result的都删掉,换成return 1;即可。


5、

猜你喜欢

转载自blog.csdn.net/csdn_dzh/article/details/79776501