C++实现学生信息管理系统

#include <iostream>
#include <string>
using namespace std;
//todo 使用链表和Student 类的学生管理系统

class Student
{
private:
    string name;
    string sex;
    double all_grades, avr_grades;

public:
    static int total; //静态成员变量
    static double all_chinese_grades;
    static double all_math_grades;
    static double all_english_grades;
    double chinese, math, english;

public:
    int id;
    Student *next;
    Student *pre;
    void Set(string name, string sex, double chinese, double math, double english, Student *pre);
    void Calculate();
    void Display();
    void ShowAll();
};
int Student::total = 0;
double Student::all_chinese_grades = 0;
double Student::all_math_grades = 0;
double Student::all_english_grades = 0;
void Student::Calculate()
{
    all_grades = chinese + math + english;
    avr_grades = all_grades / 3;
}
void Student::Display()
{
    cout << "----------------------------" << endl;
    cout << "name:" << this->name << endl;
    cout << "id:" << this->id << endl;
    cout << "sex:" << this->sex << endl;
    cout << "chinese:" << this->chinese << endl;
    cout << "math:" << this->math << endl;
    cout << "english:" << this->english << endl;
    cout << "all_grades:" << this->all_grades << endl;
    cout << "avr_grades:" << this->avr_grades << endl;
    cout << "----------------------------" << endl;
}
void Student::Set(string name, string sex, double chinese, double math, double english, Student *pre)
{
    this->name = name;
    this->sex = sex;
    this->chinese = chinese;
    this->math = math;
    this->english = english;
    all_chinese_grades += chinese;
    all_english_grades += english;
    all_math_grades += math;
    Calculate();
    id = total++;
    next = NULL;
    this->pre = pre;
}
void Student::ShowAll()
{
    cout << "all_chinese:" << all_chinese_grades << endl;
    cout << "all_math:" << all_math_grades << endl;
    cout << "all_english:" << all_english_grades << endl;
}
void Show()
{
    cout << "1.Display all information" << endl;
    cout << "2.Add a Student" << endl;
    cout << "3.Delete a Student" << endl;
    cout << "4.Quit" << endl;
}
int main()
{
    //todo 声明变量
    string name;
    string sex;
    double chinese, math, english;
    //todo -----------构造头结点-------------------------
    Student *first_stu = new Student(); //todo first_student 不包含任何信息
    Student *head_stu = new Student();
    first_stu->next = head_stu;
    head_stu->Set("ck", "boy", 1, 1, 1, first_stu);
    Student *stu = head_stu;
    Student *p;
    //todo -----------------------------
    int temp; //todo 输入删除学生的学号
    int mode = 0;
    while (mode != 4)
    {
        Show();
        cin >> mode;
        switch (mode)
        {
        case 1:
        {
            p = first_stu->next;
            while (p != NULL)
            {
                p->Display();
                p = p->next;
            }
            p->ShowAll();
            break;
        }

        case 2:
        {
            cout << "请依次输入该学生的姓名,性别,语文,数学,英语成绩" << endl;
            Student *new_stu = new Student();
            cin >> name >> sex >> chinese >> math >> english;
            stu->next = new_stu;
            new_stu->Set(name, sex, chinese, math, english, stu); //todo 最后一个参数是把新建的结点的前驱点找到
            stu = new_stu;
            break;
        }
        case 3:
        {
            cout << "请输入删除学生信息的学号" << endl;
            cin >> temp;
            p = head_stu;
            if (temp > Student::total)
            {
                cout << "找不到这个学号" << endl;
                break;
            }
            while (p != NULL)
            {
                if (p->id == temp)
                {
                    p->pre->next = p->next;
                    while (p->next != NULL)
                    {
                        p = p->next;
                        p->id--;
                        Student::all_chinese_grades -= p->chinese;
                        Student::all_english_grades -= p->english;
                        Student::all_math_grades -= p->math;
                    }
                    break;
                }
                p = p->next;
            }
            break;
        }
        case 4:
        {
            return 0;
        }
        default:
        {
            cout << "重新输入" << endl;
            break;
        }
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值