C语言课设:基于单向循环链表的学生管理系统的增删改查排序
作者:互联网
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
typedef struct Student //定义结构体
{
long num; //学号
char name[20]; //姓名
char sex; //性别
float Chinese; //语文成绩
float Math; //数学成绩
float English; //英语成绩
float aver; //平均分
}student;
typedef struct CLinkNode
{
student data;
struct CLinkNode *next;
}CLinkNode;
int n;
int Menu();
int menu();
int sort(CLinkNode *head);
void number(CLinkNode *head);
void aver(CLinkNode *head);
CLinkNode *creat_node();//创建头节点
int InitCLinkList(CLinkNode *head);
int ShowCLinkList(CLinkNode *head);
int delete_list(CLinkNode *head);
int Modify_information(CLinkNode *head);
void write(CLinkNode *head);
void read(CLinkNode *head);
int n;
int main()
{
int a;
CLinkNode *head;
head=creat_node();//创建头节点
head->next=head;//自我循环
while(1)
{
a = Menu();
switch(a)
{
case 1:InitCLinkList(head);break;
case 2:ShowCLinkList(head);break;
case 3:delete_list(head);break;
case 4:Modify_information(head);break;
case 5:InitCLinkList(head);break;
case 6:sort(head);break;
case 7:write(head);break;
case 8:read(head);break;
case 9:
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\t\t\t\t\t\t已退出...\n\n\n\n\n\n\n\n\n\n\n\n");
exit(0);
break;
default:
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t请输入正确的选择(按任意键继续)");
break;
}
getch();
}
return 0;
}
int Menu() //一级菜单
{
system("cls");
int a;
printf("\n");
printf("\t\t\t\t\t-------------------------\n");
printf("\t\t\t\t\t| 学生成绩管理系统V2.0|\t\t\t\n");
printf("\t\t\t\t\t|\t请选择功能\t|\t\t\t\t\n");
printf("\t\t\t\t\t-------------------------\n");
printf("\t\t\t\t\t|\t1.录入信息\t|\t\t\t\t\n");
printf("\t\t\t\t\t|\t2.打印信息\t|\t\t\t\t\n");
printf("\t\t\t\t\t|\t3.删除信息\t|\t\t\t\t\n");
printf("\t\t\t\t\t|\t4.修改信息\t|\t\t\t\t\n");
printf("\t\t\t\t\t|\t5.增加信息\t|\t\t\t\t\n");
printf("\t\t\t\t\t|\t6.信息排序\t|\t\t\t\t\n");
printf("\t\t\t\t\t| 7.保存信息到文件\t|\t\t\t\t\n");
printf("\t\t\t\t\t| 8.从文件读取信息\t|\t\t\t\t\n");
printf("\t\t\t\t\t|\t9.退出系统\t|\t\t\t\t\n");
printf("\t\t\t\t\t-------------------------\n");
printf("\n");
printf("\t\t\t\t\t请输入您要选择的功能键: ");
scanf("%d",&a);
return a;
}
int menu() //二级菜单
{
system("cls");
int d;
printf("\n");
printf("\t\t\t\t\t-------------------------\n");
printf("\t\t\t\t\t| 学生成绩管理系统V2.0|\t\t\t\n");
printf("\t\t\t\t\t|\t请选择功能\t|\t\t\t\t\n");
printf("\t\t\t\t\t-------------------------\n");
printf("\t\t\t\t\t|\t1.学号排序\t|\t\t\t\t\n");
printf("\t\t\t\t\t|\t2.平均分排序\t|\t\t\t\t\n");
printf("\t\t\t\t\t|\t3.返回上一级\t|\t\t\t\t\n");
printf("\t\t\t\t\t|\t4.退出系统\t|\t\t\t\t\n");
printf("\t\t\t\t\t-------------------------\n");
printf("\n");
printf("\t\t\t\t\t请输入您要选择的功能键: ");
scanf("%d",&d);
return d;
}
void write(CLinkNode *head)
{
FILE *fp;
CLinkNode *p;
int i;
if ((fp = fopen("学生信息管理.txt", "w")) == NULL) //打开文件准备写入
{
printf("cannot open this file!\n");
getch();
return;
}
p=head->next;
for(i=0;i<n;i++)
{
fprintf(fp,"%ld %s %c %.2f %.2f %.2f\n",p->data.num,p->data.name,p->data.sex,p->data.Chinese,p->data.Math,p->data.English);
p = p->next;
}
fclose(fp);
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\tout save!"); //写入成功
}
void read(CLinkNode *head)
{
system("cls");
printf("文件信息存放如下:\n");
FILE *fp;
CLinkNode *p=head;
if ((fp = fopen("学生信息管理.txt", "r")) == NULL)
{
printf("cannot open this file\n!");
getch();
exit(0);
}
while(!feof(fp))
{
if(fscanf(fp,"%ld %s %c %f %f %f",&p->data.num,p->data.name,&p->data.sex,&p->data.Chinese,&p->data.Math,&p->data.English)==EOF)
{
free(p);
break;
}
printf("%ld\t%s\t%c\t%.2f\t%.2f\t%.2f\n",p->data.num,p->data.name,p->data.sex,p->data.Chinese,p->data.Math,p->data.English);
p=p->next;
}
}
int sort(CLinkNode *head) //排序
{
system("cls");
int d;
while(1)
{
d = menu();
switch(d)
{
case 1:number(head);break; //学号排序
case 2:aver(head);break; //平均分排序*/
case 3:return(1); //返回上一级
case 4:exit(0);break; //退出
default:
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t请输入正确的选择(按任意键继续)");
break;
}
getch();
}
return 0;
}
CLinkNode *creat_node()//节点创建函数
{
CLinkNode *p=(CLinkNode *)malloc(sizeof(CLinkNode));
if(p==NULL)
{
printf("创建失败");
}
else
{
p->next=NULL;
return p;
}
}
int InitCLinkList(CLinkNode *head)//插入数据到链表中
{
system("cls");
long number;
char name1[20];
char sex1;
float Chinese1;
float Math1;
float English1;
printf("\n\n输入学号:\n");
getchar();
scanf("%ld",&number);
printf("\n输入姓名:\n");
getchar();
gets(name1);
printf("\n输入性别(m or f):\n");
scanf("%c",&sex1);
printf("\n输入语数英成绩(百分制):\n");
scanf("%f%f%f",&Chinese1,&Math1,&English1);
if(sex1 != 'm' && sex1 != 'f')
{
printf("性别输入错误!(按任意键返回菜单)");
getch();
Menu();
}
if(Chinese1>100 || Chinese1<0 ||
Math1>100 || Math1<0 ||
English1>100 || English1<0)
{
printf("成绩输入错误!(按回车返回菜单)");
getch();
Menu();
}
CLinkNode *q=creat_node();
q->data.num=number;
strcpy(q->data.name,name1);
q->data.sex=sex1;
q->data.Chinese=Chinese1;
q->data.Math=Math1;
q->data.English=English1;
n++;
printf("\n\n录入成功,按任意键继续");
if(head!=NULL)//判断头节点是否为NULL
{
CLinkNode *p=head;
while(p->next!=head)//遍历头节点到最后一个数据
{
p=p->next;
}
p->next=q;//把最后一个节点赋新的节点过去
q->next=head;
return 1;
}
else
{
return 0;
}
}
int ShowCLinkList(CLinkNode *head)
{
system("cls");
printf("\n学生信息:\n");
printf("学号\t姓名\t性别\t语文\t数学\t英语\n");
printf("-----------------------------------------------------\n");
if (head!=NULL)
{
CLinkNode *p=head;
while(p->next!=head)
{
printf("%ld\t%s\t%c\t%.2f %.2f %.2f\n",p->next->data.num,p->next->data.name,p->next->data.sex,p->next->data.Chinese,p->next->data.Math,p->next->data.English);
p=p->next;
}
printf("\n\n打印成功,按任意键继续");
return 1;
}
else
{
return 0;
}
}
int delete_list(CLinkNode *head)
{
system("cls");
if(head==NULL)
{
printf("链表为空!\n");
return 0;
}
CLinkNode *before=head;
CLinkNode *behind=head->next;
long nn;
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t请输入你要删除的学号:");
scanf("%ld",&nn);
while(behind!=head)
{
if(behind->data.num==nn)
{
if(behind->next==head)
{
before->next=head;
free(behind);
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t删除成功...");
return 1;
}
before->next = behind->next;
free(behind);
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t删除成功...");
return 1;
}
before=before->next;
behind=behind->next;
}
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t要删除的数据不存在...");
return 0;
}
int Modify_information(CLinkNode *head)
{
system("cls");
long xx;
CLinkNode *before=head;
CLinkNode *behind=head->next;
printf("输入要修改的学生学号:");
scanf("%ld",&xx);
while(behind!=head)
{
if(behind->data.num==xx)
{
if(behind->next==head)
{
before->next=head;
free(behind);
InitCLinkList(head);
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t修改成功...");
return 1;
}
before->next = behind->next;
free(behind);
InitCLinkList(head);
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t修改成功...");
return 1;
}
before=before->next;
behind=behind->next;
}
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t要修改的数据不存在...");
return 0;
}
void number(CLinkNode *head)
{
system("cls");
CLinkNode *p,*q;
p=head;
int i;
long temp;
float t1,t2,t3,t4;
char t5[25];
char t6;
//冒泡排序
for ( i =1; i<n;i++,p=p->next)
for (q=p->next;q!=head->next;q=q->next)
if (p->data.num>q->data.num)
{
//交换所有信息
t1 = p->data.Chinese;
p->data.Chinese = q->data.Chinese;
q->data.Chinese = t1;
t2 = p->data.Math;
p->data.Math = q->data.Math;
q->data.Math = t2;
t3 = p->data.English;
p->data.English = q->data.English;
q->data.English = t3;
t4 = p->data.aver;
p->data.aver= q->data.aver;
q->data.aver = t4;
strcpy(t5,p->data.name);
strcpy(p->data.name ,q->data.name);
strcpy(q->data.name ,t5);
temp=p->data.num;
p->data.num=q->data.num;
q->data.num=temp;
t6=p->data.sex;
p->data.sex=q->data.sex;
q->data.sex=t6;
}
printf("学号由低到高排序:\n");
ShowCLinkList(head);
}
void aver(CLinkNode *head)
{
CLinkNode *p,*q;
p=head;
int i;
for(i=0;i<n;i++)
{
p->data.aver=1.0*(p->data.Chinese + p->data.Math + p->data.English)/3.0;
p = p->next;
}
p=head;
long temp;
float t1,t2,t3,t4;
char t5[25];
char t6;
//冒泡排序
for (i=1;i<n;i++,p=p->next)
for (q=p->next;q!=head->next;q=q->next)
if (p->data.aver<q->data.aver)
{
//交换所有信息
t1 = p->data.Chinese;
p->data.Chinese = q->data.Chinese;
q->data.Chinese = t1;
t2 = p->data.Math;
p->data.Math = q->data.Math;
q->data.Math = t2;
t3 = p->data.English;
p->data.English = q->data.English;
q->data.English = t3;
t4 = p->data.aver;
p->data.aver= q->data.aver;
q->data.aver = t4;
strcpy(t5,p->data.name);
strcpy(p->data.name ,q->data.name);
strcpy(q->data.name ,t5);
temp=p->data.num;
p->data.num=q->data.num;
q->data.num=temp;
t6=p->data.sex;
p->data.sex=q->data.sex;
q->data.sex=t6;
}
printf("平均分由高到低排序:\n");
ShowCLinkList(head);
}
希望这篇文章对你有帮助!
仅供参考!
标签:head,课设,int,改查,next,链表,printf,data,CLinkNode 来源: https://blog.csdn.net/qq_47900759/article/details/120607988