C++第九章编程练习答案

题一

头文件

test9_1.h

复制代码
#ifndef TEST9_1_H_
#define TEST9_1_H_
const int Len = 40;
struct golf
{
    char fullname[Len];
    int handicap;
};

void setgolf(golf &g, const char *name, int hc);
// 赋值函数

int setgolf(golf &g);
// 按输入赋值

void handicap(golf &g, int hc);
// 对handicap重新赋值

void showgolf(const golf &g);
#endif

源文件

复制代码
#include <iostream>
#include "test9_1.h"
#include <cstring>
using namespace std;

void setgolf(golf &g, const char *name, int hc)
{
    strcpy(g.fullname, name);
    g.handicap = hc;
}

int setgolf(golf &g)
{
    cout << "Enter the fullname :";
    cin.get(g.fullname, Len);
    cout << "Enter the handicap: ";
    cin >> g.handicap;
    cin.get();
    if (g.fullname[0] == '\0')
        return 0;
    else
        return 1;
}

void handicap(golf &g, int hc)
{
    g.handicap = hc;
}

void showgolf(const golf &g)
{
    if (g.fullname[0] != '\0')
    {
        cout << "fullname: " << g.fullname << endl;
        cout << "handicap: " << g.handicap << endl;
    }
}

源代码

复制代码
#include <iostream>
#include "test9_1.h"
using namespace std;
int main()
{
    golf ann;
    setgolf(ann, "Ann Birdfree", 24);
    showgolf(ann);

    golf *golfarr = new golf[3];
    for (int i = 0; i < 3 && setgolf(golfarr[i]); i++)
    {
    }
    for (int i = 0; i < 3; i++)
        showgolf(golfarr[i]);
    delete[] golfarr;
    return 0;
}

题二

复制代码
// static.cpp--using a static local variable
#include <iostream> //constants
#include <string>
#include <cstring>
using namespace std;
// const int ArSize = 10;
//  function prototype
void strcount(const string str);
int main()
{
    // char input[ArSize];
    string input;
    char next;
    cout << "Enter a line:\n";
    getline(cin, input);
    while (input != "")
    {
        strcount(input);
        cout << "Enter next line(empty line to quit);\n";
        getline(cin, input);
    }
    cout << "Bye\n";
    return 0;
}
void strcount(const string str)
{
    using namespace std;
    static int total = 0;
    int count = 0;
    // static local variable
    // automatic local variable
    cout << "\"" << str << "\"contains \n";
    count = str.size();
    total += count;
    cout << count << " characters\n";
    cout << total << " characters total\n";
}

题三

方法一

复制代码
#include <iostream>
#include <new>
#include <cstring>
const int BUF = 512;
const int N = 5;
char buffer[BUF];
struct chaff
{
    char dross[20];
    int salg;
};
int main()
{
    using namespace std;
    chaff *array1 = new (buffer) chaff[2];
    for (int i = 0; i < 2; i++)
    {
        cout << "dross" << i + 1 << ": ";
        cin.getline(array1[i].dross, 20);
        cout << "salg" << i + 1 << ":";
        cin >> array1[i].salg;
        cin.get();
    }

    for (int i = 0; i < 2; i++)
    {
        cout << "dross" << i + 1 << ": " << array1[i].dross << endl;

        cout << "salg" << i + 1 << ": " << array1[i].salg << endl;
    }
    return 0;
}

方法二

复制代码
#include <iostream>
#include <new>
#include <cstring>
const int BUF = 512;
struct chaff
{
    char dross[20];
    int salg;
};
int main()
{
    using namespace std;
    char *buffer = new char[BUF];
    chaff *array1 = new (buffer) chaff[2];
    for (int i = 0; i < 2; i++)
    {
        cout << "dross" << i + 1 << ": ";
        cin.getline(array1[i].dross, 20);
        cout << "salg" << i + 1 << ":";
        cin >> array1[i].salg;
        cin.get();
    }

    for (int i = 0; i < 2; i++)
    {
        cout << "dross" << i + 1 << ": " << array1[i].dross << endl;

        cout << "salg" << i + 1 << ": " << array1[i].salg << endl;
    }
    delete buffer;
    return 0;
}

题四

头文件

复制代码
#ifndef TEST9_4_H_
#define TEST9_4_H_
namespace SALES
{
    const int QUARTERS = 4;
    struct Sales
    {
        double sales[QUARTERS];
        double average;
        double max;
        double min;
    };

    void setSales(Sales &s, const double ar[], int n);

    void setSales(Sales &s);

    void showSales(const Sales &s);
}
#endif

源代码

复制代码
#include "test9_4.h"
#include <iostream>
using namespace std;

void SALES::setSales(Sales &s, const double ar[], int n)
{
    double sum;
    s.max = ar[0];
    s.min = ar[0];
    // int length = sizeof(ar) / sizeof(double);
    for (int i = 0; i < QUARTERS; i++)
    {
        if (i < n)
        {
            sum += ar[i];
            s.sales[i] = ar[i];
            if (ar[i] > s.max)
                s.max = ar[i];
            if (ar[i] < s.min)
                s.min = ar[i];
        }
        else
            s.sales[i] = 0;
    }
    s.average = sum / n;
}

void SALES::setSales(Sales &s)
{
    s.max = 0;
    s.min = 999999;
    for (int i = 0; i < QUARTERS; i++)
    {
        cout << "Enter " << i + 1 << "-th quarter sales:";
        cin >> s.sales[i];
        s.average += s.sales[i];
        if (s.sales[i] > s.max)
            s.max = s.sales[i];
        if (s.sales[i] < s.min)
            s.min = s.sales[i];
    }
    s.average /= QUARTERS;
}
void SALES::showSales(const Sales &s)
{
    for (int i = 0; i < QUARTERS; i++)
        cout << i + 1 << "-th quarter sales:" << s.sales[i] << endl;

    cout << "average :" << s.average << endl;
    cout << "max :" << s.max << endl;
    cout << "min :" << s.min << endl;
}

源代码

复制代码
#include <iostream>
#include "test9_4.h"
using namespace std;
using namespace SALES;
int main()
{
    Sales FirstYear;
    double earning[3] = {10, 20, 30};
    setSales(FirstYear, earning, 3);
    showSales(FirstYear);

    Sales SecondYear;
    setSales(SecondYear);
    showSales(SecondYear);
    return 0;
}
相关推荐
感哥7 小时前
C++ 面向对象
c++
CoovallyAIHub9 小时前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
沐怡旸9 小时前
【底层机制】std::shared_ptr解决的痛点?是什么?如何实现?如何正确用?
c++·面试
NAGNIP10 小时前
Serverless 架构下的大模型框架落地实践
算法·架构
moonlifesudo10 小时前
半开区间和开区间的两个二分模版
算法
moonlifesudo10 小时前
300:最长递增子序列
算法
CoovallyAIHub15 小时前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
使一颗心免于哀伤15 小时前
《设计模式之禅》笔记摘录 - 21.状态模式
笔记·设计模式
感哥15 小时前
C++ STL 常用算法
c++
CoovallyAIHub15 小时前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉