c++ primer plus 第七章《编程题7.13.9》
2015-08-19 15:34
471 查看
#include <iostream> using namespace std; const int SLEN = 30; struct student { char fullname[SLEN]; char hobby[SLEN]; int ooplevel; }; int getinfo(student pa[], int n); void display1(student st); void display2(const student * ps); void display3(const student pa[], int n); int main() { cout << "Enter class size: "; int class_size; cin >> class_size; while (cin.get() != '\n') ; cout << "-------------------------------------------------------\n"; student * ptr_stu = new student[class_size]; int entered = getinfo(ptr_stu, class_size); for (int i = 0; i < entered; i++) { display1(ptr_stu[i]); display2(&ptr_stu[i]); } display3(ptr_stu, entered); delete [] ptr_stu; cout << "-----------------------------------\n"; cout << "Done\n"; return 0; } int getinfo(student pa[], int n) { int count = 0; while (count < n) { cout << "Enter the " << count+1 << " student name: "; if(!(cin.get(pa[count].fullname, SLEN))) break; while (cin.get() != '\n') ; cout << "Enter the " << count+1 << " student hobby: "; cin.get(pa[count].hobby, SLEN); while (cin.get() != '\n') ; cout << "Enter the " << count+1 << " student ooplevel: "; cin >> pa[count].ooplevel; while (cin.get() != '\n') ; count++; cout << "----------------------------------------------------\n"; } return count; } void display1(student st) { cout << "The student information: " << "\n"; cout << "name:\t" << st.fullname << "\n"; cout << "hobby:\t" << st.hobby << "\n"; cout << "ooplevel:\t" << st.ooplevel << "\n"; cout << "-------------------------------------------------------\n"; return; } void display2(const student * ps) { cout << "The student information: " << "\n"; cout << "name:\t" << ps->fullname << "\n"; cout << "hobby:\t" << ps->hobby << "\n"; cout << "ooplevel:\t" << ps->ooplevel << "\n"; cout << "-------------------------------------------------------\n"; return; } void display3(const student pa[], int n) { cout << "All of the students information: " << "\n"; for (int i = 0; i < n; i++) { cout << "The #" << i + 1<< " : -----------------------\n"; cout << "name:\t" << pa->fullname << "\n"; cout << "hobby:\t" << pa->hobby << "\n"; cout << "ooplevel:\t" << pa->ooplevel << "\n"; pa++; } return; } /* Enter class size: 5 ------------------------------------------------------- Enter the 1 student name: lisa Enter the 1 student hobby: basketball Enter the 1 student ooplevel: 1 ------------------------------------------------------- Enter the 2 student name: brown Enter the 2 student hobby: football Enter the 2 student ooplevel: 2 ------------------------------------------------------- Enter the 3 student name: coco Enter the 3 student hobby: tennis Enter the 3 student ooplevel: 3 ------------------------------------------------------- Enter the 4 student name: hisaishi Enter the 4 student hobby: piano Enter the 4 student ooplevel: 4 ------------------------------------------------------- Enter the 5 student name: Sarah Brightman Enter the 5 student hobby: new age music Enter the 5 student ooplevel: 5 ------------------------------------------------------- The student information: name: lisa hobby: basketball ooplevel: 1 ------------------------------------------------------- The student information: name: lisa hobby: basketball ooplevel: 1 ------------------------------------------------------- The student information: name: brown hobby: football ooplevel: 2 ------------------------------------------------------- The student information: name: brown hobby: football ooplevel: 2 ------------------------------------------------------- The student information: name: coco hobby: tennis ooplevel: 3 ------------------------------------------------------- The student information: name: coco hobby: tennis ooplevel: 3 ------------------------------------------------------- The student information: name: hisaishi hobby: piano ooplevel: 4 ------------------------------------------------------- The student information: name: hisaishi hobby: piano ooplevel: 4 ------------------------------------------------------- The student information: name: Sarah Brightman hobby: new age music ooplevel: 5 ------------------------------------------------------- The student information: name: Sarah Brightman hobby: new age music ooplevel: 5 ------------------------------------------------------- All of the students information: The #1 : ----------------------- name: lisa hobby: basketball ooplevel: 1 The #2 : ----------------------- name: brown hobby: football ooplevel: 2 The #3 : ----------------------- name: coco hobby: tennis ooplevel: 3 The #4 : ----------------------- name: hisaishi hobby: piano ooplevel: 4 The #5 : ----------------------- name: Sarah Brightman hobby: new age music ooplevel: 5 ----------------------------------- Done */
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++ Custom Control控件向父窗体发送对应的消息
- C++中拷贝构造函数的应用详解