您的位置:首页 > 编程语言 > C语言/C++

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++