基于C语言实现学生管理系统
作者:summery456
这篇文章主要为大家详细介绍了基于C语言实现学生管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C语言实现学生管理系统的具体代码,供大家参考,具体内容如下
1.目标要求:
1.学生成绩管理系统
2.可增、删、改、查、浏览
3.数据保存在文件中
2.C语言代码:
文件1:源文件
#include"mylist.h" //菜单设计 void menu(){ //所有文件都同步到文件 printf("-------------【学生管理信息】-------------------\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("------------------------------------------------\n"); } struct Node* list; //用户的交互 //根据所选的菜单项,作相应的事情 void keyDown(){ int choice = 0; struct student data; struct Node* pMove = NULL; scanf("%d",&choice); switch(choice){ case 0: printf("正常退出!\n"); system("pause"); exit(0); break; case 1: printf("-------------录入信息-------------------\n"); //插入链表 fflush(stdin); //清空缓冲区!!!!!!!!! printf("请输入学生的\n姓名、年龄、性别、电话:\n"); scanf("%s%d%s%s",data.name,&data.age,data.sex,data.tel); insertNodeByHead(list,data); break; case 2: printf("-------------浏览信息-------------------\n"); printList(list); //打印链表 break; case 3: printf("-------------修改信息-------------------\n"); printf("请输入要修改信息的学生姓名:"); scanf("%s",data.name); reviseInfoByData(list,data.name); break; case 4: printf("-------------删除信息-------------------\n"); printf("请输入删除学生的姓名:"); scanf("%s",data.name); deleteNode(list,data.name); break; case 5: printf("-------------查找信息-------------------\n"); printf("请输入要查找学生的姓名:"); scanf("%s",data.name); if( pMove = searchInfoByData(list,data.name)){ printf("姓名\t年龄\t性别\t电话\n"); printf("%s\t%d\t%s\t%s\n",pMove->data.name,pMove->data.age,pMove->data.sex,pMove->data.tel); } break; default: printf("选择错误,重新输入\n"); system("pause"); break; } writeInfoToFile(list,"1.txt"); } int main(){ list = createList(); readInfoFromFile(list,"1.txt"); while(1){ menu(); keyDown(); system("pause"); system("cls"); } system("pause"); return 0; }
文件2:mylist.h
#include<stdio.h> #include<stdlib.h> #include<string.h> //数据的设计-----》学生信息抽象出 struct student{ char name[20]; int age; char sex[5]; char tel[20]; // int math; // int english; };//结构 //测试的结构 struct Node{ struct student data; struct Node* next; }; //创建表头 struct Node* createList(){ struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); 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 insertNodeByHead(struct Node* headNode, struct student data){ struct Node* newNode = createNode(data); newNode->next = headNode->next; headNode->next = newNode; } //指定位置删除 void deleteNode(struct Node* headNode,char* name){ struct Node* posFrontNode; struct Node* posNode; if(headNode->next == NULL){ printf("链表为空!\n"); return; } posFrontNode = headNode; posNode = headNode->next; while(posNode && strcmp(posNode->data.name,name)){ posFrontNode = posNode; posNode = posFrontNode->next; } if(!posNode){ printf("您要删除的数据不存在!\n"); return; } if(!strcmp(posNode->data.name,name)){ posFrontNode->next = posNode->next; free(posNode); } } //查找功能 struct Node* searchInfoByData(struct Node* headNode,char* name) { struct Node* posNode; if(headNode->next == NULL){ printf("链表为空!\n"); return NULL; } posNode = headNode->next; while(posNode && strcmp(posNode->data.name,name)){; posNode = posNode->next; } if(!posNode){ printf("您要查找的数据不存在!\n"); return NULL; } return posNode; } //修改功能 void reviseInfoByData(struct Node* headNode,char* name) { struct Node* posNode; if(headNode->next == NULL){ printf("链表为空!\n"); return; } posNode = headNode->next; while(posNode && strcmp(posNode->data.name,name)){; posNode = posNode->next; } if(!posNode){ printf("您要修改的数据不存在!\n"); return; } printf("请输入学生[%s]的年龄、性别、电话:\n",posNode->data.name); scanf("%d%s%s",&posNode->data.age,posNode->data.sex,posNode->data.tel); } //文件读操作 void readInfoFromFile(struct Node* headNode,char* fileName){ //1.打开文件 FILE *fp; struct student data; fp = fopen(fileName,"r"); if(fp == NULL){ fopen(fileName,"w+");//打开文件具有创建功能 } //2.读文件 while(fscanf(fp,"%s\t%d\t%s\t%s\n",data.name,&data.age,data.sex,data.tel)!=EOF){ insertNodeByHead(headNode,data); } //3.关闭文件 fclose(fp); } //文件写操作 void writeInfoToFile(struct Node* headNode,char* fileName){ //1.打开文件 FILE *fp; fp = fopen(fileName,"w"); struct Node* pMove = headNode->next; //2.写文件 while(pMove){ fprintf(fp,"%s\t%d\t%s\t%s\n",pMove->data.name,pMove->data.age,pMove->data.sex,pMove->data.tel); pMove = pMove->next; } //3.关闭文件 fclose(fp); } //打印链表 void printList(struct Node* headNode){ struct Node* pMove = headNode->next; //设计列数据的处理 printf("姓名\t年龄\t性别\t电话\n"); while(pMove){ printf("%s\t%d\t%s\t%s\n",pMove->data.name,pMove->data.age,pMove->data.sex,pMove->data.tel); pMove = pMove->next; } printf("\n"); }
3.运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。