zoukankan      html  css  js  c++  java
  • 《C++primerplus》第9章练习题

     1.(未使用原书例题)练习多文件组织。在一个头文件中定义一种学生的结构体,存储姓名和年龄,声明三个函数分别用于询问有多少个学生,输入学生的信息和展示学生的信息。在另一个源文件中给出所有函数的定义。在主程序中使用new初始化结构指针,调用三个函数。

    //main.cpp
    #include<iostream>
    #include"Extra.h"
    using namespace std;
    
    int main()
    {
        set_student();
    
        student * students_pt = new student[student_num];
    
        input_student(students_pt);
    
        show_student(students_pt);
    
        delete []students_pt;
        system("pause");
    }
    //Extra.h
    #ifndef _EXTRA_H_
    #define _EXTRA_H_
    
    #include<iostream>
    const int len = 30;
    
    struct student
    {
        char name[len];
        int age;
    };
    
    extern int student_num;
    
    void set_student();
    void input_student(student sdu[]);
    void show_student(student sdu[]);
    
    #endif // !_EXTRA_H_
    //DefineExtra.cpp
    #include<iostream>
    #include"Extra.h"
    using namespace std;
    
    int student_num = 1;
    
    void set_student()
    {
        cout << "How many students: ";
        if (!(cin >> student_num))
            cout << "Bad input.";
        else {};
        cin.get();    //消除换行符,以便cin.get输入
    }
    
    void input_student(student sdu_i[])
    {
        for (int i = 0; i < student_num; i++)
        {
            cout << "Enter student" << i + 1 << "'s name:
    ";
            cin.get(sdu_i[i].name, len);
            cout << "Enter student" << i + 1 << "'s age:
    ";
            if(cin >> sdu_i[i].age)
                cout << "Bad input.";
            else {};
            cin.get();  
        }
    }
    
    void show_student(student sdu_o[])
    {
        for (int i = 0; i < student_num; i++)
        {
            cout << "Student " << i + 1 << " | " << sdu_o[i].name << ", age " << sdu_o[i].age;
            cout << "
    ";
        }
    }

    2.(未使用原书例题,对应第四题)修改上题的程序,使用重载函数编写函数的其它版本,并使用命名空间包括和拓展。主程序的运行过程见注释。

    //main.cpp
    #include<iostream>
    #include"Extra.h"
    using namespace std;
    using namespace STU;
    
    int main()
    {
        //先询问有几个学生,然后使用new动态创建相应数量的结构体
        set_student();
        student * students_pt = new student[student_num];
    
        //输入学生的信息并展示
        input_student(students_pt);
        show_student(students_pt);
    
        //再定义一个student结构体,存储交换生的信息并展示
        student stu_transfer;
        char st[] = "Joseph";
        input_student(stu_transfer, st, 18);
        show_student(stu_transfer);
    
        delete []students_pt;
        system("pause");
    }
    //Extra.h
    #ifndef _EXTRA_H_
    #define _EXTRA_H_
    
    #include<iostream>
    #include<cstring>
    
    namespace STU
    {
        const int len = 30;
    
        struct student
        {
            char name[len];
            int age;
        };
    
        extern int student_num;
    
        void set_student();
        void input_student(student stu_i_ar[]);    //输入学生信息,交互式版本
        void input_student(student & stu_i, char str[], int age);    //输入学生信息,非交互式版本
        void show_student(const student stu_o_ar[]);    //展示结构体数组信息的版本
        void show_student(const student & stu_o);    //展示单个结构体信息的版本
    }
    #endif // !_EXTRA_H_
    //DefineExtra.cpp
    #include<iostream>
    #include"Extra.h"
    using namespace std;
    
    int STU::student_num = 1;
    
    void STU::set_student()
    {
        cout << "How many students: ";
        if (!(cin >> STU::student_num))
            cout << "Bad input.";
        else {};
        cin.get();    //消除换行符,以便cin.get输入
    }
    
    void STU::input_student(student stu_i_ar[])
    {
        for (int i = 0; i < STU::student_num; i++)
        {
            cout << "Enter student" << i + 1 << "'s name:
    ";
            cin.get(stu_i_ar[i].name, STU::len);
            cout << "Enter student" << i + 1 << "'s age:
    ";
            if(!(cin >> stu_i_ar[i].age))
                cout << "Bad input.";
            else {};
            cin.get();  
        }
    }
    
    void STU::input_student(student & stu_i, char str[], int age)
    {
        strcpy_s(stu_i.name, str);    //编译器提示使用
        stu_i.age = age;
    }
    
    void STU::show_student(const student stu_o_ar[])
    {
        for (int i = 0; i < STU::student_num; i++)
        {
            cout << "Student " << i + 1 << " | " << stu_o_ar[i].name << ", age " << stu_o_ar[i].age;
            cout << "
    ";
        }
    }
    
    void STU::show_student(const student & stu_o)
    {
        cout << "Trasfer student | " << stu_o.name << ", age " << stu_o.age;
    }

    程序运行结果:

    *编译过程中报了好多个编译器运行之前检查不出来的错,总结如下:

    1.使用string要注意,它是包含在标准命名空间内的,所以要么using namespace std,要么std::string。

    2.注意检查不同文件里声明的函数和其定义,使用的参数类型是不是一致的。

    3.strcpy()和strcpy_s()函数的使用注意事项:

    https://blog.csdn.net/leowinbow/article/details/82380252

  • 相关阅读:
    Python模糊查询本地文件夹去除文件后缀(7行代码)
    Python正则表达式
    python的logging模块
    Python中hashlib模块
    Python的os模块
    项目初始化mysql建库和授权
    Add correct host key in /root/.ssh/known_hosts to get rid of this message
    高中典型的等比数学题
    autoenv的使用方法
    celery任务进程关闭
  • 原文地址:https://www.cnblogs.com/banmei-brandy/p/11376537.html
Copyright © 2011-2022 走看看