#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NAME 11
typedef union {
float normal_score;
float pe_score;
float art_score;
} Score;
enum stu_type
{
normal,
pe,
art
};
typedef struct student_info_manager sim;
struct student_info_manager
{ //定义结构体
size_t sid; //学号
char name[MAX_NAME]; //姓名
size_t gender; //male==1-or-female==0
size_t age;
int type; //年龄
Score score; //分数
sim *next; //指向下一个学生
sim *pre; //指向上一个学生
};
sim *create_node();
int add_node(sim *head, sim *node);
void delete_node(sim *node);
sim *search_node_by_id(sim *head, size_t sid);
void modify(sim *head);
void modify_name(sim *node);
void modify_age(sim *node);
void modify_gender(sim *node);
void modify_score(sim *node);
void modify_type(sim *node);
char *get_gender(size_t gender);
char *get_type(int type);
float get_score(sim *node);
void show(sim *head);
void output_info(sim *node);
void output_format();
void add_student(sim *head);
void delete_student(sim *head);
void delete_one(sim *head);
void delete_all(sim *head);
void modify(sim *head)
{
system("clear");
size_t sid = 0;
printf("请输入要修改信息的学生学号:");
scanf("%d", &sid);
while(getchar()!='\n');
sim *del = search_node_by_id(head, sid);
if (del == NULL)
{
printf("不存在的人!\n");
printf("任意键继续\n");
getchar();
return;
}
system("clear");
int sel = 0;
output_format();
output_info(del);
printf("\t\t\t\t[1]修改姓名\n");
printf("\t\t\t\t[2]修改性别\n");
printf("\t\t\t\t[3]修改年纪\n");
printf("\t\t\t\t[4]修改类型\n");
printf("\t\t\t\t[5]修改分数\n");
printf("\t\t\t\t[0]取消操作\n");
scanf("%d", &sel);
while(getchar()!='\n');
if (sel == 1)
{
modify_name(del);
}
else if (sel == 2)
{
modify_gender(del);
}
else if (sel == 3)
{
modify_age(del);
}
else if (sel == 4)
{
modify_type(del);
}
else if (sel == 5)
{
modify_score(del);
}
else if (sel == 0)
{
return;
}
else
{
printf("不存在的操作!\n");
printf("任意键继续\n");
getchar();
}
}
void modify_name(sim *node)
{
printf("输入新的名字");
scanf("%[^\n]", node->name);
while(getchar()!='\n');
printf("修改完毕,任意键继续\n");
getchar();
}
void modify_gender(sim *node)
{
printf("输入性别(0或1)\n[0]女\t[1]男性:");
scanf("%d", &node->gender);
while(getchar()!='\n');
printf("修改完毕,任意键继续\n");
getchar();
}
void modify_age(sim *node)
{
printf("输入新的年龄:");
scanf("%d", &node->age);
while(getchar()!='\n');
printf("修改完毕,任意键继续\n");
getchar();
}
void modify_type(sim *node)
{
printf("输入新类型([0]普通类[1]体育类[2]艺术类):");
int t = node->type;
scanf("%d", &node->type);
while(getchar()!='\n');
if ((node->type == normal || node->type == pe || node->type == art) && node->type != t)
{
printf("修改成功\n");
printf("任意键继续\n");
getchar();
return;
}
node->type = t;
printf("修改失败,任意键继续\n");
getchar();
}
void modify_score(sim *node)
{
printf("输入新的分数:");
if (node->type == normal)
scanf("%f", &node->score.normal_score);
else if (node->type == pe)
scanf("%f", &node->score.pe_score);
else if (node->type == art)
scanf("%f", &node->score.art_score);
else
{
printf("类型错误!\n");
printf("任意键继续\n");
getchar();
return;
}
while(getchar()!='\n');
printf("修改完成,任意键继续\n");
getchar();
}
void delete_student(sim *head)
{
system("clear");
int sel = 0;
printf("\t\t\t\t[1]删除单个\n");
printf("\t\t\t\t[2]删除全部\n");
printf("\t\t\t\t[0]取消操作\n");
scanf("%d", &sel);
while(getchar()!='\n');
if (sel == 1)
{
delete_one(head);
}
else if (sel == 2)
{
delete_all(head);
}
else if (sel == 0)
{
return;
}
else
{
printf("不存在的操作!\n");
printf("任意键继续\n");
getchar();
}
}
void delete_one(sim *head)
{
system("clear");
size_t sid = 0;
printf("请输入要删除的学生学号:");
scanf("%d", &sid);
while(getchar()!='\n');
sim *del = search_node_by_id(head, sid);
if (del == NULL)
{
printf("不存在的人!\n");
printf("任意键继续\n");
getchar();
return;
}
delete_node(del);
printf("任意键继续\n");
getchar();
}
void delete_all(sim *head)
{
system("clear");
sim *end = head;
while (end->next != NULL)
{
end = end->next;
} //移动到末 尾
while (end != head)
{
sim *t = end;
end = end->pre;
free(t);
}
head->next = NULL;
printf("全部删除!");
printf("删除完毕,任意键继续\n");
getchar();
}
void add_student(sim *head)
{
system("clear");
sim *node = create_node();
; //待添加节点
if (node == NULL) //初始化待添加节点,并判断是否成功
{
printf("不可添加,下次,下次一定!\no(∩_∩)o\n");
printf("任意键继续\n");
getchar();
return;
}
printf("输入学生学号:");
scanf("%d", &node->sid);
while(getchar()!='\n');
if (search_node_by_id(head, node->sid) != NULL)
{ //欲添加的学生学号已经存在
printf("学号[%d]已被分配!\n", node->sid);
printf("不可添加,任意键继续\n");
getchar();
free(node);
return;
}
printf("输入学生姓名:");
scanf("%[^\n]", node->name);
while(getchar()!='\n');
printf("输入学生性别(0或1)\n[0]女\t[1]男性:");
scanf("%d", &node->gender);
while(getchar()!='\n');
printf("输入学生年龄:");
scanf("%d", &node->age);
while(getchar()!='\n');
printf("输入学生类型\n([0]普通类[1]体育类[2]艺术类):");
scanf("%d", &node->type);
while(getchar()!='\n');
printf("输入学生成绩:");
if (node->type == normal)
{
scanf("%f", &node->score.normal_score);
}
else if (node->type == pe)
{
scanf("%f", &node->score.pe_score);
}
else if (node->type == art)
{
scanf("%f", &node->score.art_score);
}
else
{
printf("不存在此类型!\n");
free(node);
printf("任意键继续\n");
getchar();
return;
}
add_node(head, node);
printf("添加完毕,任意键继续\n");
getchar();
}
void show(sim *head)
{
sim *p = head;
system("clear");
output_format();
while (p != NULL)
{
if (p != head)
output_info(p);
p = p->next;
}
printf("-------------------------------------------------\n");
printf("\n\n");
printf("显示完毕,任意键继续\n");
getchar();
}
sim *search_node_by_id(sim *head, size_t sid)
{
sim *p = head;
while (p != NULL)
{
if (p->sid == sid && p != head)
{
printf("匹配成功\n");
return p;
}
p = p->next;
}
return NULL;
}
char *get_gender(size_t