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:\n"; cin.get(sdu_i[i].name,len); cout << "Enter student" << i + 1 << "‘s age:\n"; 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 << "\n"; }}
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:\n"; cin.get(stu_i_ar[i].name,STU::len); cout << "Enter student" << i + 1 << "‘s age:\n"; if(!(cin >> stu_i_ar[i].age)) cout << "Bad input."; else {}; cin.get(); }}voID STU::input_student(student & stu_i,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 << "\n"; }}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
总结以上是内存溢出为你收集整理的《C++primerplus》第9章练习题全部内容,希望文章能够帮你解决《C++primerplus》第9章练习题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)