C++ Primer Plus(第6版)Chapter 7 编程题答案

C++ Primer Plus(第6版)Chapter 7 编程题答案

第1题:

// task 1
#include <iostream>
#include <cstring>
double funct(double, double);

int main()
{
    using namespace std;
    double x, y;
    cout << "Enter the x and y: ";
    while (cin >> x >> y && (x != 0 && y != 0))
    {
        double result = funct(x, y);
        cout << "for " << x << " and " << y << " ,result is " << result << endl;
        cout << "Continue enter the x and y: ";
    }
    return 0;
}

double funct(double m, double n)
{
    return 2.0*m*n / (m + n);
}

第2题:

// task 2
int input(double *, int);
void display(const double*, int);
double average(const double*, int);
int main()
{
    using namespace std;

    const int SIZE = 10;
    double score[SIZE];
    int count = input(score, SIZE);
    display(score, count);
    if (count > 0)
    {
        double avg = average(score, count);
        cout << "The average is " << avg << endl;
    }

    cin.get();
    cin.get();
    cin.get();
    return 0;
}

int input(double* score, int size)
{
    using namespace std;
    int count = 0;
    cout << "Enter the most 10 golf scores: ";
    for (int i = 0; i < size; i++)
    {
        if (!(cin >> score[i]))
            break;
        count++;
    }
    return count;
}

void display(const double* score, int count)
{
    using std::cout;
    using std::endl;
    for (int i = 0; i < count; i++)
        cout << i + 1 << "st: " << score[i] << endl;
}

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

第3题:

// task 3
struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
void show_box(box);
void set_box(box*);
int main()
{
    using std::cin;
    box b1 = { "Golden State", 3.0, 4.0, 5.0, 60.0 };
    box b2 = { "Boston Green", 6.0, 8.0, 10.0, 0 };
    show_box(b1);
    set_box(&b2);
    show_box(b2);
    cin.get();
    return 0;

}

void show_box(box b)
{
    using std::cout;
    using std::endl;
    cout << "By value:\n";
    cout << "maker: " << b.maker << endl;
    cout << "height: " << b.height << endl;
    cout << "width: " << b.width << endl;
    cout << "length: " << b.length << endl;
    cout << "volume: " << b.volume << endl;

}

void set_box(box* pb)
{
    pb->volume = pb->width*pb->height*pb->length;
}

第4题:

// task 4
long long luck(int, int);
int main()
{
    using namespace std;
    long long t = luck(47, 27);
    cout << "The possibility of prize is 1/" << t << endl;

    cin.get();
    cin.get();
    return 0;
}
long long luck(int m, int n)
{
    long long a = 1;
    for ( double i = 1; i <= 5; i++) //double i 而不能是 int i
        a *= ((42 + i) / i);

    return a * 27;
}

第5题:

// task 5
int jiecheng(int);
int main()
{
    using namespace std;
    int n;
    cout << "Enter a num: ";
    while (cin >> n)
    {
        int result = jiecheng(n);
        cout << n << "! is " << result << endl;
        cout << "Again(q to quit): ";
    }

    cin.get();
    cin.get();
    return 0;
}

int jiecheng(int n)
{
    using std::cout;
    using std::endl;
    if (n == 0)
    {
        return 1;
    }
    else
        return n*jiecheng(n - 1);
}

第6题:

// task 6
int Fill_array(double*, int);
void Show_array(const double*, int);
void Reverse_array(double*, int);
const int SIZE = 5;
using namespace std;

int main()
{
    double arr[SIZE];
    int length = Fill_array(arr, SIZE);
    Show_array(arr, length);
    Reverse_array(arr, length);
    Show_array(arr, length);

    cin.get();
    cin.get();
    return 0;

}

int Fill_array(double* arr, int SIZE)
{
    int length = 0;

    for (int i = 0; i < SIZE; i++)
    {
        cout << "Enter the " << i + 1 << "st num: ";
        if (!(cin >> arr[i]))
            break;

        length++;
    }
    return length;
}

void Show_array(const double* arr, int length)
{
    cout << endl;
    for (int i = 0; i < length; i++)
    {
        cout << "The " << i + 1 << "st num is " << arr[i] << endl;
    }
}

void Reverse_array(double* arr, int length)
{
    if (length <= 2)
    {
        cout << "No need Reverse  " << endl;
        return;
    }
    int i = 1, j = length - 2;
    double temp;
    for (; i < j; i++, j--)
    {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

第7题:

// task 7
using namespace std;
const int MAX = 5;
double* fill_array(double ar[], double* maxend);
void show_array(const double ar[], const double* end);
void revalue(double r, double ar[], double* end);

int main()
{
    double properties[MAX];
    double* end = fill_array(properties, properties + MAX);
    show_array(properties, end);
    if ((end - properties) > 0)
    {
        cout << "Enter revalution factor: ";
        double factor;
        while (!(cin >> factor))
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input,Please enter a number: ";
        }
        revalue(factor, properties, end);
        show_array(properties, end);
    }
    cout << "Done.\n";

    cin.get();
    cin.get();
    return 0;
}

double* fill_array(double ar[], double* maxend)
{
    double temp;
    int i;
    double *begin = ar;
    for (i = 0; begin < maxend ; i++, begin++)
    {
        cout << "Enter value #" << (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 begin;   //返回末尾元素后面一个地址
}

void show_array(const double ar[], const double* end)
{
    const double* begin = ar;
    for (int i=0; begin < end; begin++,i++)
    {
        cout << "Property #" << (i+1) << ": $" << *begin << endl;
    }
}

void revalue(double r, double ar[], double* end)
{
    for (; ar < end; ar++)
    {
        *ar *= r;
    }
}

第8题:

// task 8.1
using namespace std;
const int Seasons = 4;
const char* Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };


void fill(double *);
void show(const double *);

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

    cin.get();
    cin.get();
    return 0;
}

void fill(double* pa)
{
    for (int i = 0; i < Seasons; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> pa[i];
    }
}

void show(const double* pa)
{
    double total = 0.0;
    cout << "\nEXPENSES\n";
    for (int i = 0; i < Seasons; i++)
    {
        cout << Snames[i] << ": $" << pa[i] << endl;
        total += pa[i];
    }
    cout << "Total Expenses: $" << total << endl;
}

//task 8.2
using namespace std;
const int Seasons = 4;
const char* Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
struct Estruct
{
    double expenses[Seasons];
};

void fill(Estruct *);
void show(Estruct);

int main()
{
    Estruct es1;
    fill(&es1);
    show(es1);

    cin.get();
    cin.get();
    return 0;
}

void fill(Estruct* pes)
{
    for (int i = 0; i < Seasons; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> pes->expenses[i];
    }
}

void show(Estruct es)
{
    double total = 0.0;
    cout << "\nEXPENSES\n";
    for (int i = 0; i < Seasons; i++)
    {
        cout << Snames[i] << ": $" << es.expenses[i] << endl;
        total += es.expenses[i];
    }
    cout << "Total Expenses: $" << total << endl;
}

第9题:

// task 9
using namespace std;
const int SLEN = 30;
struct student
{
    char fullname[SLEN];
    char hobby[SLEN];
    int ooplevel;
};
int getinfo(student[], int);
void display1(student);
void display2(const student*);
void display3(const student[], int);

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";

    cin.get();
    cin.get();
    return 0;
}

int getinfo(student pa[], int n)
{
    int num = 0;
    for (int i = 0; i < n; i++)
    {
        cout << "Enter the " << i + 1 << "st student's fullname: ";
        cin.getline(pa[i].fullname, SLEN - 1);
        if (strlen(pa[i].fullname) == 0)  //提前结束
            break;

        cout << "Enter the " << i + 1 << "st student's hobby: ";
        cin.getline(pa[i].hobby, SLEN - 1);
        cout << "Enter the " << i + 1 << "st student's ooplevel: ";
        cin >> pa[i].ooplevel;
        cin.get();   //吃掉换行

        num++;
    }
    return num;
}
void display1(student st)
{
    cout << "Display 1: " << endl;
    cout << st.fullname << "\t\t" << st.hobby << "\t\t" << st.ooplevel << endl;
}
void display2(const student* ps)
{
    cout << "Display 2: " << endl;
    cout << ps->fullname << "\t\t" << ps->hobby << "\t\t" << ps->ooplevel << endl;
}

void display3(const student pa[], int n)
{
    cout << endl << endl << "Display 3: " << endl;
    for (int i = 0; i < n; i++)
    {
        if (i % 2)
            display1(pa[i]);
        else
            display2(&pa[i]);
    }
}

第10题:

// task 10
double add(double, double);
double add_s(double, double);
double add_plus(double, double);
double calculate(double, double, double(*)(double, double)); // ()很重要

using namespace std;

int main()
{
    double m, n;
    double(*pa[3])(double, double) = { add, add_s, add_plus };

    cout << "Enter x and y: ";
    while (cin >> m >> n)
    {
        for (int i = 0; i < 3; i++)
        {
            double res = calculate(m, n, pa[i]);
            cout << "for the function " << i + 1 << " the result is " << res << endl;
        }

        cout << endl << "Enter again: ";
    }
}

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

double add_s(double x, double y)
{
    return x + 2*y;
}

double add_plus(double x, double y)
{
    return x + 3*y;
}

double calculate(double m, double n, double(*pf)(double x, double y))
{
    return pf(m, n);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值