C++ Prime Plus 第六版 第七章 函数——C++模块 编程练习 参考答案 详细版

以下答案本人在linux vscode中均已亲自测试编译通过,完美运行.

复习题8:

#include<iostream>
using namespace std;
int replace(char * str, char c1, char c2);
int main()
{
    const int size = 20;
    char str[size] = "ajajjaca";
    cout << str << endl;
    replace(str,'a','@');
    cout << str << endl;
}
int replace(char * str, char c1, char c2)
{
    int count =0;
    for(int i=0; str[i] != '\0'; i++ )
    {
        if(str[i] == c1)
        {
            str[i] = c2;
            count++;
        }
    }
    cout << count << endl;
}

复习题12:

#include<iostream>
using namespace std;
struct appliant
{
    char name[30];
    int credit_ratings[3];
};
void show1(appliant see);
void show2(appliant * see);
int main()
{
    appliant wu{"wu1234456",1,2,3};
    show1(wu);
    show2(&wu);
}
void show1(appliant see)
{
    cout << see.name << endl;
    for(int i=0; i < 3; i++ )
        cout << see.credit_ratings[i] << endl;
}
void show2(appliant * see)
{
    cout << see->name << endl;
    for(int i=0; i < 3; i++ )
        cout << see->credit_ratings[i] << endl;
}

编程练习:

7.1

#include<iostream>
using namespace std;
double funave(double x, double y);
int main()
{
    double x, y,ave;
    cout << "请输入两个数:";
    while(cin >> x >> y)
    {
        if(0 == x || y == 0)
            break;
        ave = funave(x, y);
        cout << "调和平均数=" << ave << endl;
        cout << "请输入两个数:";
    }
}
double funave(double x, double y)
{
    double ave = 0.0;
    ave = 2.0 * x * y / (x + y);
    return ave;
}

7.2

#include<iostream>
using namespace std;
void input(double score[],int size);
void show(double score[]);
double funave(double score[]);
int main()
{
    const int size = 10;
    double score[size]{0};
    double ave = 0;
    input(score,size);
    show(score);
    ave = funave(score);
    cout << "\n平均成绩:" << ave << endl; 
}
void input(double score[], int size)
{
    int i = 0;
    cin >> score[i];
    while((cin.get() != '\n') && (i < size))  //可以提前结束输入
    {
        i++;
        cin >> score[i];
    }
}
void show(double score[])
{
    int i = 0;
    while(score[i] != 0)
    {
        cout << score[i] << ' ';
        i++;
    }
}
double funave(double score[])
{
    int i = 0;
    double ave = 0, sum = 0;
    while(score[i] != 0)
    {
        i++;
        sum += score[i];
    }
    ave = sum / i;
    return ave;
}

7.3

#include<iostream>
using namespace std;
struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
void show1(box see);
void show2(box * see);
int main()
{
    box wu{"I am a box!!!",10,20,30,40};
    show1(wu);
    show2(&wu);
    show1(wu);
}
void show1(box see)
{
    cout << see.maker << endl
         << see.height << endl
         << see.width << endl
         << see.length << endl
         << see.volume << endl;
}
void show2(box * see)
{
    see->volume = see->height * see->width * see->length;
}

7.4

#include<iostream>
using namespace std;
long double fun(unsigned, unsigned, unsigned, unsigned);
int main()
{
    unsigned total1, total2, picks1, picks2;
    cout << "请输入域号码总数,选择域号码数h,特选号码总数,选择特选号码数:";
    while (cin >> total1 >> picks1 >> total2 >> picks2 && (picks1 < total1 && picks2 < total2 ))
    {
        cout << "你玩一次中奖的概率是:" << fun(total1, picks1, total2, picks2) << endl;
        cout << "请输入下-组数据 (输入q退出):" << endl;
    } 
    cout << "Bye!!" << endl;
}
long double fun(unsigned total1, unsigned picks1, unsigned total2, unsigned picks2)
{
    long double result = 1.0, result1 = 1.0, result2 = 1.0, n = 0;
    unsigned p;
    for(n = total1, p = picks1; p > 0; n--, p--)
        result1 = result1 * n / p;
    for(n = total2, p = picks2; p > 0; n--, p--)
        result2 = result2 * n / p;
    result = result1 * result2;
    return result;
}

7.5

#include<iostream>
using namespace std;
unsigned fun(int n);
int main()
{
    unsigned n, result;
    cout << "请输入一个值:";
    while(cin >> n)
    {
        result = fun(n);
        cout << n <<"的阶乘是:" << result << endl;
        cout << "请输入下一个值(输入q结束程序):";
    }
}
//经测试最大只能算33! 33的阶乘是:2147483648
unsigned fun(int n)
{
    unsigned result;
    if(n < 0)
    {
        cout << "你的输入不合法,请重新输入!!!" << endl;
        return 0;
    }
    else if(0 == n || 1 == n)
        result = 1;
    else
        result = fun(n-1) * n;
    return result;
}

7.6

#include<iostream>
using namespace std;
int Fill_array(double arr[],int size);
void Show_array(double arr[],int size);
void Reverse_array(double arr[],int size);
void Reverse_array_1(double arr[],int size);
int main()
{   
    const int size = 10;
    int n;
    double arr[size]{0};
    n = Fill_array(arr, size);
    cout << "输入数组如下:" << endl;
    Show_array(arr, n);
    Reverse_array(arr, n);
    cout << "反转数组如下:" << endl;
    Show_array(arr, n);
    Reverse_array_1(arr, n);
    cout << "(首尾不变)反转数组如下:" << endl;
    Show_array(arr, n);
}
int Fill_array(double arr[],int size)
{
    int i = 0;
    cout << "请输入一系列小数(最多10个):" << endl;
    while(i < size && cin >> arr[i])
        i++;
    return i;
}
void Show_array(double arr[],int size)
{
    int i = 0;
    while(i < size)
    {
        cout << arr[i] << ' ';
        i++;
    }
    cout << endl;
}
void Reverse_array(double arr[],int size)
{
    int i;
    double temp;
    for(i = 0; i < size/2; i++)
    {
        temp = arr[i];
        arr[i] = arr[size-1-i];
        arr[size-1-i] = temp;
    }
}
void Reverse_array_1(double arr[],int size)
{
    int i;
    double temp;
    for(i = 1; i <= (size-2)/2; i++)
    {
        temp = arr[i];
        arr[i] = arr[size-1-i];
        arr[size-1-i] = temp;
    }
}

7.7

#include<iostream>
using namespace std;
const int MAX = 5;
double * Fill_array(double * begin, double * end);
void Show_array(const double * begin, const double * end);
void revalue(double r, double * begin, double * end);
int main()
{
    double properties[MAX]{0};
    double * end =  Fill_array(properties, properties + MAX);
    Show_array(properties, end);
    if(end > properties)
    {   
        cout << "Enter revaluation factor:";
        double factor;
        while(!(cin >> factor))
        {
            cin.clear();
            while(cin.get() != '\n')
                continue;
            cout << "输入错误!!请重新输入一个小数: ";
        }
        revalue(factor, properties, end);
        Show_array(properties, end);
    }
    cout << "Done.\n" << endl;
}
double * Fill_array(double * begin, double * end)
{
    double * pt;
    double temp;
    int i = 0;
    for(pt = begin; pt != end; pt++, i++)
    {
        cout << "Enter value #" << (i+1) << ":$";
        cin >> temp;
        if(!cin)
        {
            cin.clear();
            while(cin.get() != '\n')
                continue;
            cout << "输入错误,停止输入!!!" << endl;
            break;
        }
        else if(temp < 0)
            break;
        *pt = temp;
    }
    return pt;
}
void Show_array(const double * begin, const double * end)
{
    const double * pt;
    int i = 0;
    for(pt = begin; pt != end; pt++)
    {
        cout << "Property #" << (i+1) << ":$";
        cout << *pt << endl;
        i++;
    }
}
void revalue(double r, double * begin, double * end)
{
    double * pt;
    for(pt = begin; pt != end; pt++)
        *pt *= r;
}

7.8

#include<iostream>
#include<string>
using namespace std;

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

const int season = 4;
const char * Pseason[season]{"Spring", "Summer", "Fall", "Winter"};
int main()
{   
    double expenses[season]{0};
    fill(expenses);
    show(expenses);
}
void fill(double * pa)
{
    for(int i = 0; i < season; i++)
    {
        cout << "Enter " <<  Pseason[i] << " expenses:";
        cin >> pa[i];
    }
}
void show(double * pa)
{
    double total = 0.0;
    cout << "\nEXPENSES\n";
    for(int i = 0; i < season; i++)
    {
        cout  <<  Pseason[i] << ":$" << pa[i] << endl;
        total += pa[i];
    }
    cout << "Total Expenses:$" << total << endl;
}

7.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)
{
    for(int i = 0; i < n; i++)
    {
        cout << "Enter your fullname: ";
        cin.getline(pa[i].fullname, SLEN);
        cout << "Enter your hobby: ";
        cin.getline(pa[i].hobby, SLEN);
        cout << "Enter your ooplevel: ";
        cin >> pa[i].ooplevel;
        cin.get();
    }
    return n;
}
void display1(student st)
{
    cout << "your fullname is: " << st.fullname << endl;;
    cout << "your hobby is: " << st.hobby << endl;
    cout << "your ooplevel is: " <<  st.ooplevel << endl;
}
void display2(const student * ps)
{
    cout << "your fullname is: " << ps->fullname << endl;;
    cout << "your hobby is: " << ps->hobby << endl;
    cout << "your ooplevel is: " <<  ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
    for(int i = 0; i < n; i++)
    {
        cout << "your fullname is: " << pa[i].fullname << endl;;
        cout << "your hobby is: " <<  pa[i].hobby << endl;;
        cout << "your ooplevel is: " << pa[i].ooplevel << endl;;
    }
}

7.10

#include<iostream>
using namespace std;
double add(double, double);
double less1(double x, double y);
double mul(double x, double y);
double div(double x, double y);
double calculate(double, double, double (*f)(double x, double y));

int main()
{  
    double x, y, q;
    double (*pf[4])(double x, double y){add, less1, mul, div};
    cout << "请输入两个小数:";
    while(cin >> x >> y)
    {
        for(int i = 0; i < 4; i++)
        {
            q = calculate(x, y, pf[i]);
            if(0 == i)
                cout << " x + y = ";
            else if(1 == i)
                cout << " x - y = ";
            else if(2 == i)
                cout << " x * y = ";
            else
                cout << " x / y = ";
            cout << q << endl;
        }
        cout << "请输入两个小数:";
    }
    return 0;
}
double add(double x, double y)
{
    return x + y; 
}
double less1(double x, double y)
{
    return x - y;
}
double mul(double x, double y)
{
    return x * y;
}
double div(double x, double y)
{
    return x / y;
}
double calculate(double x, double y, double (*f)(double x, double y))
{
    return f(x, y);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值