C++ Primer Plus 第六版 第七章课后编程练习答案

1.

#include <iostream>
using namespace std;
double f(int x, int y);

int main()
{
    int x,y;
    double ave;
    while (1)
    {
        cout << "Enter 2 num: ";
        cin >> x >> y;
        if(x==0 || y==0)
        {
            cout << "Error.\n";
            break;
        }
        else
        {
            ave = f(x, y);
            cout << "Average is: " << ave << endl;
        }
    }

    return 0;
}

double f(int x, int y)
{
    double ave;
    ave  = 2 * double (x * y)/(x + y);
    return ave;
}

2.

#include <iostream>
using namespace std;
int input(double arr[], int limit);
void dispaly(double arr[], int i);
double cal(double arr[], int i);
const int MAX = 10;

int main()
{
    double scores[MAX];
    int size = input(scores, MAX);
    dispaly(scores, size);
    double ave;
    ave = cal(scores, size);
    cout << "The average score: " << ave << endl;

    return 0;
}

int input(double arr[], int limit)
{
    int i;
    double score;
    cout << "Enter scores, 'q' to quit.\n";
    for(i=0; i<limit; i++)
    {
        cout << "Enter scores#" << i << ": ";
        cin >> score;
        if(!cin)  //允许用户提早结束输入,比如输入'q'
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Error\n";
            break;
        }
        arr[i] = score;
    }
    return i; //输入的个数
}

void dispaly(double arr[], int i)
{
    cout << "You entered " << i << " scores.\n";
    cout << "The scores are:\n";
    for(int j=0; j<i; j++)
        cout << arr[j] << "\t"; //在一行上显示所有成绩
    cout << endl;
}

double cal(double arr[], int i)
{
    double ave;
    double sum = 0;
    for(int j=0; j<i; j++)
        sum += arr[j];
    ave = sum / i;
    return ave;
}

3.

#include <iostream>
using namespace std;

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

void display(box tmp);
float cal(box * tmp);

int main()
{
    cout << "Here is the example: \n";
    box tmp;
    cout << "Enter maker: ";
    cin >> tmp.maker;
    cout << "Enter height: ";
    cin >> tmp.height;
    cout << "Enter width: ";
    cin >> tmp.width;
    cout << "Enter length: ";
    cin >> tmp.length;
    cout << "Enter volume: ";
    cin >> tmp.volume;
    cout << endl;
    display(tmp);
    float val;
    val = cal(&tmp);
    cout << "The calculation: " << val <<endl;

    return 0;
}

void display(box tmp) //按值传递结构,显示数据
{
    cout << "Maker: " << tmp.maker << endl;
    cout << "Height: " << tmp.height << endl;
    cout << "Width: " << tmp.width << endl;
    cout << "Length: " << tmp.length << endl;
    cout << "Volume: " << tmp.volume << endl;
}

float cal(box * tmp) //按地址传递结构,并计算数据
{
    float val;
    val = tmp->length * tmp->width * tmp->height;
    return val;
}

4.

#include <iostream>
using namespace std;
double cal(int num1, int num2, int bingo1, int bingo2);

int main()
{
    int num1, num2, bingo1, bingo2;
    double v;
    cout << "Enter Num1: ";
    cin >> num1;
    cout << "Enter Num2: ";
    cin >> num2;
    cout << "Enter Bingo1: ";
    cin >> bingo1;
    cout << "Enter Bingo2: ";
    cin >> bingo2;
    v = cal(num1, num2, bingo1, bingo2);
    cout << "Probability: " << v << endl;

    return 0;
}

double cal(int num1, int num2, int bingo1, int bingo2)
{
    double v1, v2, v3;
    v1 = double (bingo1)/num1;
    v2 = double (bingo2)/num2;
    v3 = v1 * v2;
    return v3;
}

5.

#include <iostream>
using namespace std;
int cal(int n);

int main()
{
    int n, result;
    while (1)
    {
        cout << "Enter num: ";
        cin >> n;
        if(!cin) //非数字输入则直接终止
        {
            cout << "Quit!";
            break;
        }
        else if (n<0) //当输入小于0,请重新输入正值
        {
            cout << "Error. Please enter positive num!";
            continue;
        }
        else if (n>=0)
        {
            result = cal(n);
            cout << "Calcvulation: " << result << endl;
        }
    }

    return 0;
}

int cal(int n)
{
    int result;
    if(n==0)
        result = 1;
    else
        result = n * cal(n-1);
    return result;
}

6.

#include <iostream>
using namespace std;
int Fill_array(double arr[], int size);
void Show_array(double arr[], int num);
void Reverse_array(double arr[], int num);
const int MAX=30;

int main()
{
    double arr[MAX];
    int num = Fill_array(arr, MAX); //填充数组,然后显示数组
    Show_array(arr, num);

    Reverse_array(arr, num); //反转数组,然后显示数组
    cout << "After reverse:\n";
    Show_array(arr, num);

    Reverse_array(arr, num); //先还原数组排序
    Reverse_array(arr+1, num-2); //反转数组中除第一个和最后一个元素之外的所有元素,然后显示数组
    //不能动首尾元素,故个数num-2。要从第二个元素开始,故地址是arr+1
    cout << "After reverse:\n";
    Show_array(arr, num);

    return 0;
}

int Fill_array(double arr[], int size) //将一个double数组的名称和长度作为参数
{
    int i;
    double j;
    for(i=0; i<size; i++)
    {
        cout << "Enter data:";
        cin >> j;
        if(!cin) //当输入了非数字时,停止
        {
            cout << "Error. Quit!\n";
            break;
        }
        else
            arr[i] = j;
    }
    return i; //返回实际输入了多少个数字
}

void Show_array(double arr[], int num)
{
    cout << "The array is:\n";
    for(int j=0; j<num; j++)
        cout << arr[j] << "\t";
    cout << "\n";
}

void Reverse_array(double arr[], int num)
{
    double tmp;
    for (int j = 0; j < (num / 2); j++)
    {
        tmp = arr[j];
        arr[j] = arr[num - 1 - j];
        arr[num - 1 - j] = tmp;
    }
}

7.

#include <iostream>
using namespace std;
double * fill_array(double * arr, double * limit);
void show_array(const double * arr, double * n);
void revalue(double r, double * arr, double * n);
const int MAX = 5;

int main()
{
    double properties[MAX];
    double * n = fill_array(properties,  properties+MAX);//注意形参limit对应的是properties+MAX
    show_array(properties, n);
    if(*n > 0)
    {
        cout << "Enter revaluatiojn factor: ";
        double factor;
        while (!(cin >> factor))
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input; Please enter a num: ";
        }
        revalue(factor, properties, n);
        show_array(properties, n);
    }
    cout << "Done.\n";
    return 0;
}

double * fill_array(double * arr, double * limit) //使用两个指针参数来表示区间,将按值传递数组改为按地址传递数组;返回值是指针,故fill_array类型是int*而非int
{
    double tmp;
    double * i;
    int j=0;
    for(i=arr; i<limit; i++, j++)  //从首地址起
    {
        cout << "Enter value#" << (j + 1) << ": ";
        cin >> tmp;
        if(!cin)
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input. Quit!"<<endl;
            break;
        }
        else if (tmp<0)
            break;
        *i = tmp;
    }
    return i;  //指针传递
}

void show_array(const double * arr, double * n)
{
    int j=0;
    const double * i;
    for(i = arr; i<n; i++,j++)
    {
        cout << "Property #" << (j + 1) << ": $";
        cout << *i << endl;
    }
}

void revalue(double r, double * arr, double * n)
{
    for(double * i = arr; i<n; i++)
        *i *= r;
}

8.

#include <iostream>
using namespace std;
const int Seasons = 4;
const char* Snames[Seasons] = {"Spring", "Summer", "Fall", "Winter"}; //用const char*数组存储表示季度名称的字符串
void fill(double * pa);
void show(double * da);

int main()
{
    double expenses[Seasons];
    fill(expenses);
    show(expenses);

    return 0;
}

void fill(double * pa) //用double数组存储开支; double * pa,同 double pa[]
{
    for(int i=0; i<Seasons; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> pa[i];
    }
}

void show(double * da)
{
    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;
}

9.

#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);
        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 << "Enter fullname: ";
        cin >> pa[i].fullname;
        cout << "Enter hobby: ";
        cin >> pa[i].hobby;
        cout << "Enter ooplevel: ";
        cin >> pa[i].ooplevel;
        cin.get(); //不能省,否则输入有问题!!!
        count++;
    }
    cout << "Enter over!\n";
    return count;
}

void display1(student st) //显示结构体信息
{
    cout << "Fullname: " << st.fullname << "\n";
    cout << "Hobby: " << st.hobby << "\n";
    cout << "Ooplevel: " << st.ooplevel << "\n";
}

void display2(const student *ps) //按指针显示结构体信息
{
    cout << "Fullname: " << ps->fullname << "\n";
    cout << "Hobby: " << ps->hobby << "\n";
    cout << "Ooplevel: " << ps->ooplevel << "\n";
}

void display3(const student pa[], int n) //指针指向首地址,显示结构体信息
{
    for(int i=0; i<n; i++)
    {
        cout << "Fullname: " << pa[i].fullname << "\n";
        cout << "Hobby: " << pa[i].hobby << "\n";
        cout << "Ooplevel: " << pa[i].ooplevel << "\n";
    }
}

10.

#include <iostream>
using namespace std;
double add(double x, double y);
double sub(double x, double y);
double calculate(double x, double y, double(*pf)(double x, double y) );

int main()
{
    while (1)
    {
        cout << "Enter 2 num ('q' to quit!): ";
        double x, y;
        cin >> x >> y;
        if(!cin)
        {
            cout << "Quit!\n";
            break;
        }
        double result1 = calculate(x, y, add);
        double result2 = calculate(x, y, sub);
        cout << "x" << "+" << "y" << "=" << result1 << endl;
        cout << "x" << "-" << "y" << "=" << result2 << endl;
    }

    return 0;
}

double add(double x, double y)
{
    return x+y;
}

double sub(double x, double y)
{
    return x-y;
}

double calculate(double x, double y, double(*pf)(double x, double y) ) //指针数组
{
    double result;
    result = (*pf)(x, y);
    return result;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值