//manangement.c主要运行代码
#include"manangement.h"
#include"linkedlist.h"
bool login_status=false;
void login(){
printf("=================================\n");
printf("===============登陆==============\n");
printf("\t请输入用户名:");
char user[128]={0};
fgets(user,sizeof(user)-1,stdin);
user[strlen(user)-1]='\0';
printf("\t请输入密码:");
char password[128]={0};
fgets(password,sizeof(password)-1,stdin);
password[strlen(password)-1]='\0';
if(strcmp(user,"linux")==0 && strcmp(password,"hello")==0){
printf("登陆成功\n");
printf("^.^\n");
login_status=true;
}else{
printf("用户输入用户名或密码输入错误\n");
}
}
//创建哈希表
void create_hashtable(hash_t** p_hashtable){
hash_t* p=(hash_t*)malloc(sizeof(hash_t));
if(NULL==p){
printf("malloc failure");
return;
}
memset(p,0,sizeof(hash_t));
p->n=0;
for(int i=0;i<SIZE;i++){
linkedlist_init(&(p->item[i]));
}
*p_hashtable=p;
//将data添加到链表的尾部
}
uint32_t get_hashIndex(char* id){
long hashcode =0;
while(*id!='\0'){
hashcode += *id;
id++;
}
return(uint32_t)(hashcode%SIZE);
}
void insert_stu(hash_t* p_hashtable){
datatype_t* stu =(datatype_t*)malloc(sizeof(datatype_t));
if(NULL==stu){
printf("create failure\n");
exit(EXIT_FAILURE);
}
memset(stu,0,sizeof(hash_t));
printf("请输入学生信息\n");
printf("ID:");
scanf("%s",stu -> id);
while (getchar() != '\n');
printf("姓名:");
scanf("%49s", stu->name);
printf("性别:");
scanf("%9s", stu->gender);
printf("年龄:");
scanf("%d", &(stu->age));
printf("成绩:");
scanf("%f", &(stu->score));
uint32_t hashIndex =get_hashIndex(stu->id);
bool result = linkedlist_insert_head(p_hashtable->item[hashIndex],stu);
if(result){
printf("输入成功^_^\n");
p_hashtable->n++;
}else{
printf("输入失败\n");
}
}
void show_stus(hash_t* p_hashtable){
printf("-----------------学生如下----------------\n");
printf("\tID\t姓名\t性别\t年龄\t成绩\t\n");
for(int i=0;i<SIZE;i++){
linkedlist_show(*(p_hashtable->item+i));
}
printf("-----------------------------------------\n");
}
二,manangement.h
#ifndef __HEAD_MANAGEMENT_H__
#define __HEAD_MANAGEMENT_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <stdbool.h>
#define SIZE 13
typedef struct{
char id[128];
char name[128];
char gender[64];
uint32_t age;
float score;
}datatype_t;
typedef struct Node{
datatype_t* data;
struct Node* next;
}linkednode_t;
typedef struct {
linkednode_t* item[SIZE];
int n;
}hash_t;
// 登录状态
extern bool login_status;
extern void login();
extern void create_hashtable(hash_t**);
// 创建hash函数
extern uint32_t get_hashIndex(char*);
extern void insert_stu(hash_t*);
extern void show_stus(hash_t*);
extern void show_stus_linkedlist(hash_t*);
#endif
三.main.c
#include "management.h"
void menu();
int main(){
while(!login_status){
login();
}
int select;
hash_t* hashtable=NULL;
// 创建hashtable
create_hashtable(&hashtable);
if(NULL == hashtable){
printf("hash table is NULL\n");
exit(EXIT_FAILURE);
}
do{
menu();
printf("请选择:");
scanf("%d",&select);
switch(select){
case 1:
insert_stu(hashtable);
break;
case 2:
show_stus(hashtable);
break;
case 3:
//search_stu_by_id();
break;
case 4:
//modify_stu_by_id();
break;
case 5:
//del_stu_by_id();
break;
case 0:
printf("欢迎下次光临!\n");
exit(EXIT_SUCCESS);
case 8:
show_stus_linkedlist(hashtable);
break;
}
}while(1);
}
void menu(){
printf("-----------------------------------------\n");
printf("|\t\t学生管理系统 |\n");
printf("-----------------------------------------\n");
printf("|\t\t1、增加学生 \t\t|\n");
printf("|\t\t2、显示学生信息 \t|\n");
printf("|\t\t3、查找学生 \t\t|\n");
printf("|\t\t4、修改学生信息 \t|\n");
printf("|\t\t5、删除学生 \t\t|\n");
printf("|\t\t0、退出 \t\t|\n");
printf("----------------------------------------\n");
}
四。效果图