C++ Prime Plus 第六版 第八章 函数探幽 编程练习 参考答案 详细版

这是一份详细的C++ Prime Plus第六版第八章的函数编程练习答案,所有答案已在Linux环境下使用VSCode进行测试并成功编译运行。
摘要由CSDN通过智能技术生成

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

8.1

#include<iostream>
using namespace std;
void fun(const char *, int flag = 0);
int main(void)
{
    const int size = 20;
    char str[size] = "I am is a str!!";
    fun(str);               //count == 1
    fun(str);               //count == 2
    fun(str,2020);          //count == 3, 总共打印5遍字符串
}
void fun(const char * p, int flag)
{
    static int count = 0;
    count++;
    if(!flag) 
        cout << p << endl;
    else
    {
        for(int i = 0; i < count; i++)
            cout << p << endl;
    }
}

8.2

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

struct CandyBar
{
    char name[20];
    double weight;
    int Calories;
};

void setCandyBar(CandyBar & CBr, const char * Name = "Millennium Munch", double Wg = 2.85, int Cal = 350);
void show(const CandyBar & CBr);

int main(void)
{
    CandyBar CB;
    char name[20]{0};
    double wg;
    int cal;
    cout << "***现在是默认参数:" << endl;
    setCandyBar(CB);
    show(CB);
    cout << "请输入品牌名称:";
    cin.getline(name, 30);
    cout << "请输入重量和热量:";
    cin >> wg >> cal; 
    setCandyBar(CB, name, wg, cal);
    cout << "***现在不是默认参数:" 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值