面向对象程序设计实验记录 - 逆光喵の秘密基地

面向对象程序设计实验记录

分类:c++,代码 ; 热度:98 ; 最后更新于2020 年 06 月 23 日

逆光喵w逆光喵w

题目

设计一个类CStudent,类中包含一个学生的基本数据如下:
编号,姓名,性别,年龄,数学成绩,计算机成绩,外语成绩。
并假设编号为整数,且从1号往后连续编码;姓名为字符串,性别为字符。如:
1 LiPing m 18 89 98 94
请采用binary文件形式,并使用随机读写处理方式,对自定义CStudent类的对象数
据进行存储与读写处理(即是说,总按具有连续编码的编号num为"序"来对文件中的
各对象数据进行随机读写处理)。并设计该类的成员函数,而且对输出运算符"<<"进
行重载,使该运算符能够完成将一个学生的信息输出到屏幕上。要求成员函数完成以下
功能:
(1)从键盘输入一个学生的有关信息,并将它们存入到数据文件中(按编号来确
定写出位置)。
(2)按编号对学生信息进行检索并将检索结果显示在屏幕上。
(3)按姓名对学生信息进行检索并将检索结果显示在屏幕上。
(4)计算某编号学生的总成绩与平均成绩。
(5)列出所有总成绩超过n分的性别为s同学的有关信息(n,s由用户从键盘输入)。


代码

#include <iostream>
#include <string>
using namespace std;

//Student类用于接收用户输入的数据
class Student{
    public:

        //构造函数Student用于接收数据并进行封装
        Student(int num,string name,string sex,int age,double MScore,double CScore,double EScore){

            /*
            id:编号
            name:姓名
            sex:性别
            age:年龄
            MatScore:数学成绩
            ComScore:计算机成绩
            EngScore:英语成绩
            totalScore:总成绩
            aveScore:平均成绩
            */

            m_id = num;
            m_name = name;
            m_sex = sex;
            m_age = age;
            m_MatScore = MScore;
            m_ComScore = CScore;
            m_EngScore = EScore;
            m_totalScore = m_MatScore+m_ComScore+m_EngScore;
            m_aveScore = m_totalScore/3;
            }

        //初始化构造函数    
        Student(){
            m_id = 0;
            m_name = "NULL";
            m_sex = "NULL";
            m_age = 0;
            m_MatScore = m_ComScore = m_EngScore = 0;
            m_totalScore = m_aveScore = 0;
        }

        //析构函数
        ~Student(){}

        //友元函数
        friend class StuMessage;

    private:
        string m_name;
        string m_sex;
        int m_age;
        int m_id;
        double m_MatScore,m_ComScore,m_EngScore;
        double m_aveScore;
        double m_totalScore;
        friend class StuMessage;
        Student *next;
};

//StuMessage类用于处理程序各种数据
class StuMessage{
    public:

        StuMessage(){
            num = 0;
            first = new Student;
        }

        ~StuMessage(){
            delete first;//释放内存
            first = NULL;//将指针设置为NULL
            };

        void Push();//录入学生信息与成绩
        void Search();//查找功能
        void Search_id();//根据编号查找
        void Search_name();//根据姓名查找
        void DisplayAll();//输出所有学生所有信息
        void Show_score();//根据总分与性别查找
        int menu();//菜单
        
    private:
        int num;
        Student *first;
        
};

void StuMessage::Push(){
    string name;
    string sex;
    int age;
    double MScore,CScore,EScore;
    cout<<"请输入学生姓名:";
    cin>>name;
    cout<<"请输入性别:";
    cin>>sex;
    cout<<"请输入年龄:";
    cin>>age;
    cout<<"请输入数学成绩:";
    cin>>MScore;
    cout<<"请输入计算机成绩:";
    cin>>CScore;
    cout<<"请输入英语成绩:";
    cin>>EScore;
    cout<<"信息录入完成"<<endl;
    cout<<endl;
    num++;//编号+1

    //为下一份数据开辟空间
    Student *New_Stu = new Student(num,name,sex,age,MScore,CScore,EScore);
    Student *p = first;
    while (p->next != NULL)
    {
        p= p->next;
    }
    p->next = New_Stu;
    New_Stu->next = NULL;
}

void StuMessage::Search(){
    int choice;
    cout<<"选择查找方式————1:按编号查找 2:按姓名查找"<<endl;
    cin>>choice;
    switch (choice)
    {
    case 1:
        Search_id();//按编号查找
        break;
    case 2:
        Search_name();//按姓名查找
        break;
    default:
        cout<<"输入错误";
        break;
    }
}

void StuMessage::Search_id(){
    int Input_id;
    bool check;
    cout<<"请输入需要查找的学生编号:";
    cin>>Input_id;
    Student *p = first->next;//初始化指针

    //数据遍历
    while (p != NULL)
    {
        if(p->m_id == Input_id){
            cout<<endl;
            cout<<"编号:"<<p->m_id<<endl;
            cout<<"姓名:"<<p->m_name<<endl;
            cout<<"性别:"<<p->m_sex<<endl;
            cout<<"年龄:"<<p->m_age<<endl;
            cout<<"数学成绩: "<<p->m_MatScore<<endl;
            cout<<"计算机成绩: "<<p->m_ComScore<<endl;
            cout<<"英语成绩: "<<p->m_EngScore<<endl;
            cout<<"总分:"<<p->m_totalScore<<endl;
            cout<<"平均分:"<<p->m_aveScore<<endl;
            cout<<"---------------------------------------"<<endl;
            check = true;
        }
        p= p->next;//指针后移
    }
    
    if (check != 1){
        cout<<"没有查找到数据"<<endl;
    }
    
}

void StuMessage::Search_name(){
    string name;
    bool check;
    cout<<"请输入查找的姓名:";
    cin>>name;
    Student *p = first->next;//初始化指针

    //数据遍历
    while (p != NULL)
    {
        if(p->m_name == name){
            cout<<endl;
            cout<<"编号:"<<p->m_id<<endl;
            cout<<"姓名:"<<p->m_name<<endl;
            cout<<"性别:"<<p->m_sex<<endl;
            cout<<"年龄:"<<p->m_age<<endl;
            cout<<"数学成绩: "<<p->m_MatScore<<endl;
            cout<<"计算机成绩: "<<p->m_ComScore<<endl;
            cout<<"英语成绩: "<<p->m_EngScore<<endl;
            cout<<"总分:"<<p->m_totalScore<<endl;
            cout<<"平均分:"<<p->m_aveScore<<endl;
            cout<<"---------------------------------------"<<endl;
            cout<<endl;
            check = true;
        }
        p = p->next;//指针后移
    }
    
    if (check != 1){
        cout<<"没有查找到数据"<<endl;
    }
    
}

void StuMessage::DisplayAll(){
    if (num != 0)//判断非空
    {
        Student *p = first->next;//初始化指针

        //数据遍历
        while (p != NULL)
        {
            cout<<endl;
            cout<<"编号:"<<p->m_id<<endl;
            cout<<"姓名:"<<p->m_name<<endl;
            cout<<"性别:"<<p->m_sex<<endl;
            cout<<"年龄:"<<p->m_age<<endl;
            cout<<"数学成绩: "<<p->m_MatScore<<endl;
            cout<<"计算机成绩: "<<p->m_ComScore<<endl;
            cout<<"英语成绩: "<<p->m_EngScore<<endl;
            cout<<"总分:"<<p->m_totalScore<<endl;
            cout<<"平均分:"<<p->m_aveScore<<endl;
            cout<<"---------------------------------------"<<endl;
            cout<<endl;
            p = p->next;//指针后移
        }
    }

    else
    {
        cout<<"学生信息为空!"<<endl;
    }

}

void StuMessage::Show_score(){
    double Input_Score;
    string Input_Sex;
    bool check;
    cout<<"请输入一个分数用于查询超过该分数的学生:";
    cin>> Input_Score;
    cout<<"请输入筛选的学生性别:";
    cin>> Input_Sex;
    Student *p = first->next;//初始化指针

    //数据遍历
    while (p->next != NULL){
        if (p->m_totalScore > Input_Score){//对比总分
            if (p->m_sex == Input_Sex){//对比性别
                cout<<endl;
                cout<<"编号:"<<p->m_id<<endl;
                cout<<"姓名:"<<p->m_name<<endl;
                cout<<"性别:"<<p->m_sex<<endl;
                cout<<"年龄:"<<p->m_age<<endl;
                cout<<"数学成绩: "<<p->m_MatScore<<endl;
                cout<<"计算机成绩: "<<p->m_ComScore<<endl;
                cout<<"英语成绩: "<<p->m_EngScore<<endl;
                cout<<"总分:"<<p->m_totalScore<<endl;
                cout<<"平均分:"<<p->m_aveScore<<endl;
                cout<<"---------------------------------------"<<endl;
                cout<<endl;
                check = 1;
            }
        }   
    p = p->next;//指针后移
    }

    if (check!= 1){
        cout<<"没有找到数据"<<endl;
    }
}

int StuMessage::menu(){
    int select;
    cout<<endl;
    cout<<"===================================="<<endl;
    cout<<"------欢迎使用学生成绩管理系统------"<<endl;
    cout<<"1:录入学生信息与成绩"<<endl;
    cout<<"2:查询学生信息"<<endl;
    cout<<"3:展示所有已录入的学生信息"<<endl;
    cout<<"4:根据分数和性别查询信息"<<endl;
    cout<<"0:退出"<<endl;
    cout<<"===================================="<<endl;
    cout<<"请选择功能:";
    cin>>select;
    cout<<endl;
    return select;
}

int main(){
    StuMessage rank1;
    int select;

    //菜单
    while (select = rank1.menu())
    {
       switch (select)
        {   
            case 1:
                rank1.Push();//数据录入
                break;
            case 2:
                rank1.Search();//数据查找
                break;
            case 3:
                rank1.DisplayAll();//数据展示
                break;
            case 4:
                rank1.Show_score();//数据按类搜索
                break;
            default:
                cout<<"输入错误,请重新输入!"<<endl;
                break;
        } 
    }
    return 0;
}

评论卡