您好,登錄后才能下訂單哦!
本篇內容主要講解“基于C語言如何實現學生管理系統”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“基于C語言如何實現學生管理系統”吧!
1.學生成績管理系統
2.可增、刪、改、查、瀏覽
3.數據保存在文件中
文件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"); }
到此,相信大家對“基于C語言如何實現學生管理系統”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。