这几天用学习用C语言写了一个学生信息管理系统,在此分享出来。
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
struct student
{
char name[20];
char num[20];
int age;
char sex[5];
char telephone[20];
};
char nam[20],number[20],s[5],tele[20];
int ag;
//分结构去写
struct Node
{
//int data;
struct student data;
struct Node* next;
};
//创建表
struct Node* createList()
{
//结构体变量表示表头
//指针变成变量 用动态内存申请
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
//表头:做差异化处理 数据域data不做初始化
headNode->next = NULL;
return headNode;
};
//创建结点
struct Node* createNode(struct student data)
{
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//系统帮助及说明
void help()
{
printf("\n\t欢迎使用系统帮助!\n");
printf("\n\t1.初次进入系统后,请先选择录入学生信息;\n");
printf("\n\t2.按照菜单提示键输入数字代号;\n");
printf("\n\t3.增加学生信息后,信息会被保存在该程序所在的文件夹中的《学生信息档案》文件中;\n");
printf("\n\t谢谢您的使用!再见\n");
}
//录入功能
void insertNodeByHead(struct Node* headNode, struct student data)
{
struct Node* pMove = headNode->next;
printf("请输入学生姓名,学号,年龄,性别,电话:\n");
fflush(stdin); //清空缓冲区
scanf("%s%s%d%s%s", &data.name,&data.num, &data.age, &data.sex, &data.telephone);
while (pMove != NULL)
{
if (strcmp(pMove->data.num, data.num) == 0)
{
printf("录入信息学号重复,请重新录入信息\n");
return;
}
break;
}
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
//删除功能
void deleteNode(struct Node* headNode, char* name)
{
struct Node* posNode = headNode->next;
struct Node* posFrontNode = headNode;
if(posNode == NULL)
{
printf("数据为空,无法删除!\n");
return;
}
//姓名是字符串用strcmp函数
while (strcmp(posNode->data.name,name))
{
posFrontNode = posNode;
posNode = posFrontNode->next;
if (posNode == NULL)
{
printf("未找到指定位置,无法删除!\n");
return;
}
}
//找到了
posFrontNode->next = posNode->next;
free(posNode);
}
//修改功能
void change(struct Node* headNode)
{
printf("请输入要修改信息的学生学号:\n");
scanf("%s",number);
struct Node* changeNode = headNode->next;
if(changeNode == NULL)
{
printf("数据为空,无法改变!\n");
return NULL;
}
else
{
if(0==strcmp(changeNode->data.num,number))
{
printf("请输入该学生要改变的信息选项:1、姓名 2、电话号码 3、年龄\n");
int choose;
scanf("%d",&choose);
switch(choose)
{
case 1:
printf("请输入您要修改的名字:");
scanf("%s",nam);
printf("修改的名字为:%s\n",nam);
strcpy(changeNode->data.name,nam);
printf("修改名字成功!\n");
break;
case 2:
printf("请输入您要修改的电话号码");
scanf("%s",&tele);
printf("修改的电话号码为:%s\n",tele);
strcpy(changeNode->data.telephone,tele);
printf("修改电话号码成功!\n");
break;
case 3:
printf("请输入您要修改的年龄:");
scanf("%d",&ag);
printf("修改的年龄为:%d\n",ag);
changeNode->data.age=ag;
printf("修改年龄成功!\n");
break;
default :
printf("请输入正确的选项\n");
break;
}
}
}
}
//查找功能
struct Node* searchInfoByData(struct Node* headNode, char* name)
{
struct Node* pMove = headNode->next;
if (pMove == NULL)
{
return NULL;
}
if (strcmp(pMove->data.name, name) != 0) {
return NULL;
}
while (strcmp(pMove->data.name, name))
{
pMove = pMove->next;
}
return pMove;
}
//浏览功能
void printList(struct Node* headNode)
{
struct Node* pMove = headNode->next;
//设计到数据的处理
printf("姓名\t学号\t年龄\t性别\t电话\n");
while (pMove)
{
printf("%s\t%s\t%d\t%s\t%s\n",
pMove->data.name,pMove->data.num,pMove->data.age,pMove->data.sex,pMove->data.telephone);
pMove = pMove->next;
}
printf("\n");
}
//文件的存储
void writeInfoToFile(struct Node* headNode, char* fileName)
{
FILE* fp;
fp= fopen(fileName,"w");
if (fp == NULL)
{
printf("文件打开失败!\n");
}
struct Node* pMove = headNode->next;
while (pMove)
{
fprintf(fp, "%s\t%s\t%d\t%s\t%s\n", pMove->data.name,pMove->data.num, pMove->data.age, pMove->data.sex, pMove->data.telephone);
pMove = pMove->next;
}
fclose(fp);
}
//文件的读取
void readInfoToFile(struct Node* headNode, char *fileName)
{
//1、打开文件
FILE* fp;
struct student data;
fp = fopen(fileName, "rt");
if (fp == NULL)
{
fopen(fileName, "wt+");
}
//2、读文件
while (fscanf(fp, "%s\t%s\t%d\t%s\t%s\n", data.name, &data.num,&data.age, data.sex, data.telephone) != EOF)
{
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
//3、关闭文件
fclose(fp);
}
void menu()
{
//所有操作都同步到文件
printf("********************************************\n");
printf("-----------【学生信息管理系统】-------------\n");
printf("********************************************\n");
printf("***************系统功能菜单*****************\n\n");
printf("\t\t0.系统说明\n");
printf("\t\t1.录入信息\n");
printf("\t\t2.浏览信息\n");
printf("\t\t3.修改信息\n");
printf("\t\t4.删除信息\n");
printf("\t\t5.查找信息\n");
printf("\t\t6.退出系统\n\n");
printf("-----------------欢迎使用-------------------\n");
printf("********************************************\n");
}
struct Node* list;
//用户的交互
//根据所选的菜单项,做相应的事情
void keyDown()
{
int choice = 0;
struct student data;
struct Node* pMove = NULL;
printf("请选择您想使用的功能\n");
scanf("%d",&choice);
switch (choice)
{
case 0:
printf("***********系统帮助及说明***********");
help();
break;
case 1:
printf("--------【录入信息】--------\n");
//增加学生信息
insertNodeByHead(list, data);
break;
case 2:
printf("--------【浏览信息】--------\n");
printList(list); //打印链表
break;
case 3:
printf("--------【修改信息】--------\n");
change(list);
break;
case 4:
printf("--------【删除信息】--------\n");
printf("请输入删除的学生姓名:\n");
scanf("%s", data.name);
deleteNode(list, data.name);
break;
case 5:
printf("--------【查找信息】--------\n");
printf("请输入要查找的学生姓名:\n");
scanf("%s", data.name);
pMove = searchInfoByData(list, data.name);
if (pMove == NULL)
{
printf("未找到相关信息!\n");
system("pause");
}
else
{
printf("姓名\t学号\t年龄\t性别\t电话\n");
printf("%s\t%s\t%d\t%s\t%s\n", pMove->data.name,pMove->data.num,pMove->data.age,pMove->data.sex,pMove->data.telephone);
}
break;
case 6:
printf("正常退出!");
system("pause");
exit(0);
break;
default:
printf("选择错误,重新输入\n");
system("pause");
break;
}
writeInfoToFile(list,"学生信息档案.txt");
}
int main() {
list = createList();
readInfoToFile(list, "学生信息档案.txt");
while (1)
{
menu();
keyDown();
system("pause");
system("cls");
}
return 0;
}
对学生信息管理系统感兴趣,还想了解更多的童鞋欢迎看我写的第二版系统,我又添加了密码登录模式
C语言学生信息管理系统第二版