class 学员信息录入

来源:互联网 发布:linux shell test 编辑:程序博客网 时间:2024/04/30 12:50


/// @file exam_1_3.cpp/// @brief /**3.写一个学员信息录入程序,以学员信息类数组来放置录入的学员信息,并提供多种录入方式:(1)学号和年龄为默认,学号默认为从1开始递增,年龄默认为20(2)学号,姓名,年龄均不为默认,以学号和姓名来做为构造函数的参数(但要判断学号是唯一的)实现学员信息类的拷贝,(利用拷贝构造函数)*/#include <crtdbg.h>#include <iostream>#include <limits>#include <locale.h>#include "Student.h"#define LINE "------------------------------------------------------------"using namespace std;BOOL inputStudentInfo(CStudent* pStuAry, size_t nLenAry);BOOL isIdValid(CStudent* pStuAry, size_t nLenAry, size_t nId);BOOL inputStuInfo_quick(CStudent* pStu, size_t nId);BOOL inputStuInfo_normal(CStudent* pStuAry, size_t nLenAry, CStudent* pStu);void showStudentInfo(CStudent* pStuAry, size_t nLenAry);void clear_cin();int main(int argc, char** argv, char** envp){    CStudent stuAry[3/*29*/];    setlocale(LC_ALL, ".936");    if (inputStudentInfo(stuAry, sizeof(stuAry) / sizeof(stuAry[0])))        showStudentInfo(stuAry, sizeof(stuAry) / sizeof(stuAry[0]));    cout << "END, press any key to quit" << endl;    clear_cin();    getchar();    return 0;}BOOL inputStudentInfo(CStudent* pStuAry, size_t nLenAry){    BOOL isInputOk = FALSE;    enum {input_quick = 0, input_normal = 1};    int iInputType = 0; ///< 输入方式(快速输入或详细输入)    size_t nIndex = 0;    size_t nIdInputBegin = 1; ///< 快速输入的id值开始值    CStudent StuTmp;    if (NULL == pStuAry)    {        return FALSE;    }    cout << "请选择输入方式(快速输入[0], 详细输入[1]):";    cin >> iInputType;    cout << endl;    if ((input_quick != iInputType) && (input_normal != iInputType))    {        cout << "输入方式错误(快速输入[0], 详细输入[1])" << endl;        return FALSE;    }    for (nIndex = 0; nIndex < nLenAry; nIndex++)    {        StuTmp.clear();        cout << LINE << endl;        if (input_quick == iInputType)            {            isInputOk = inputStuInfo_quick(&StuTmp, nIdInputBegin++);        }        else if (input_normal == iInputType)        {            isInputOk = inputStuInfo_normal(pStuAry, nLenAry, &StuTmp);        }        if (isInputOk)        {            pStuAry[nIndex] = StuTmp;        }    }    return TRUE;}BOOL inputStuInfo_quick(CStudent* pStu, size_t nId){    BOOL isInputOk = FALSE;    char cName[CStudent::NAME_SIZE_MAX]; ///< 姓名    if (NULL == pStu)    {        return FALSE;    }    do     {        clear_cin();        cout << "请输入学生姓名:";        memset(cName, '\0', sizeof(cName));        cin.getline(cName, sizeof(cName) - 1, '\n');        isInputOk = (strlen(cName) > 0);    } while (!isInputOk);    *pStu = CStudent(cName, nId);    return TRUE;}BOOL inputStuInfo_normal(CStudent* pStuAry, size_t nLenAry, CStudent* pStu){    BOOL isInputOk = FALSE;    char cName[CStudent::NAME_SIZE_MAX]; ///< 姓名    int iAge = 0;    size_t nId = 0;        if (NULL == pStu)    {        return FALSE;    }        do    {        clear_cin();        cout << "请输入学生姓名:";        memset(cName, '\0', sizeof(cName));        cin.getline(cName, sizeof(cName) - 1, '\n');        isInputOk = (strlen(cName) > 0);    } while (!isInputOk);        do    {        isInputOk = FALSE;        clear_cin();        cout << "请输入学生年龄(16~80):";        cin >> iAge;        if (cin.fail())        {            continue;        }        if ((iAge < 16) || (iAge > 80))        {            cout << "错误 : 学生年龄有效范围(16~80)" << endl;            continue;        }        isInputOk = TRUE;    } while (!isInputOk);    do    {        isInputOk = FALSE;        clear_cin();        cout << "请输入学生ID:";        cin >> nId;        if (cin.fail())        {            continue;        }                if (!isIdValid(pStuAry, nLenAry, nId))        {            cout << "学生ID输入无效" << endl;            continue;        }        isInputOk = TRUE;    } while (!isInputOk);    *pStu = CStudent(cName, nId, iAge);    return TRUE;}BOOL isIdValid(CStudent* pStuAry, size_t nLenAry, size_t nId){    BOOL isValid = TRUE;    size_t nIndex = 0;    if ((size_t)-1 == nId)    {        return FALSE;    }        _ASSERT(NULL != pStuAry);    for (nIndex = 0; nIndex < nLenAry; nIndex++)    {        if (pStuAry[nIndex].getter_m_nId() == nId)        {            isValid = FALSE;            break;        }    }    return isValid;}void showStudentInfo(CStudent* pStuAry, size_t nLenAry){    size_t nIndex = 0;    _ASSERT(NULL != pStuAry);    cout << LINE << endl;    for (nIndex = 0; nIndex < nLenAry; nIndex++)    {        cout << "student NO." << nIndex << endl;        cout << "Id = " << pStuAry[nIndex].getter_m_nId() << endl;        cout << "Name = " << pStuAry[nIndex].getter_m_cName() << endl;        cout << "Age = " << pStuAry[nIndex].getter_m_iAge() << endl;        cout << LINE << endl;    }}void clear_cin(){    cin.clear();    cin.sync();}

// Student.h: interface for the CStudent class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_STUDENT_H__672796D9_2EA0_4C6C_AFC3_A1961BDAA31B__INCLUDED_)#define AFX_STUDENT_H__672796D9_2EA0_4C6C_AFC3_A1961BDAA31B__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#ifndef BOOLtypedef int BOOL;const int TRUE = 1;const int FALSE = 0;#endif#include <stddef.h>#include <memory.h>#include <string.h>/**3.写一个学员信息录入程序,以学员信息类数组来放置录入的学员信息,并提供多种录入方式:(1)学号和年龄为默认,学号默认为从1开始递增,年龄默认为20(2)学号,姓名,年龄均不为默认,以学号和姓名来做为构造函数的参数(但要判断学号是唯一的)实现学员信息类的拷贝,(利用拷贝构造函数)*/class CStudent  {    /// 常量定义public:    enum {NAME_SIZE_MAX = 64};    /// 构造, 拷贝构造, 析构函数public:CStudent();    CStudent(const char* pcName, size_t nId = 1, int iAge = 20);    CStudent(CStudent& src);    virtual ~CStudent();    /// 成员函数public:    void clear();private:    void init(); ///< 类初始化    void uninit(); ///< 类反初始化    void copy(CStudent& src);    /// setter, getterpublic:    // m_nId    void setter_m_nId(size_t nIn) {m_nId = nIn;}    size_t getter_m_nId() {return m_nId;}    // m_cName    void setter_m_cName(const char* pcIn)    {        if (NULL == pcIn)            return;        memset(m_cName, '\0', NAME_SIZE_MAX);        strncpy(m_cName, pcIn,             (strlen(pcIn) < (NAME_SIZE_MAX - 1)) ?             strlen(pcIn) : (NAME_SIZE_MAX - 1));    }    const char* getter_m_cName() {return m_cName;}    // m_iAge    void setter_m_iAge(int iIn) {m_iAge = iIn;}    int getter_m_iAge() {return m_iAge;}    /// 成员变量private:    size_t m_nId; ///< 学号    char m_cName[NAME_SIZE_MAX]; ///< 姓名    int m_iAge; ///< 年龄};#endif // !defined(AFX_STUDENT_H__672796D9_2EA0_4C6C_AFC3_A1961BDAA31B__INCLUDED_)

// Student.cpp: implementation of the CStudent class.////////////////////////////////////////////////////////////////////////#include "Student.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CStudent::CStudent(){    init();}CStudent::CStudent(const char* pcName, size_t nId /*= 1*/, int iAge /*= 20*/){    setter_m_cName(pcName);    setter_m_nId(nId);    setter_m_iAge(iAge);}CStudent::CStudent(CStudent& src){    copy(src);}void CStudent::copy(CStudent& src){    setter_m_cName(src.getter_m_cName());    setter_m_nId(src.getter_m_nId());    setter_m_iAge(src.getter_m_iAge());}CStudent::~CStudent(){}void CStudent::clear(){    m_nId = (size_t)-1;    memset(m_cName, '\0', NAME_SIZE_MAX);    m_iAge = -1;}void CStudent::init(){    clear();}void CStudent::uninit(){}


0 0
原创粉丝点击