c++ primer plus 第七章《编程题7.13.9》

#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
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值