#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;
}