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
- class 学员信息录入
- 录入学员信息
- 获取键盘录入信息
- 录入学生信息
- 制作信息录入窗体
- java__for录入会员信息
- 学生信息录入
- 身份证信息录入
- 删除FireFox录入信息记录
- 1-1-3录入信息
- 实现会员信息录入功能
- 如何快速录入房产证信息?
- [易飞]录入信息传递设置信息
- 要求循环录入2个班的学员成绩,假设每个班都有3个学员, 依次录入,统计超过90分的学员人数,以及这批超过90分的学员平均分。
- TextBox里录入的信息相当于Combox
- 题目:北京市二手房交易信息录入程序
- 项目小结---信息录入的验证
- 05.[推荐]录入信息的技巧
- Wee5-6Lexicalized parsing
- Dubbox:来自当当网的SOA服务框架
- 数据存储与输入输出
- Android 提升进入界面的速度
- 数组循环移位算法
- class 学员信息录入
- APUE-3源码环境搭建 CentOS
- JVM 内存参数设置
- hdu1045
- Tomcat 7 如何手工部署war包对应的Java项目
- online_judge_1041
- Spring常用的四大注解
- Android中Settings.System的使用
- APK瘦身值得分享的一些经验