C++Primer Plus(第六版)第九章编程练习:

本文介绍了C++ Primer Plus第六版第九章的编程练习,涵盖字符串操作、数组赋值、getline使用及命名空间函数定义等知识点,通过实际编程示例进行讲解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一题
知识点

  • 在code::block中,不能直接用""来比较确认空字符串,不用string类的情况下还是最好用cstring函数确定长度来确定空串
  • 字符串数组单个元素赋值的时候末尾的\0要自己赋值,不然不会自动填充

golf.h

#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED
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);
void showgolf(const golf & g);

#endif // GOLF_H_INCLUDED

main

#include <iostream>
#include "golf.h"

using namespace std;
int main()
{
    golf test[5];
    setgolf(test[0], "happy time", 20);
    showgolf(test[0]);
    while(1)
    {
        int i = 1;
        if(setgolf(test[i]) && i < 5)
        {
            showgolf(test[i]);
            i++;
        }
        else
            break;
    }
    cout << "byebye";
    return 0;
}

define

// contain function called in file1
#include <iostream>
#include "golf.h"
#include <cstring>
void setgolf(golf & g, const char* name, int hc)
{
    for(int i = 0; i <= strlen(name); i++) //我尝试用for(int i = 0; name[i] != '\0'; i++)来做循环
        g.fullname[i] = name[i];           //这样可以不用使用cstring的函数,结果也是发现name指针末尾的\0不会赋值到数组,会多比较一段乱码出来
    g.handicap = hc;
}
int setgolf(golf & g)
{
    using namespace std;
    cout << "Enter the name and the value:"<< endl;
    cin.getline(g.fullname,Len);
    cin >> g.handicap;
    cin.get();
    if(strlen(g.fullname) == 0)         //我试过直接用q.fullname == ''来比较,但是无法触发分支,感觉是编译器的原因,应该用vs studio可以做到
        return 0;
    else
        return 1;
}
void handicap(golf & g, int hc)
{
    g.handicap = hc;
}

void showgolf(const golf & g)
{
    std::cout << g.fullname << std::endl;
    std::cout << g.handicap << std::endl;
}

一道题做尼玛半天。

第二题
知识点

  • 就是string为参数的getline记住别在前面加个cin.,而且格式getline(cin,str);
#include <iostream>
#include <string>

using namespace std;
void strcount(const string str);

int main()
{
    string input;
    cout << "input the line:\n";
    getline(cin,input);
    while(!input.empty())
    {
        strcount(input);
        cout << "enter next line (empty line to quit):\n";
        getline(cin,input);
    }
    return 0;
}

void strcount(const string str)
{
    static int total = 0;
    int count = 0;
    cout << "\"" << str << "\"contains" ;
    count = str.length();
    total += count;
    cout << count << " characters\n";
    cout << total << " characters total\n";
}

第三题:
知识点

  • 没什么好说的,就是分配空间的事情,分配缓冲区
#include <iostream>
#include <new>
#include <cstring>

using namespace std;
const int BUF = 10;
struct chaff
{
    char dross[20];
    int slag;
};

chaff buffer[BUF];

int main()
{
    chaff * pd = new (buffer) chaff[2];
    strcpy(pd[0].dross, "Bigdaddy");
    strcpy(pd[1].dross, "asskicker");
    pd[0].slag = 10;
    pd[1].slag = 20;
    for(int i = 0; i < 2; i++)
    {
        cout << pd[i].dross <<endl;
        cout << pd[i].slag << endl;
    }
    delete []pd;
    return 0;
}

第四题
知识点:

  • 在源代码中定义名称空间中函数时,要像声明的时候一样的格式,不能用using来定义,是不行的

head

#ifndef GOLF_H_INCLUDED
#define GOLF_H_INCLUDED
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 // GOLF_H_INCLUDED

definition

// contain function called in file1
#include <iostream>
#include "golf.h"

namespace SALES
{

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

    if(n < 4)
    {
        double temp = 0;
        double max = -100;
        double min = 100;
        for(int i = 0; i < n; i++)
            {
                s.sales[i] = ar[i];
                temp += s.sales[i];
                if(max < s.sales[i])
                    max = s.sales[i];
                if(min > s.sales[i])
                    min = s.sales[i];
            }
            s.average = temp / n;
            s.max = max;
            s.min = min;
        for(n; n < 4; n++)
            s.sales[n] = 0;
    }
    else
    {
        double temp = 0;
        double max = -100;
        double min = 100;
        for(int i = 0; i < n; i++)
            {
                s.sales[i] = ar[i];
                temp += s.sales[i];
                if(max < s.sales[i])
                    max = s.sales[i];
                if(min > s.sales[i])
                    min = s.sales[i];
            }
            s.average = temp / n;
            s.max = max;
            s.min = min;
    }

}
void setSales(Sales &  s)
{
    std::cout << "input the sales of 4 quarters:"<<std::endl;
    double ar[4];
    double temp = 0;
    double max = -100;
    double min = 100;
    for(int i = 0; i < 4; i++)
        std::cin >> ar[i];
    for(int i = 0; i < 4; i++)
            {
                s.sales[i] = ar[i];
                temp += s.sales[i];
                if(max < s.sales[i])
                    max = s.sales[i];
                if(min > s.sales[i])
                    min = s.sales[i];
            }
            s.average = temp / 4;
            s.max = max;
            s.min = min;
}
void showSales(const Sales & s)
{
    for(int i = 0; i < 4; i++)
    std::cout << "\n"<<s.sales[i]<< "\t";
    std::cout << "\n" << "average = " << s.average;
    std::cout << "\n" << "max = " <<s.max;
    std::cout << "\n" << "min = " <<s.min;
}

}

main

#include <iostream>
#include "golf.h"

using namespace std;
using namespace SALES;


int main()
{
    Sales test1,test2;
    double ar[4] = {15.25, 25.22, 50.22, 90.44};
    setSales(test1,ar,4);
    setSales(test2);
    showSales(test1);
    showSales(test2);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值