#include "iostream"
#include "string"
using namespace std;
class Student
{
public:
Student();
void Creat ( Student stu[] );
void Insert ( Student stu[] );
void Delete ( Student stu[] );
void Lookup ( Student stu[] );
void Update ( Student stu[] );
void Stat ( Student stu[] );
int Length ( Student stu[] );
void Print ( Student stu[] );
private:
string name; //姓名
long num; //学号
float score; //成绩
};
Student::Student()
{
name = ' ';
num = 0;
score = 0;
}
/*************************************************************************
创建学生成绩信息的线性表(数组)
**************************************************************************/
void Student::Creat(Student stu[])
{
cout << "请输入学生人数:" ;
int n;
cin >> n;
cout<<"姓名"<<'\t'<<"学号"<<'\t'<<"成绩"<<endl;
for(int i = 1; i <= n; i ++)
{
string newname;
long newnum;
float newscore;
cin >> newname;
stu[i].name = newname;
cin >> newnum;
stu[i].num = newnum;
cin >> newscore;
stu[i].score = newscore;
}
}
/*************************************************************************
依次输出线性表中的学生成绩信息
**************************************************************************/
void Student::Print(Student stu[])
{
cout << "姓名" << '\t' << "学号" << '\t' << "成绩" << '\n';
int i = 1;
while (stu[i].num)
{
cout << stu[i].name << '\t' << stu[i].num << '\t' << stu[i].score << '\n';
i++;
}
cout << '\n';
}
/*************************************************************************
根据学号查询线性表中某个学生成绩信息
**************************************************************************/
void Student::Lookup(Student stu[])
{
cout << "请输入你要查询的学号:" ;
long num;
cin >> num;
int i = 1;
while (stu[i].num)
{
if (stu[i].num == num)
{
cout << "姓名" << '\t' << "学号" << '\t' << "成绩" << '\n';
cout << stu[i].name << '\t' << stu[i].num << '\t' << stu[i].score << '\n';
break;
}
i ++;
}
if (!stu[i].num)
{
cout << "你要查询的学号不存在,请重新输入!" << '\n';
Student::Lookup(stu);
}
cout << '\n';
}
/*************************************************************************
在线性表的某个位置上插入学生信息
**************************************************************************/
void Student::Insert(Student stu[])
{
if (Student::Length(stu) == 100)
{
cout << "存储空间已满,不能进行插入操作!" << '\n';
}
else
{
cout << "请输入要插入的位置:" ;
int m;
cin >> m;
int n = Student::Length(stu);
if (m > n + 1)
{
cout << "插入位置不正确,请重新输入!" << '\n';
Student::Insert(stu);
}
else
{
for(int i = n; i >= m; i --) //数组中的数据依次后移
{
stu[i + 1].name = stu[i].name;
stu[i + 1].num = stu[i].num;
stu[i + 1].score = stu[i].score;
}
cout << "请依次输入姓名,学号,成绩" << '\n';
string newname;
long newnum;
float newscore;
cin >> newname;
stu[m].name = newname;
cin >> newnum;
stu[m].num = newnum;
cin >> newscore;
stu[m].score = newscore;
}
}
}
/*************************************************************************
查询线性表中有效数据的长度
**************************************************************************/
int Student::Length(Student stu[])
{
int i = 1;
while (stu[i].num)
{
i ++;
}
return (i - 1);
}
/*************************************************************************
根据学号删除线性表中对应的学生信息
**************************************************************************/
void Student::Delete(Student stu[])
{
cout << "请输入你要删除的学号:" ;
long num;
cin >> num;
int i = 1;
if (!stu[i].num)
{
cout << "你要删除的学号不存在,请重新输入!" << '\n';
Student::Delete(stu);
}
while (stu[i].num)
{
if (stu[i].num == num)
{
int n = Student::Length(stu);
for(int j = i; j < n; j ++) //数组中数据依次前移
{
stu[i].name = stu[i + 1].name;
stu[i].num = stu[i + 1].num;
stu[i].score = stu[i + 1].score;
}
stu[j].name = ' ';
stu[j].num = 0;
stu[j].score = 0;
break;
}
i ++;
}
}
/*************************************************************************
统计各成绩段的学生信息
**************************************************************************/
void Student::Stat(Student stu[])
{
cout << "不及格的学生是:" << '\n';
cout << "姓名" << '\t' << "学号" << '\t' << "成绩" << '\n';
int i = 1;
while (stu[i].num)
{
if (stu[i].score < 60)
{
cout << stu[i].name << '\t' << stu[i].num << '\t' << stu[i].score << '\n';
}
i ++;
}
cout << '\n';
cout << "成绩为“良”的学生是:" << '\n';
cout << "姓名" << '\t' << "学号" << '\t' << "成绩" << '\n';
i = 1;
while (stu[i].num)
{
if (stu[i].score >= 60 && stu[i].score < 75)
{
cout << stu[i].name << '\t' << stu[i].num << '\t' << stu[i].score << '\n';
}
i ++;
}
cout << '\n';
cout << "成绩为“中”的学生是:" << '\n';
cout << "姓名" << '\t' << "学号" << '\t' << "成绩" << '\n';
i = 1;
while (stu[i].num)
{
if (stu[i].score >= 75 && stu[i].score < 90)
{
cout << stu[i].name << '\t' << stu[i].num << '\t' << stu[i].score << '\n';
}
i ++;
}
cout << '\n';
cout << "成绩为“优”的学生是:" << '\n';
cout << "姓名" << '\t' << "学号" << '\t' << "成绩" << '\n';
i = 1;
while (stu[i].num)
{
if (stu[i].score >= 90)
{
cout << stu[i].name << '\t' << stu[i].num << '\t' << stu[i].score << '\n';
}
i ++;
}
cout << '\n';
}
/*************************************************************************
修改线性表中某个位置的学生信息
**************************************************************************/
void Student::Update(Student stu[])
{
cout << "请输入你要修改的位置:";
int m;
cin >> m;
if (m > Student::Length(stu))
{
cout << "你要修改的位置不存在,请重新输入!" << '\n';
Student::Update(stu);
}
else
{
cout << "请依次输入更改后的姓名,学号,成绩" << '\n';
string newname;
long newnum;
float newscore;
cin >> newname;
stu[m].name = newname;
cin >> newnum;
stu[m].num = newnum;
cin >> newscore;
stu[m].score = newscore;
}
}
/*************************************************************************
主函数,调用类Student的成员函数以实现相应功能
**************************************************************************/
void main()
{
cout << "首先建立学生管理系统!" << '\n';
Student stu[101];
stu[0].Creat(stu);
stu[0].Print(stu);
int j=100;;
cout << "请选择您要进行的操作(1为插入,2为删除,3为查找,4为修改,5为统计,0为取消操作)";
while(j)
{
cout << "请选择您要进行的操作:";
cin >> j;
switch(j)
{
case 1:
{
stu[0].Insert(stu);
stu[0].Print(stu);
break;
}
case 2:
{
stu[0].Delete(stu);
stu[0].Print(stu);
break;
}
case 3:
{
stu[0].Lookup(stu);
break;
}
case 4:
{
stu[0].Update(stu);
stu[0].Print(stu);
break;
}
case 5:
{
stu[0].Stat(stu);
break;
}
default:
break;
}
}
cout <<"线性表中共有 "<<stu[0].Length(stu)<<" 个学生";
}