学生信息管理系统
文章目录
本项目是运用c语言实现的一个学生信息管理系统,主要实现学生信息的管理,采用哈希表加单链表的存储方式,利用哈希查找进行实现.
1.头文件
#ifndef _HASH_H_
#define _HASH_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#define SIZE 20
#define N 18
enum res
{
EXIST = -4,
NOEXIST,
NULLERROR,
MALLOCREEOR,
OK
};
//定义数据域结构体类型
typedef struct student
{
int id;
char name[30];
int age;
char sex[10];
char xuehao[20];
}Student;
//定义节点的结构体类型
typedef struct node
{
Student data;//数据域
struct node *next;//指针域
}Node;
//定义hash表的结构体类型
typedef struct hash
{
Node *hash[SIZE];//指针数组,数组元素为链表地址
int count;//记录个数
}Hash;
//创建hash函数
int HashFunc(Student *item);
//创建hash表
Hash *createHash(void);
//创建一个新节点
Node *createNode(void);
Student *creatStu(void);
//在hash表中添加数据
int insertItemHash(Hash *pHash,Student *stu);
//查找
int searchItemHash(Hash *pHash,Student *stu);
int deletes(Hash *pHash,Student *stu,Student *pData);
int updata(Hash *pHash,Student *stu);
int save(Hash *pHash,Student *stu,Student *delstu);
int load(Hash *pHash);
int destory(Hash **pHash);
int show(Hash *pHash);
#endif
2.主函数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"hash.h"
void nuem()
{
printf(****************学生信息管理系统***************);
printf("1.......insertItemHash\n");
printf("2.......searchItemHash\n");
printf("3.......delete\n");
printf("4.......updata\n");
printf("5.......save\n");
printf("6.......load\n");
printf("7.......destory\n");
printf("8.......show\n");
printf("-1......exit\n");
printf(****************学生信息管理系统***************);
}
int main(void)
{
Hash *pHash=NULL;
pHash=createHash();
Student *stu=NULL;
stu=creatStu();
Student *delstu=NULL;
delstu=creatStu();
int item;
int ret=0;
int op;
while(1)
{
nuem();
printf("请输入要进行的操作:\n");
scanf("%d",&op);
if(op==-1)
{
break;
}
switch(op)
{
case 1:
printf("请输入要添加的学生信息:\n");
scanf("%d%s%d%s%s",&stu->id,stu->name,&stu->age,stu->sex,stu->xuehao);
ret=insertItemHash(pHash,stu);
if(ret==OK)
{
printf("添加成功\n");
}
else
{
printf("添加失败\n");
}
break;
case 2:
printf("请输入要查找的学生编号:\n");
scanf("%d",&stu->id);
searchItemHash(pHash,stu);
break;
case 3:
printf("请输入要删除的学生编号:\n");
scanf("%d",&stu->id);
ret=deletes(pHash,stu,delstu);
if(ret==OK)
{
printf("删除成功\n");
}
break;
case 4:
printf("请输入要修改的学生信息:\n");
scanf("%d%s%d%s%s",&stu->id,stu->name,&stu->age,stu->sex,stu->xuehao);
ret=updata(pHash,stu);
if(ret==OK)
{
printf("修改成功\n");
}
break;
case 5:
printf("请输入要删除的学生编号:\n");
scanf("%d",&stu->id);
ret=save(pHash,stu,delstu);
if(ret==OK)
{
printf("保存成功\n");
}
break;
case 6:
ret=load(pHash);
if(ret==OK)
{
printf("恢复成功\n");
}
break;
case 7:
ret=destory(&pHash);
if(ret==OK)
{
printf("销毁成功\n");
}
break;
case 8:
ret=show(pHash);
break;
default:
printf("输入有误!\n");
}
}
}
3.功能函数
3.1建立哈希表、节点、学生结构体
Hash *createHash(void)
{
Hash *pHash=NULL;
pHash=(Hash *)malloc(sizeof(Hash));
if(pHash==NULL)
{
perror("malloc error");
return NULL;
}
memset(pHash,0,sizeof(Hash));
return pHash;
}
Node *createNode(void)
{
Node *pNode=NULL;
pNode=(Node *)malloc(sizeof(Node));
if(pNode==NULL)
{
perror("malloc error");
return NULL;
}
memset(pNode,0,sizeof(Node));
return pNode;
}
Student *creatStu(void)
{
Student *stu=NULL;
stu=(Student *)malloc(sizeof(Student));
if(stu==NULL)
{
printf("malloc error");
return NULL;
}
memset(stu,0,sizeof(Student));
return stu;
}
3.2哈希函数
int HashFunc(Student *item)
{
return item->id % N;
}
3.3增添函数
int insertItemHash(Hash *pHash,Student *stu)
{
if(pHash==NULL)
{
return NULLERROR;
}
int k;
Node *pTmp=NULL;
Node *pNew=createNode();
if(pNew==NULL)
{
return MALLOCREEOR;
}
pNew->data=*stu;
k=HashFunc(stu);
pTmp=pHash->hash[k];
if(pTmp!=NULL)
{
while(pTmp->next!=NULL)
{
pTmp=pTmp->next;
}
pTmp->next=pNew;
}
else
{
pHash->hash[k]=pNew;
}
pHash->count++;
return OK;
}
3.4查找函数
int searchItemHash(Hash *pHash,Student *stu)
{
if(pHash==NULL)
{
return NULLERROR;
}
if(pHash->count==0)
{
return NOEXIST;
}
int k;
k=HashFunc(stu);
Node *pTmp=pHash->hash[k];
if(pTmp==NULL)
{
return NOEXIST;
}
while(1)
{
if(NULL==pTmp)
{
break;
}
if(pTmp->data.id==stu->id)
{
printf("%d ",pTmp->data.id);
printf("%s ",pTmp->data.name);
printf("%d ",pTmp->data.age);
printf("%s ",pTmp->data.sex);
printf("%s\n",pTmp->data.xuehao);
return OK;
}
pTmp=pTmp->next;
}
}
3.5删除函数
int deletes(Hash *pHash,Student *stu,Student *delstu)
{
if(pHash==NULL)
{
return NULLERROR;
}
if(pHash->count==0)
{
return NOEXIST;
}
int k;
Node *pTmp=NULL;
Node *pDel=NULL;
Student *pData=delstu;
if(pData==NULL)
{
return MALLOCREEOR;
}
k=HashFunc(stu);
pTmp=pHash->hash[k];
pDel=pHash->hash[k];
if(pTmp==NULL)
{
return NOEXIST;
}
while(1)
{
if(pTmp->data.id==stu->id)
{
break;
}
pDel=pTmp;
pTmp=pTmp->next;
}
*pData=pTmp->data;
pDel->next=pTmp->next;
pTmp=NULL;
free(pTmp);
pHash->count--;
return OK;
}
3.6更新函数
int updata(Hash *pHash,Student *stu)
{
if(pHash==NULL)
{
return NULLERROR;
}
if(pHash==NULL)
{
return NOEXIST;
}
int k;
Node *pTmp=NULL;
k=HashFunc(stu);
pTmp=pHash->hash[k];
if(pTmp==NULL)
{
return NOEXIST;
}
while(1)
{
if(pTmp->data.id==stu->id)
{
break;
}
pTmp=pTmp->next;
}
pTmp->data=*stu;
return 0;
}
3.7上传函数
int save(Hash *pHash,Student *stu,Student *delstu)
{
if(pHash==NULL)
{
return NULLERROR;
}
if(pHash->count==0)
{
return NOEXIST;
}
int k;
FILE *fw=fopen("1.txt","a");
if(fw==NULL)
{
perror("open filer error");
return -1;
}
// char buf[1024]={0};
Node *pTmp=NULL;
Node *pDel=NULL;
Student *pData=delstu;
if(pData==NULL)
{
return MALLOCREEOR;
}
k=HashFunc(stu);
pTmp=pHash->hash[k];
pDel=pHash->hash[k];
if(pTmp==NULL)
{
return NOEXIST;
}
while(1)
{
if(pTmp->data.id==stu->id)
{
break;
}
pDel=pTmp;
pTmp=pTmp->next;
}
*pData=pTmp->data;
fwrite(pData,sizeof(Student),1,fw);
pDel->next=pTmp->next;
pTmp=NULL;
free(pTmp);
fclose(fw);
return OK;
}
3.8下载函数
int load(Hash *pHash)
{
if(pHash==NULL)
{
return NULLERROR;
}
int k;
FILE *fr=fopen("1.txt","r");
if(fr==NULL)
{
perror("open error");
return ;
}
Node *pTmp=NULL;
Node *pNew=createNode();
if(pNew==NULL)
{
return MALLOCREEOR;
}
// pNew->data=*stu;
fread(&(pNew->data),1,sizeof(Student),fr);
k=HashFunc(&(pNew->data));
pTmp=pHash->hash[k];
if(pTmp!=NULL)
{
while(pTmp->next!=NULL)
{
pTmp=pTmp->next;
}
pTmp->next=pNew;
}
else
{
pHash->hash[k]=pNew;
}
pHash->count++;
return OK;
}
3.9销毁函数
int destory(Hash **pHash)
{
if(pHash==NULL)
{
return NULLERROR;
}
int i;
Node *pDel=NULL;
Node *pTmp=NULL;
for(i=0;i<SIZE;i++)
{
pTmp=(*pHash)->hash[i];
while(pTmp!=NULL)
{
//头删
pDel=pTmp;
pTmp=pTmp->next;
free(pDel);
pDel=NULL;
}
}
free(*pHash);
*pHash=NULL;
return OK;
}