#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TSIZE 20
typedef struct note {
char name[TSIZE]; //学生姓名
char number[TSIZE]; //学生学号
double chinese; //语文成绩
double english; //英语成绩
double math; //数学成绩
struct Student * next; //定义结构指针,形成链表
}Student; //定义学生类型结构,储存学生信息
void dele(Student * poi); //删除节点
void plus(Student * poi); //增加节点
void show(Student * poi); //显示学生信息
void frees(Student * poi); //清理内存
int main(void)
{
int a;
char copy[TSIZE];
Student * head = NULL;
Student * currect,* prev,*end; //声明指针
printf("请问要执行哪项操作:(输入对应编号)\n");
printf("1.输入学生信息 2.删除指定学生信息\n");
printf("3.添加学生信息 4.显示学生信息\n");
printf("5.退出程序\n");
scanf("%d",&a);
while(a != 5)
{
switch (a) //使用switch让用户进行选择使用
{
case 1: //让用户输入学生信息
printf("学生姓名\t学生学号\t语文成绩\t数学成绩\t英语成绩(输入ctrl+z退出)\n");
while(scanf("%s",copy) != EOF&©[0] != '\0')
{
currect = (Student *) malloc(sizeof(Student));
if(head == NULL)
head = currect;
else
prev->next = currect;
currect->next = NULL;
end = currect;
strcpy(currect->name,copy);
scanf("%s%lf%lf%lf",currect->number,&currect->chinese,&currect->math,&currect->english);
while(getchar() != '\n')
continue;
prev = currect;
}
printf("请问要执行哪项操作:(输入对应编号)\n");
printf("1.输入学生信息 2.删除指定学生信息\n");
printf("3.添加学生信息 4.显示学生信息\n");
printf("5.退出程序\n");
scanf("%d",&a);
break;
case 2: //让用户删除指定信息
dele(head);
printf("请问要执行哪项操作:(输入对应编号)\n");
scanf("%d",&a);
break;
case 3: //让用户增加信息
plus(end);
printf("请问要执行哪项操作:(输入对应编号)\n");
scanf("%d",&a);
break;
case 4: //显示信息
show(head);
printf("请问要执行哪项操作:(输入对应编号)\n");
scanf("%d",&a);
break;
case 5: //结束使用
printf("谢谢使用\n");
frees(head);
break;
}
}
frees(head); //清理内存
return 0;
}
void dele(Student * poi) //定义删除节点函数
{
Student * find, * de,*save;
char num[TSIZE];
printf("请输入要删除信息的学生学号\n");
scanf("%s",&num[0]);
while(getchar() != '\n')
continue;
de = poi;
save = poi;
while(strcmp(num,de->number))
{
find = de;
de = de->next;
}
if(poi == de)
poi = de->next;
else
find->next = de->next;
free(de);
}
void plus(Student * poi) //定义增加节点函数
{
printf("请输入学生信息\n");
Student * find, *over;
find = (Student *) malloc(sizeof(Student));
poi->next = find;
scanf("%s%s%lf%lf%lf",find->name,find->number,&find->chinese,&find->math,&find->english);
find->next = NULL;
}
void show(Student * poi) //定义显示函数
{
while(poi != NULL)
{
printf("%s\t%s\t%0.2lf\t%0.2lf\t%0.2lf\n",poi->name,poi->number,poi->chinese,poi->math,poi->english);
poi = poi->next;
}
}
void frees(Student * poi) //定义清理内存函数
{
Student * save;
save = poi;
while(poi != NULL)
{
save = poi;
poi = poi->next;
free(save);
}
}
|