9.6编程练习

  1. 第一题

main.cpp 源文件

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

const int Max = 3;
int main() {

    char name[Len];
    int hand;
    golf people[Max];

    std::cout << "非交互式版本:请输入用户的全名和handicap:
";
    setgolf(people[0], "zxc vb", 1);
    setgolf(people[1], "asd fg", 2);
    setgolf(people[2], "qwe rt", 3);

    for (auto & i : people)
    {
        showgolf(i);
    }
    std::cout << "交互式版本:请输入用户的全名和handicap:
";
    int flag;
    for (auto & i : people)
    {
        flag = setgolf(i);
        if (flag)
            std::cout << "名字存在
";
        else
            std::cout << "名字不存在
";
    }
    std::cout << "将第一个用户的handicap设置为5:
";
    handicap(people[0], 5);

    for (auto & i : people)
    {
        showgolf(i);
    }

    return 0;
}

golf.cpp 源文件

//
// Created by ycy on 2021/2/24.
//

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

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

}

int setgolf(golf & g)
{
    std::cin.getline(g.fullname, Len);
    std::cin >> g.handicap;
    std::cin.get();
    if (strlen(g.fullname))
        return 1;
    return 0;
}

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

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

golf.h 头文件

//
// Created by ycy on 2021/2/24.
//

#ifndef FIRST_GOLF_H
#define FIRST_GOLF_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);

void showgolf(const golf & g);

#endif //FIRST_GOLF_H

CMakeLists.txt (我使用了cmake)

cmake_minimum_required(VERSION 3.17)  // camke版本
project(first)  // 项目名称

set(CMAKE_CXX_STANDARD 11)  // C++ 11版本,支持C++ 11新特性

add_executable(first main.cpp golf.h golf.cpp)  // 项目和包含的文件名

运行结果:

非交互式版本:请输入用户的全名和handicap:
zxc vb 1
asd fg 2
qwe rt 3
交互式版本:请输入用户的全名和handicap:
qwe
4
名字存在
rty 5
5
名字存在

6
名字不存在
将第一个用户的handicap设置为5:
qwe 5
rty 5 5
 6
  1. 第二题
#include <iostream>
#include <string>

void strcount(const std::string & str);

int main() {

    using namespace std;
    string input;

    cout << "Enter a line:
";
    getline(cin, input);
    while (cin && !input.empty())
    {
        strcount(input);
        cout << "Enter next line (empty line to quit):
";
        getline(cin, input);
    }

    cout << "Bye
";
    return 0;
}

void strcount(const std::string & str)
{
    using namespace std;
    static int total = 0;
    int count;

    cout << """ << str << "" contains ";
    count = int(str.length());
    total += count;
    cout << count << " characters
";
    cout << total << " characters total
";
}

运行结果:

Enter a line:
nice pants
"nice pants" contains 10 characters
10 characters total
Enter next line (empty line to quit):
thanks
"thanks" contains 6 characters
16 characters total
Enter next line (empty line to quit):
parting is such sweet sorrow
"parting is such sweet sorrow" contains 28 characters
44 characters total
Enter next line (empty line to quit):
ok
"ok" contains 2 characters
46 characters total
Enter next line (empty line to quit):

Bye

  1. 第三题
#include <iostream>
#include <cstring>

struct chaff
{
    char dross[20];
    int slag;
};
void second();

const int BUF = 512;
const int N = 2;
char buffer[BUF];

int main() {

    using std::cin;
    using std::cout;
    using std::endl;

    chaff ch[N] {};
    char *pd1;
    int *pd2;

    cout << "使用常规new运算符来分配缓冲区:
";
    cout << "Calling new and placement new:
";
    pd1 = new char [20];
    pd2 = new int;
    for (auto & i : ch)
    {
        cin.get(pd1, 20);
        cin >> *pd2;
        cin.get();
        strcpy(i.dross, pd1);
        i.slag = *pd2;
    }
    for (auto & k: ch)
        cout << k.dross << " " << k.slag << endl;

    delete [] pd1;
    delete pd2;

    second();

    return 0;
}

void second()
{
    std::cout << "使用静态数组用作缓冲区
";
    chaff ch[N] {};
    std::cout << (int *) ch << std::endl;
    char *pd3;
    int *pd4;
    std::cout << "Calling new and placement new:
";

    pd3 = new (buffer) char [20];
    pd4 = new (buffer + 20 * sizeof(char)) int;
    std::cout << (int * ) buffer << std::endl;

    for (auto & i : ch)
    {
        std::cin.get(pd3, 20);
        std::cin >> *pd4;
        std::cin.get();
        std::cout << (int *) pd3 << " " << pd4 << std::endl;
        strcpy(i.dross, pd3);
        i.slag = *pd4;
    }

    for (auto & k: ch)
    {
        std::cout << k.dross << " " << k.slag << std::endl;
        std::cout << &k.dross << " " << &k.slag << std::endl;
    }

    // 不可以通过delete删除定位new运算符发开辟的空间

}

运行结果:

使用常规new运算符来分配缓冲区:
Calling new and placement new:
qwe
1
ewq
2
qwe 1
ewq 2
使用静态数组用作缓冲区
0x61fcd0 // ch数组结构第一个数组的起始地址
Calling new and placement new:
0x408040  // buffer数组的起始地址
qw
3
0x408040 0x408054
wq
4
0x408040 0x408054  // 可以看到pd3地址和buffer数组地址起始相同,pd4地址是在buffer基础上加了20字节
qw 3
0x61fcd0 0x61fce4  // 该结构地址和ch数组结构对应,char[20]=20字节,所以第二个地址比第一个地址大20(16进制计算)
wq 4
0x61fce8 0x61fcfc  // int默认4字节,所以第一个地址是上面第二个地址加4之后的值,第二个同理加char[20]=20字节
  1. 第四题(使用了第一题的函数名字)
    交互式就是自己输入值,非交互式就是提前设定好值。
    头文件golf.h:
//
// Created by ycy on 2021/2/24.
//

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

golf.cpp第一个源代码文件:

//
// Created by ycy on 2021/2/24.
//

#include <iostream>
#include "golf.h"
namespace SALES
{
    using std::cin;
    using std::cout;
    using std::endl;
    void setSales(Sales & s, const double ar[], int n)
    {
        for (int i=0;i<QUARTERS;++i)
        {
            s.sales[i] = ar[i];
        }
        double sum = 0, max, min;
        max = ar[0];
        min = ar[0];
        sum += ar[0];
        for (int i=1;i<QUARTERS;++i)
        {
            if (max < ar[i])
                max = ar[i];
            if (min > ar[i])
                min = ar[i];
            sum += ar[i];
        }
        s.average = sum / QUARTERS;
        s.max = max;
        s.min = min;
    }

    void setSales(Sales & s)
    {
        for (double & sale : s.sales)
        {
            cin >> sale;
        }
        double sum = 0, max, min;
        max = s.sales[0];
        min = s.sales[0];
        sum += s.sales[0];
        for (int i=1;i<QUARTERS;++i)
        {
            if (max < s.sales[i])
                max = s.sales[i];
            if (min > s.sales[i])
                min = s.sales[i];
            sum += s.sales[i];
        }
        s.average = sum / QUARTERS;
        s.max = max;
        s.min = min;
    }

    void showSales(const Sales & s)
    {
        cout << "All number:";
        for (double sale : s.sales)
        {
            cout << sale << " ";
        }
        cout << "
average:";
        cout << s.average;
        cout << "
max:";
        cout << s.max;
        cout << "
min:";
        cout << s.min << endl;
    }
}

main.cpp第二个源代码文件:

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


int main() {

    using std::cin;
    using std::cout;
    using std::endl;
    using namespace SALES;

    Sales s1 {};
    Sales s2 {};
    double arr[4] = {1.0, 2.0, 3.0, 4.0};

    // 非交互式代码
    cout << "非交互式代码" << endl;
    setSales(s1, arr, QUARTERS);
    showSales(s1);
    // 交互式代码
    cout << "交互式代码" << endl;
    setSales(s2);
    showSales(s2);

    return 0;
}

运行结果:

非交互式代码
All number:1 2 3 4
average:2.5
max:4
min:1
交互式代码
4 5 6 7
All number:4 5 6 7
average:5.5
max:7
min:4
原文地址:https://www.cnblogs.com/ycycn/p/14456833.html