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

  1. //main.cpp
  2. #include<iostream>
  3. #include"Extra.h"
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. set_student();
  9.  
  10. student * students_pt = new student[student_num];
  11.  
  12. input_student(students_pt);
  13.  
  14. show_student(students_pt);
  15.  
  16. delete []students_pt;
  17. system("pause");
  18. }
  1. //Extra.h
  2. #ifndef _EXTRA_H_
  3. #define _EXTRA_H_
  4.  
  5. #include<iostream>
  6. const int len = 30;
  7.  
  8. struct student
  9. {
  10. char name[len];
  11. int age;
  12. };
  13.  
  14. extern int student_num;
  15.  
  16. void set_student();
  17. void input_student(student sdu[]);
  18. void show_student(student sdu[]);
  19.  
  20. #endif // !_EXTRA_H_
  1. //DefineExtra.cpp
  2. #include<iostream>
  3. #include"Extra.h"
  4. using namespace std;
  5.  
  6. int student_num = 1;
  7.  
  8. void set_student()
  9. {
  10. cout << "How many students: ";
  11. if (!(cin >> student_num))
  12. cout << "Bad input.";
  13. else {};
  14. cin.get(); //消除换行符,以便cin.get输入
  15. }
  16.  
  17. void input_student(student sdu_i[])
  18. {
  19. for (int i = 0; i < student_num; i++)
  20. {
  21. cout << "Enter student" << i + 1 << "'s name:\n";
  22. cin.get(sdu_i[i].name, len);
  23. cout << "Enter student" << i + 1 << "'s age:\n";
  24. if(cin >> sdu_i[i].age)
  25. cout << "Bad input.";
  26. else {};
  27. cin.get();
  28. }
  29. }
  30.  
  31. void show_student(student sdu_o[])
  32. {
  33. for (int i = 0; i < student_num; i++)
  34. {
  35. cout << "Student " << i + 1 << " | " << sdu_o[i].name << ", age " << sdu_o[i].age;
  36. cout << "\n";
  37. }
  38. }

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

  1. //main.cpp
  2. #include<iostream>
  3. #include"Extra.h"
  4. using namespace std;
  5. using namespace STU;
  6.  
  7. int main()
  8. {
  9. //先询问有几个学生,然后使用new动态创建相应数量的结构体
  10. set_student();
  11. student * students_pt = new student[student_num];
  12.  
  13. //输入学生的信息并展示
  14. input_student(students_pt);
  15. show_student(students_pt);
  16.  
  17. //再定义一个student结构体,存储交换生的信息并展示
  18. student stu_transfer;
  19. char st[] = "Joseph";
  20. input_student(stu_transfer, st, 18);
  21. show_student(stu_transfer);
  22.  
  23. delete []students_pt;
  24. system("pause");
  25. }
  1. //Extra.h
  2. #ifndef _EXTRA_H_
  3. #define _EXTRA_H_
  4.  
  5. #include<iostream>
  6. #include<cstring>
  7.  
  8. namespace STU
  9. {
  10. const int len = 30;
  11.  
  12. struct student
  13. {
  14. char name[len];
  15. int age;
  16. };
  17.  
  18. extern int student_num;
  19.  
  20. void set_student();
  21. void input_student(student stu_i_ar[]); //输入学生信息,交互式版本
  22. void input_student(student & stu_i, char str[], int age); //输入学生信息,非交互式版本
  23. void show_student(const student stu_o_ar[]); //展示结构体数组信息的版本
  24. void show_student(const student & stu_o); //展示单个结构体信息的版本
  25. }
  26. #endif // !_EXTRA_H_
  1. //DefineExtra.cpp
  2. #include<iostream>
  3. #include"Extra.h"
  4. using namespace std;
  5.  
  6. int STU::student_num = 1;
  7.  
  8. void STU::set_student()
  9. {
  10. cout << "How many students: ";
  11. if (!(cin >> STU::student_num))
  12. cout << "Bad input.";
  13. else {};
  14. cin.get(); //消除换行符,以便cin.get输入
  15. }
  16.  
  17. void STU::input_student(student stu_i_ar[])
  18. {
  19. for (int i = 0; i < STU::student_num; i++)
  20. {
  21. cout << "Enter student" << i + 1 << "'s name:\n";
  22. cin.get(stu_i_ar[i].name, STU::len);
  23. cout << "Enter student" << i + 1 << "'s age:\n";
  24. if(!(cin >> stu_i_ar[i].age))
  25. cout << "Bad input.";
  26. else {};
  27. cin.get();
  28. }
  29. }
  30.  
  31. void STU::input_student(student & stu_i, char str[], int age)
  32. {
  33. strcpy_s(stu_i.name, str); //编译器提示使用
  34. stu_i.age = age;
  35. }
  36.  
  37. void STU::show_student(const student stu_o_ar[])
  38. {
  39. for (int i = 0; i < STU::student_num; i++)
  40. {
  41. cout << "Student " << i + 1 << " | " << stu_o_ar[i].name << ", age " << stu_o_ar[i].age;
  42. cout << "\n";
  43. }
  44. }
  45.  
  46. void STU::show_student(const student & stu_o)
  47. {
  48. cout << "Trasfer student | " << stu_o.name << ", age " << stu_o.age;
  49. }

程序运行结果:

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

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

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

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

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

《C++primerplus》第9章练习题的更多相关文章

  1. 《C++ primerplus》第13章练习题

    1.对CD类的派生练习.基类CD类存储作者和作品号等信息,派生类Classic额外增加一格"主要作品"的信息.主函数使用拷贝构造函数.按引用传递参数的函数和指针来测试基类和派生类的 ...

  2. 《C++primerplus》第12章练习题

    做一下倒数两题,都是在队列模拟的程序基础上做点修改测试. 5.找出平均等候时间为1分钟时,每小时到达的客户数为多少(试验时间不少于100小时). 指定队伍最大长度10人,模拟100小时.粗略估计答案在 ...

  3. 《C++primerplus》第11章练习题

    1.修改程序清单11.5(随机漫步),使之以特定的格式将结果写入文件中. //vector.h -- Vector Class #ifndef _VECTOR_H_ #define _VECTOR_H ...

  4. 《C++primerplus》第10章练习题

    1.定义一个类表示银行账户.数据成员包括姓名,账号和存款.成员函数可以执行初始化数据.显示数据和取款存款的功能. //Bank.cpp #include<iostream> #includ ...

  5. 《C++primerplus》第8章练习题

    1.(简单用一下引用变量,没有采用书中的题目)定义一个替身结构体,存储名字(char[])和力量值(int).使用结构体引用作为形参写两个函数,一个不加const,使得能对定义的结构体做修改,另一个加 ...

  6. 《C++primerplus》第7章练习题

    1.用户不断输入两个数,计算调和平均数,直到其中一个数为0. #include<iostream> using namespace std; double harm_mean(double ...

  7. 《C++primerplus》第6章练习题

    本来前面五题都做完了,写博客时没保存好草稿= =,写了个整合版的程序,实现前五题的关键部分. 1.定义一个叫jojo的结构,存储姓名.替身和力量值,使用动态结构数组初始化二乔.承太郎和乔鲁诺乔巴纳等人 ...

  8. 《C++primerplus》第4章练习题

    注:略过部分题目,修改了题设要求,实现差不多的功能 1.使用字符数组.要求用户输入姓名,等第和年龄,输出其姓名和年龄,等第降一级(即字母高一级). #include<iostream> u ...

  9. python第一章练习题

    本章总节 练习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...

随机推荐

  1. webpack做项目优化

    webpack优化 -- compression-webpack-plugin 开启gzip 打包的时候开启gzip可以大大减少体积,非常适合于上线部署.下面以vue-cli2.x项目为例,介绍如何在 ...

  2. Go语言使用swagger生成接口文档

    swagger介绍 Swagger本质上是一种用于描述使用JSON表示的RESTful API的接口描述语言.Swagger与一组开源软件工具一起使用,以设计.构建.记录和使用RESTful Web服 ...

  3. 09vuex

    state 大白话:获取state的值 vuex中state数据是响应式的.只要state中数据发生变化.vue组件自动更新. 要想做到响应式 前提:是在mutaion中定义函数修改state值. 最 ...

  4. 剑指 Offer 42. 连续子数组的最大和

    题目描述 输入一个整型数组,数组中的一个或连续多个整数组成一个子数组.求所有子数组的和的最大值. 要求时间复杂度为\(O(n)\). 示例1: 输入: nums = [-2,1,-3,4,-1,2,1 ...

  5. Fabric1.4 kafka共识的多orderer集群

    https://www.cnblogs.com/zhangmingcheng/p/10556469.html#FeedBack https://yq.aliyun.com/articles/63746 ...

  6. Ubuntu 18.04 LTS 配置静态IPv6地址

    学校的IPv4地址限制了校内IP访问,在家连校内机器只能先连接学校的VPN,十分不方便.好在学校没有对IPv6地址做限制,因此我们可以给自己的机器配置一个静态IPv6地址来绕过这个限制. 本机系统使用 ...

  7. SpringMVC-乱码问题

    乱码问题 目录 乱码问题 1. 使用原生filter解决 1. 前端jsp 2. 编写controller 3. 编写过滤器 4. 注册过滤器 2. 使用SpringMVC提供的过滤器实现 1. 使用 ...

  8. 使用SVG symbols建立图标系统

    在实现Web项目的图标系统时,SVG是一个不错的选择.虽然使用SVG创建图标系统有多种方式.在这篇文章中,我们只看其中一种:SVG symbols.这项技术基于两个元素的使用:<symbol&g ...

  9. linux下redis安装部署

    1.获取redis资源 进官网下载https://redis.io/download最新版本后将文件移到linux环境中 或者直接wget http://download.redis.io/relea ...

  10. 如何创建本地git分支到远程

    创建本地分支到远程: 1.$git init 之后创建的本地仓库默认master分支 如果现在就要$ git branch 查看当前分支,是不显示任何分支的,只有在add,commit文件之后才显示, ...