class 学员信息录入


/// @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 BOOL
typedef 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, getter
public:
    // 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用参数数组方法编写的Java代码,实现了学员信息的添加、查找、展示、修改和排序功能: ``` public class Student { // 定义学员属性 String name; int id; int age; int score; // 添加学员信息 public static void addStudent(Student[] arr, String name, int id, int age, int score) { Student stu = new Student(); stu.name = name; stu.id = id; stu.age = age; stu.score = score; for (int i = 0; i < arr.length; i++) { if (arr[i] == null) { arr[i] = stu; System.out.println("添加成功!"); break; } } } // 根据学号查找学员信息 public static void findStudentById(Student[] arr, int id) { for (int i = 0; i < arr.length; i++) { if (arr[i] != null && arr[i].id == id) { System.out.println("姓名:" + arr[i].name + " 学号:" + arr[i].id + " 年龄:" + arr[i].age + " 成绩:" + arr[i].score); return; } } System.out.println("查无此人!"); } // 展示所有学员信息 public static void showAllStudents(Student[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] != null) { System.out.println("姓名:" + arr[i].name + " 学号:" + arr[i].id + " 年龄:" + arr[i].age + " 成绩:" + arr[i].score); } } } // 根据查找学员姓名修改学员分数 public static void updateScoreByName(Student[] arr, String name, int score) { for (int i = 0; i < arr.length; i++) { if (arr[i] != null && arr[i].name.equals(name)) { arr[i].score = score; System.out.println("修改成功!"); return; } } System.out.println("查无此人!"); } // 对学员的成绩进行成绩的排序 public static void sortStudentsByScore(Student[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] != null && arr[j + 1] != null && arr[j].score < arr[j + 1].score) { Student temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } System.out.println("排序后的学员信息:"); showAllStudents(arr); } public static void main(String[] args) { // 创建学员数组 Student[] arr = new Student[5]; // 添加学员信息 addStudent(arr, "张三", 1001, 18, 80); addStudent(arr, "李四", 1002, 19, 90); addStudent(arr, "王五", 1003, 20, 70); addStudent(arr, "赵六", 1004, 21, 85); addStudent(arr, "钱七", 1005, 22, 95); // 根据学号查找学员信息 findStudentById(arr, 1002); // 展示所有学员信息 showAllStudents(arr); // 根据查找学员姓名修改学员分数 updateScoreByName(arr, "张三", 90); // 对学员的成绩进行成绩的排序 sortStudentsByScore(arr); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值