// main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "head.h"
int main(int argc,const char *argv[])
{
ListStu L = stu_create();
if(NULL == L)
{
printf("创建失败\n");
return -1;
}
int menu;
char flag = 'y';
int num;char name[20];float score;
while(1)
{
system("clear");
printf("----学生信息管理系统----\n");
printf("1.添加学生\t2.展示学生信息\n");
printf("3.成绩排序\t4.查找学生\n");
printf("5.删除学生\t6.修改学生信息\n");
printf("请输入功能号:");
scanf(" %d",&menu);
putchar(10);
switch (menu)
{
case 1:
while(flag == 'y' || flag == 'Y')
{
printf("请输入学号:");
scanf(" %d",&num);
printf("请输入姓名:");
scanf(" %s",name);
printf("请输入分数:");
scanf(" %f",&score);
stu_insert_head(L,num,name,score);
putchar(10);
printf("是否继续输入【y/n】:");
scanf(" %c",&flag);
}
break;
case 2:
stu_show(L);
break;
case 3:
stu_sort(L);
stu_show(L);
break;
case 4:
printf("请输入要查找的姓名:");
scanf(" %s",name);
stu_search_name(L,name);
break;
case 5:
printf("请输入要删除的学号:");
scanf(" %d",&num);
stu_delete_num(L,num);
break;
case 6:
printf("请输入学生学号:");
scanf(" %d",&num);
printf("请输入分数:");
scanf(" %f",&score);
stu_update_score(L,num,score);
default:
printf("请重新输入");
break;
}
getchar();
getchar();
}
return 0;
}
// head.h
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct Stu
{
int num;
char name[20];
float score;
}*Stu;
typedef struct Node
{
union{
int len;
Stu student;
};
struct Node *next;
}Node,*ListStu;
ListStu stu_create();
Stu new_student(int num,char *name,float score);
void stu_insert_head(ListStu L,int num,char *name,float score);
void stu_insert_tail(ListStu L,int num,char *name,float score);
void stu_insert_pos(ListStu L,int pos,Stu S);
int stu_empty(ListStu L);
void stu_show(ListStu L);
void stu_sort(ListStu L);
ListStu stu_search_name(ListStu L,char *name);
ListStu stu_search_num(ListStu L,int num);
ListStu stu_search_pos(ListStu L,int pos);
void stu_delete_num(ListStu L,int num);
void stu_update_score(ListStu L,int num,float score);
// head.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"head.h"
ListStu stu_create()
{
ListStu L = (ListStu)malloc(sizeof(Node));
if(NULL == L)
{
printf("创建失败\n");
return NULL;
}
L->len = 0;
L->student = 0;
L->next = NULL;
return L;
}
Stu new_student(int num,char *name,float score)
{
Stu student = (Stu)malloc(sizeof(struct Stu));
if(student == NULL)
{
printf("new学生失败\n");
return NULL;
}
strcpy(student->name,name);
student->num = num;
student->score = score;
return student;
}
void stu_insert_head(ListStu L,int num,char *name,float score)
{
ListStu p = (ListStu)malloc(sizeof(Node));
if(NULL == L || p == NULL)
{
printf("insert失败\n");
return;
}
p->next = L->next;
L->next = p;
p->student = new_student(num,name,score);
L->len++;
}
void stu_show(ListStu L)
{
if(NULL == L)
{
printf("show失败\n");
return;
}
ListStu p = L->next;
while (p != NULL)
{
printf("%d\t%s\t%.1f\n",p->student->num,\
p->student->name,\
p->student->score);
p = p->next;
}
}
void stu_insert_tail(ListStu L,int num,char *name,float score)
{
ListStu p = (ListStu)malloc(sizeof(Node));
if(NULL == L || p == NULL)
{
printf("insert失败\n");
return;
}
L->len++;
while(L->next)
{
L = L->next;
}
p->next = L->next;
L->next = p;
p->student = new_student(num,name,score);
}
void stu_insert_pos(ListStu L,int pos,Stu S)
{
ListStu p = (ListStu)malloc(sizeof(Node));
if(NULL == L || p == NULL || pos <= 0 || pos > L->len+2)
{
printf("insert失败\n");
return;
}
L->len++;
for(int i = 0; i < pos-1; i++)
{
L = L->next;
}
p->next = L->next;
L->next = p;
p->student = S;
}
int stu_empty(ListStu L)
{
return L->len == 0 ? 1 : 0;
}
void stu_sort(ListStu L)
{
ListStu p = L->next;
if(NULL == L || NULL == p || stu_empty(L))
{
printf("sort失败\n");
return;
}
for(int i = 1; i < L->len; i++)
{
p = L->next;
for(int j = 0; j < L->len-i; j++)
{
if(p->student->score < p->next->student->score)
{
Stu S = p->student;
p->student = p->next->student;
p->next->student = S;
}
p = p->next;
}
}
}
ListStu stu_search_name(ListStu L,char *name)
{
ListStu p = L->next;
if(NULL == L || NULL == p || stu_empty(L) || name == NULL)
{
printf("search失败\n");
return NULL;
}
while(p)
{
if(!strcmp(p->student->name,name))
{
printf("学号:%d\t姓名:%s\t分数:%.1f\n",p->student->num,\
p->student->name,\
p->student->score);
return p;
}
p = p->next;
}
return NULL;
}
void stu_delete_num(ListStu L,int num)
{
if(NULL == L || stu_empty(L))
{
printf("delete失败\n");
return;
}
ListStu p = L;
while(p->next)
{
if(p->next->student->num == num)
{
putchar(10);
printf("学号:%d\t姓名:%s\t分数:%.1f\n",p->next->student->num,\
p->next->student->name,\
p->next->student->score);
ListStu q = p->next;
p->next = p->next->next;
free(q);
printf("已删除!\n");
L->len--;
return;
}
p = p->next;
}
printf("未找到该学号\n");
}
void stu_update_score(ListStu L,int num,float score)
{
if(L == NULL || stu_empty(L))
{
printf("update失败\n");
return;
}
while(L = L->next)
{
if(L->student->num == num)
{
L->student->score = score;
printf("学号:%d\t姓名:%s\t分数:%.1f\n",L->student->num,\
L->student->name,\
L->student->score);
return;
}
}
printf("未找到学生学号\n");
}
#endif