题目:
编写一套计算机学院教师信息管理系统,用菜单进行管理,具备输入、显示、查找、排序、插入等功能。每一条记录包括一位教师的职工号、姓名、职称、性别、2门主讲课程,包括课程名称、开课学期、课程性质(必修/限选课)和教学效果,以及教学效果综合评分。
话不多说,直接上代码吧,代码比语言描述清晰
#include
#include
#include
#include
using namespace std;
typedef struct//课程信息
{
string class_name;
string class_date;
string class_type;
string class_effect;
}Class;
typedef struct//教师信息
{
string num;
string name;
string pos;
string sexul;
Class class1;
Class class2;
int sumscore;
}Teacher;
typedef struct teacher_xinxi//创建链表节点
{
Teacher teacher;
struct teacher_xinxi *next;
}Linklist;
//创建链表
Linklist * creatlinklist(int num)
{
Linklist *head_node , *temp_node, *end_node;
head_node = new Linklist;
if(head_node == NULL){
//cout<<"内存分配失败"<
return NULL;
}
end_node = head_node;//刚开始没有存数据的是时候,头节点就是尾节点
//开始输入学生数据
for(int i = 1; i <= num; i++){
temp_node = new Linklist;
cin>>temp_node->teacher.num>>temp_node->teacher.name>>temp_node->teacher.pos>>temp_node->teacher.sexul;
cin>>temp_node->teacher.class1.class_name>>temp_node->teacher.class1.class_date>>temp_node->teacher.class1.class_type>>temp_node->teacher.class1.class_effect;
cin>>temp_node->teacher.class2.class_name>>temp_node->teacher.class2.class_date>>temp_node->teacher.class2.class_type>>temp_node->teacher.class2.class_effect;
cin>>temp_node->teacher.sumscore;
end_node->next = temp_node;
end_node = temp_node;
}
end_node->next = NULL;
return head_node;
}
//输出链表中的内容
void display_Linklist(Linklist *h)
{
while(h->next != NULL){
h = h->next;
cout<<h->teacher.num<<" "<<h->teacher.name<<" "<<h->teacher.pos<<" "<<h->teacher.sexul<<" ";
cout<<h->teacher.class1.class_name<<" "<<h->teacher.class1.class_date<<" "<<h->teacher.class1.class_type<<" "<<h->teacher.class1.class_effect<<" ";
cout<<h->teacher.class2.class_name<<" "<<h->teacher.class2.class_date<<" "<<h->teacher.class2.class_type<<" "<<h->teacher.class2.class_effect;
cout<<" "<<h->teacher.sumscore<<endl;
}
}
//查找链表中的内容
int Search_teacher(string flag,Linklist *h)
{
int res = 0;//记录已经找到的符合条件的教师数量;
while(NULL != h->next){
h = h->next;
if(flag == h->teacher.name||flag == h->teacher.class1.class_name||flag == h->teacher.class2.class_name){
cout<<h->teacher.num<<" "<<h->teacher.name<<" "<<h->teacher.pos<<" "<<h->teacher.sexul<<" ";
cout<<h->teacher.class1.class_name<<" "<<h->teacher.class1.class_date<<" "<<h->teacher.class1.class_type<<" "<<h->teacher.class1.class_effect<<" ";
cout<<h->teacher.class2.class_name<<" "<<h->teacher.class2.class_date<<" "<<h->teacher.class2.class_type<<" "<<h->teacher.class2.class_effect;
cout<<" "<<h->teacher.sumscore<<endl;
res++;
}
}
return res;
}
//修改系统中某教师信息
int Change_Linklist(string flag_num,Linklist *h)
{
while(NULL != h->next){
h = h->next;
if(flag_num == h->teacher.num){
cin>>h->teacher.num>>h->teacher.name>>h->teacher.pos>>h->teacher.sexul;
cin>>h->teacher.class1.class_name>>h->teacher.class1.class_date>>h->teacher.class1.class_type>>h->teacher.class1.class_effect;
cin>>h->teacher.class2.class_name>>h->teacher.class2.class_date>>h->teacher.class2.class_type>>h->teacher.class2.class_effect;
cin>>h->teacher.sumscore;
return 1;
}
}
return 0;
}
//系统教师信息的删除
int delete_Linklist(string name,Linklist *h)
{
Linklist *temp = h, *t;
int if_delete = 0;
while(NULL != temp->next){
t = temp;
temp = temp->next;
if(temp->teacher.name == name){
if_delete = 1;
break;
}
}
//如果是头节点后的第一个元素,只需要将头节点连接至第一个元素后面的信息即可
if(temp == h->next){
h->next = temp->next;
}
//普通节点的情况
else{
t->next = temp->next;
}
delete(temp);
return if_delete;
}
//插入数据
void List_insert(Linklist *h)
{
Linklist *temp = new Linklist;
cin>>temp->teacher.num>>temp->teacher.name>>temp->teacher.pos>>temp->teacher.sexul;
cin>>temp->teacher.class1.class_name>>temp->teacher.class1.class_date>>temp->teacher.class1.class_type>>temp->teacher.class1.class_effect;
cin>>temp->teacher.class2.class_name>>temp->teacher.class2.class_date>>temp->teacher.class2.class_type>>temp->teacher.class2.class_effect;
cin>>temp->teacher.sumscore;
/*
//从头结点插入数据
temp->next = h->next;
h->next = temp;*/
//从尾结点插入数据
Linklist *end_node = h;
while(NULL != end_node->next){
end_node = end_node->next;
}
end_node->next = temp;
temp->next = NULL;
}
//获取链表存储长度
int get_List_len(Linklist *h)
{
int count = 0;
while(NULL != h->next){
h = h->next;
count++;
}
return count;
}
//冒泡排序对列表数据进行排序
void sort_List(Linklist *h,int sort_type)
{
int length = get_List_len(h);
Linklist *p = h->next;
Teacher temp;
//p,是为了防止排序过程中h被改变,temp做排序交换时的中间变量
for(int i = 0; i < length - 1; i++){
p = h->next;//保证每次排序都是从头节点开始
if(sort_type){//如果输入的是1则按照教学效果综合评分排序
for(int j = 0; j <length - 1 - i; j++){
if(p->teacher.sumscore < p->next->teacher.sumscore){
temp = p->teacher;
p->teacher = p->next->teacher;
p->next->teacher = temp;
}
p = p->next;
}
}
else{
for(int j = 0; j <length - 1 - i; j++){
if(p->teacher.num < p->next->teacher.num){
temp = p->teacher;
p->teacher = p->next->teacher;
p->next->teacher = temp;
}
p = p->next;
}
}
}
}
//链表的内存释放
void delete_all_Linklist(Linklist * h)
{
while(NULL != h){
Linklist *p = h;
delete(p);
h = h->next;
}
}
int main()
{
Linklist *p;
int tnum;//起始输入系统教师数量
while(1){
string oper;
cin>>oper;
//起始输入教师信息
if(oper == "input"){
cin>>tnum;
p = creatlinklist(tnum);
}
//通过输入的教师姓名或者课程名称查找教师
if(oper == "search"){
string flag;
cin>>flag;
int ans = Search_teacher(flag,p);
if(!ans){
cout<<"NO match!"<<endl;
}
}
//修改系统中教师信息
if(oper == "change"){
string flag_num;
cin>>flag_num;
int if_change = Change_Linklist(flag_num,p);
}
//删除输入教师名信息
if(oper == "delete"){
string name;
cin>>name;
int if_delete = delete_Linklist(name,p);
/*if(!if_delete){
cout<<"删除失败"<
}
//插入老师信息,由于这里没有说明每次插入信息的位置,所以默认从头结点插入
if(oper == "insert"){
List_insert(p);
}
//对系统中的老师按要求进行排序
//0按照职工号排序,1按照教学效果综合评分排序
if(oper == "sort"){
int sort_type;//判断应该按哪种方式进行排序的标识符
cin>>sort_type;
sort_List(p,sort_type);
}
//输出系统中教师信息
if(oper == "display"){
display_Linklist(p);
}
//退出程序
if(oper == "quit"){
break;
}
}
delete_all_Linklist(p);//释放内存
return 0;
}