文件链表综合之学生管理系统

学生管理系统

将链表作为内存数据模型,将文件作为数据库,将终端作为交互界面。读文件生成
链表,修改链表写入文件。

实现功能:
1,初始化现有数据到文件
2,读文件生链表
3,操作链表(增加,删除)
4,写链表到文件

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
//初始化数据库,此时的数据库是文件
//读数据库,生成内存数据模型 
//增查改删 
//更新数据库 
typedef struct student
{
   char name[30];
   char sex[6];
   int age;
   float score;	
}Stu;

typedef struct _StuNode
{
	Stu date;
	struct _StuNode *next; 
}StuNode; 

void initDateFiLE()
{
    Stu	s[5]=
	{
	   "张三","男",22,89,
	   "李四","女",21,49,
	   "王五","男",24,66,
	   "赵六","女",18,67,
	   "小七","男",22,11,
	};
	FILE *pf=fopen("stu.date","w+");//将数组内容写进文件中去 
	if(NULL==pf)
	{
		exit(-1);printf("erro");
	}
	
	fwrite((void*)s,sizeof(s),1,pf);
	fclose(pf);
	return;
} 

StuNode *creatListFromFile(char *path)
{
	FILE *pf=fopen(path,"r+");
	
	if(NULL==pf)
	exit(-1);
	StuNode *head=(StuNode *)malloc(sizeof(StuNode));
	head->next=NULL;
	
	StuNode *cur=(StuNode *)malloc(sizeof(StuNode));
	while(fread((void*)&cur->date,sizeof(Stu),1,pf))//1 0
	{
		cur->next =head->next ;
		head->next=cur;
		cur=(StuNode *)malloc(sizeof(StuNode));
	}
	free(cur);
	return head;
}

void traverseStuList(StuNode *head)
{
	printf("           学生管理系统       \n\n"); 
	printf("\t\t\t\t\t\t\t\t  有疑问请联系	QQ:1073241584\n\n"); 
	printf("姓名\t      学号\t年龄\t分数\n");
	putchar(10);
	head=head->next;
	while(head)
	{
		printf("%-13s %-10s %-5d %-1.2f\n",head->date.name ,head->date.sex ,head->date.age ,head->date.score);
		head=head->next ;
	}	
}
void addListStuNode(StuNode *head)//增加 
{
	StuNode *cur=(StuNode *)malloc(sizeof(StuNode));
	printf("姓名:");
	scanf("%s",cur->date.name);//数组名就是首地址 	
	getchar(); 
	
	printf("性别:");
	scanf("%s",&cur->date.sex);//注意加&取地址 
	
	printf("年龄:");
	scanf("%d",&cur->date.age);
	
	printf("分数:");
	scanf("%f",&cur->date.score);
	cur->next =head->next ; 
	head->next=cur;	
}
StuNode  *searchListStuNode(StuNode *head)//根据姓名查找 
{
	char Name[30];
	printf("请输入您需要查找的名字\n"); 
	scanf("%s",Name);
	getchar();
	head=head->next;
	while(head)
	{
		if(strcmp(head->date.name,Name)==0)
		break;
		head=head->next ;
	}
	return head;
}
void *deletedListStuNode(StuNode *head)//删除学生信息 
{
	StuNode *pfind=searchListStuNode(head);//删的前提是先查找 
	if(NULL==pfind) printf("您要删除的人不在本学生管理系统中"); 
	while(head->next !=pfind) 
    head =head->next;
    head->next =pfind->next;
    free(pfind);
}
int lenthList(StuNode *head) //求系统中数据长度 
{
   int len =0;
   head=head->next;
   while(head)
   {
   	len++;
   	head=head->next ;
   }
   return len;
}
void *sortList(StuNode *head)//排序 
{
	int len= lenthList(head);
	StuNode *prep,*p,*q; 
	for(int i=0;i<len-1;i++)
	{
		prep=head;
		p=prep->next;
		q=p->next;
		for(int j=i;j<len-i-1;j++)
        {
        	if(p->date.score>q->date.score)//按成绩排序 
        	{
        		prep->next =q;
        		p->next =q->next ;
        		q->next =p;
        		
        		prep=q;
        		q=p->next ;
        		continue;
			}
        	prep=prep->next ;
		    p=p->next;
		    q=q->next;
		}		
	}
}

void *saveListStuFile(StuNode *head,char * pathFile) //保存文件 
{
	FILE *pf=fopen(pathFile,"w+");
	if(NULL==pf) exit(-1);
	head=head->next;
	while(head)
	{
		fwrite((void *)&head->date ,sizeof(Stu),1,pf);
		head=head->next;
	}
	fclose(pf);
}
void *destroyListStu(StuNode *head)
{
	StuNode *t;
	while(head)
	{
		t=head;
		head=head->next;
		free(t);
	}
}
int main()
{
	initDateFiLE();
	StuNode *head=creatListFromFile("stu.date");
	StuNode *pfind;//查找的名字 
	while(1)
	{
//		system("cls");//清屏函数 
		traverseStuList(head);
		printf("1->增加\t 2->查找 3->删除 4->排序 5->退出\n") ;//设置增删查改的指令 
		int choice;
		scanf("%d",&choice);
		switch(choice)
		{
			case 1:
				addListStuNode(head);
				break;
			case 2:
				if(pfind=searchListStuNode(head))
				{
					printf("您要查找的数据在本学生管理系统中\n\n");
					printf("姓名\t      学号\t年龄\t分数\n");
					printf("%-13s %-10s %-5d %-1.2f\n",pfind->date.name ,
					pfind->date.sex ,pfind->date.age ,pfind->date.score);
				}
				else
					printf("无此学生数据信息") ;
					getchar();   
				break;
			case 3:
				deletedListStuNode(head);
				break;
			case 4:
				sortList(head); //成绩排序 
				getchar();
				break;
			case 5:
				saveListStuFile(head,"stu.date"); //保存文件 
				destroyListStu(head);
				break;
			default:
				printf("您的输入有误\n"); 
	   }	
	}	
    return 0;
}

代码效果演示
在这里插入图片描述

这是在学完链表文件后做的一个学生管理系统,虽然现在QT图形化界面的学生管理系统已经非常完善,但是底层驱动逻辑实现方法是不会变的,所以还是有学习的必要。需要源代码资料的可在博客下方评论活私聊我QQ,不足之处还望指教。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值