鱼C论坛

 找回密码
 立即注册

C++Primer Plus 第六版编程练习7.13-9

已有 174 次阅读2015-7-4 11:28 |个人分类:c++Primer Plus 第6版编程练习笔记| Plus

#include <iostream>
#include<string>

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(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')
        continue ;
    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<<"Done\n" ;
    return 0 ;
}

int getinfo(student pa[] ,int n)
{
    int i =0 ;
    for( i; i<n ;i++)
    {
        cout<<"Please enter the "<<i+1<<" student's fullname:" ;
        cin.getline(pa->fullname,SLEN) ;
        cout<<"Please enter the "<<i+1<<" student's hobby:" ;
        cin.getline(pa->hobby,SLEN) ;
        cout<<"Please enter the "<<i+1<<" student's ooplevel:" ;
        cin>>pa->ooplevel ;
        while(cin.get() != '\n')
            continue ;
        pa++ ;
    }
    return i ;
}

void display1(student st)
{
    
    cout<<st.fullname<<"\t"<<st.hobby<<"\t"<<st.ooplevel<<endl ;
}

void display2(student * ps)
{
    
    cout<<ps->fullname<<"\t"<<ps->hobby<<"\t"<<ps->ooplevel<<endl ;
}

void display3(const student pa[] ,int n)
{
    for(int i =0 ;i<n ;i++)
    {
        cout<<pa[i].fullname<<"\t"<<pa[i].hobby<<"\t"<<pa[i].ooplevel<<endl ;
    }
}

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 07:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部