C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單學(xué)生信息管理系統(tǒng)
學(xué)生信息管理系統(tǒng)的功能有,也可以自己增加或者改進(jìn)一些函數(shù)功能。

在main函數(shù)里調(diào)用這8個(gè)函數(shù)
學(xué)生信息包含姓名、年齡、學(xué)號(hào)、成績(jī),需要定義一個(gè)結(jié)構(gòu)體(結(jié)構(gòu)體是全局變量,所以需要全局聲明):
typedef struct _student{
?? ?char name[20];
?? ?int age;
?? ?int stuNum;
?? ?int score;
}student;需要有一個(gè)存儲(chǔ)數(shù)據(jù)的空間,所以使用單鏈表存儲(chǔ),定義如下:
typedef struct _Node{
?? ?student stu1;
?? ?struct _Node* pNext;
}Node;此時(shí)需要給鏈表一個(gè)頭:
Node *g_head=NULL;
錄入學(xué)生信息:
1)創(chuàng)建一個(gè)新節(jié)點(diǎn),結(jié)點(diǎn)里包括結(jié)構(gòu)體數(shù)據(jù),和下一個(gè)指針。
2)需要判斷頭結(jié)點(diǎn)是不是空的,如果是空的,則此時(shí)需要將新節(jié)點(diǎn)付給頭結(jié)點(diǎn),如果不是空的,那么該結(jié)點(diǎn)的下一個(gè)指針=頭結(jié)點(diǎn)(頭插法,能用就行)。
3)然后就輸入數(shù)據(jù)scanf("%s",&p->stu1.age);
4)最后提示一下該學(xué)生信息輸入完畢!
5)現(xiàn)在只輸入了一個(gè)學(xué)生信息,此時(shí)只需要在主函數(shù)里寫一個(gè)循環(huán)就可以無(wú)限調(diào)用該函數(shù)進(jìn)行數(shù)據(jù)錄入。
void input(){
?? ?printf("請(qǐng)輸入學(xué)生信息\t\n");
?? ?Node *pNewNode=(Node*)malloc(sizeof(Node));//創(chuàng)建一個(gè)新節(jié)點(diǎn)。
?? ?pNewNode->pNext=NULL;
?? ?
?? ?if(g_head==NULL){
?? ??? ?g_head=pNewNode;
?? ?}
?? ?else{
?? ??? ?pNewNode->pNext=g_head;
?? ??? ?g_head=pNewNode;
?? ?}
?? ?printf("請(qǐng)輸入姓名: ");
?? ?scanf("%s",pNewNode->stu1.name);
?? ?printf("請(qǐng)輸入年齡: ");
?? ?scanf("%d",&pNewNode->stu1.age);
?? ?printf("請(qǐng)輸入學(xué)號(hào) : ");
?? ?scanf("%d",&pNewNode->stu1.stuNum);
?? ?printf("請(qǐng)輸入成績(jī) : ");
?? ?scanf("%d",&pNewNode->stu1.score);
printf("該學(xué)生信息輸入完畢!\n\n");
}查看信息:
1)需要對(duì)鏈表進(jìn)行遍歷然后printf;
2)新建一個(gè)結(jié)點(diǎn)指針然后 Node* p=g_head; while(p!=NULL){ printf p=p->next}(注:以上代碼只是功能的描述)
代碼:
void printdate(){
?? ?Node* p=g_head;
?? ?printf("\t姓名\t年齡\t學(xué)號(hào)\t成績(jī)\n");
?? ?while(p!=NULL)
?? ?{
?? ??? ?printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
?? ??? ?p=p->pNext;
?? ?}
}保存信息:
1)先用文件指針指向一個(gè)要使用的文件,
2)便利所有鏈表的數(shù)據(jù),將遍歷的數(shù)據(jù)寫入文件指針指向的文件里
void save(){
?? ?FILE *fp=fopen("E:\\stu.data","w");
?? ?if(fp==NULL){
?? ??? ?printf("文件打開(kāi)失敗");
?? ??? ?return;
?? ?}
?? ?Node* p=g_head;
?? ?while(p!=NULL){
?? ??? ?fwrite(&p->stu1,1,sizeof(student),fp);
?? ??? ?p=p->pNext;
?? ?}
?? ?fclose(fp);
?? ?printf("文件保存成功!\n");
}文件讀取:
1)先用文件指針指向一個(gè)使用的文件,
2)這里需要開(kāi)辟鏈表來(lái)將讀取到的數(shù)據(jù)寫入鏈表里的數(shù)據(jù)里的結(jié)構(gòu)體里,
3)需要判斷一下是否讀取到了數(shù)據(jù),將文件里的數(shù)據(jù)先讀到結(jié)構(gòu)體數(shù)組里
4)然后while循環(huán)里 ,,將結(jié)構(gòu)體數(shù)組里的數(shù)據(jù)復(fù)制給鏈表,
5)需要將每一個(gè)節(jié)點(diǎn)排好隊(duì)列,這里用頭插法,每復(fù)制一個(gè)就會(huì)開(kāi)辟一個(gè)結(jié)點(diǎn),
代碼:
void rs(){
?? ?FILE* fp=fopen("E:\\stu.data","r");
?? ?if(fp==NULL){
?? ??? ?printf("文件打開(kāi)失敗");
?? ?}
?? ?printf("文件讀取成功!\n 查看文件請(qǐng)按2\n");
?? ?student stu;
?? ?
?? ?while(fread(&stu,1,sizeof(student),fp)){
?? ??? ?Node* pNewNode=(Node*)malloc(sizeof(Node));
?? ??? ?pNewNode->pNext=NULL;
?? ??? ?memcpy(pNewNode,&stu,sizeof(student));
?? ??? ?if(g_head==NULL){
?? ??? ??? ?g_head=pNewNode;
?? ??? ?}
?? ??? ?else{
?? ??? ??? ?pNewNode->pNext=g_head;
?? ??? ??? ?g_head=pNewNode;
?? ??? ?}
?? ?}
?? ?}統(tǒng)計(jì)人數(shù):
1)遍歷鏈表的時(shí)候定義一個(gè)整型數(shù)組自加最后輸出
void count(){
?? ?int a=0;
?? ?FILE* fp=fopen("E:\\stu.data","r");
?? ?if(fp==NULL){
?? ??? ?printf("文件打開(kāi)失敗");
?? ??? ?return;
?? ?}
?? ?Node* p=g_head;
?? ?while(p!=NULL){
?? ??? ?p=p->pNext;
?? ??? ?a++;
?? ?}
?? ?printf("總?cè)藬?shù)%d",a);
}查找學(xué)生:
1)定義整型數(shù)據(jù),輸入學(xué)號(hào),先遍歷再if判斷輸入的學(xué)號(hào)和p->stu1.num是否相等,相等的話輸出:
void find(){
?? ?int num;
?? ?printf("請(qǐng)輸入要查找的學(xué)生學(xué)號(hào): \n");
?? ?scanf("%d",&num);
?? ?Node* p=g_head;
?? ?while(p!=NULL){
?? ??? ?if(p->stu1.stuNum==num){
?? ??? ??? ?printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
?? ??? ?}
?? ??? ?p=p->pNext;
?? ?}
printf("無(wú)該學(xué)生信息");
}修改信息:
1)定義整型數(shù)據(jù),輸入學(xué)號(hào),先遍歷再if判斷輸入的學(xué)號(hào)和p->stu1.num是否相等,相等的話重新輸入:
void change(){
?? ?int num;
?? ?printf("請(qǐng)輸入要修改的學(xué)生的學(xué)號(hào): ");
?? ?scanf("%d",&num);
?? ?Node* p=g_head;
?? ?while(p!=NULL){
?? ??? ?if(p->stu1.stuNum==num){
?? ??? ??? ?printf("請(qǐng)輸入姓名: \n");
?? ??? ??? ?scanf("%s",p->stu1.name);
?? ??? ??? ?printf("請(qǐng)輸入年齡: \n");
?? ??? ??? ?scanf("%d",&p->stu1.age);
?? ??? ??? ?printf("請(qǐng)輸入學(xué)號(hào): \n");
?? ??? ??? ?scanf("%d",&p->stu1.stuNum);
?? ??? ??? ?printf("請(qǐng)輸入成績(jī): \n");
?? ??? ??? ?scanf("%d",&p->stu1.score);
?? ??? ??? ?printf("信息更改完畢!");
?? ??? ?}
?? ??? ?p=p->pNext;
?? ?}
?? ?if(p==NULL){
?? ??? ?printf("該學(xué)生不存在!\n");
?? ?}
}刪除信息:
1)思路是先遍歷,找到了free就可以了,
2)需要定義兩個(gè)節(jié)點(diǎn)指針,如果找到了是頭結(jié)點(diǎn)直接將頭結(jié)點(diǎn)付給定義的節(jié)點(diǎn),然后free,
如果不是頭結(jié)點(diǎn),將該節(jié)點(diǎn)付給定義的節(jié)點(diǎn),p->next=p->next->next;
然后free
void del(){
?? ?int num;
?? ?printf("請(qǐng)輸入要?jiǎng)h除的學(xué)號(hào)");
?? ?scanf("%d",&num);
?? ?Node* p=g_head;
?? ?Node*p1,*p2;
?? ?if(p->stu1.stuNum==num){
?? ??? ?p1=p->pNext;
?? ??? ?free(p1);
?? ?}
?? ?if(p->pNext!=NULL){
?? ??? ?p2=p->pNext;
?? ??? ?p->pNext=p->pNext->pNext;
?? ??? ?free(p2);
?? ?}
printf("學(xué)號(hào)為%d的信息刪除成功!\n",num);
}下面是完整代碼:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
typedef struct _student{
?? ?char name[20];
?? ?int age;
?? ?int stuNum;
?? ?int score;
}student;
typedef struct _Node{
?? ?student stu1;
?? ?struct _Node* pNext;
}Node;
Node *g_head=NULL;
void menu(){
?? ?printf("------------------\n");
?? ?printf("- 1錄入信息-\n");
?? ?printf("- 2查看信息-\n");
?? ?printf("- 3保存信息-\n");
?? ?printf("- 4讀取信息-\n");?? ?
?? ?printf("- 5統(tǒng)計(jì)人數(shù)-\n");
?? ?printf("- 6查找信息-\n");
?? ?printf("- 7修改信息-\n");
?? ?printf("- 8刪除信息-\n");
?? ?printf("- 0退出-\n");
?? ?printf("退出不要直接點(diǎn)叉,請(qǐng)按0退出!\n查看文件之前請(qǐng)先讀取文件!\n");
}
void input(){
?? ?printf("請(qǐng)輸入學(xué)生信息\t\n");
?? ?Node *pNewNode=(Node*)malloc(sizeof(Node));//創(chuàng)建一個(gè)新節(jié)點(diǎn)。
?? ?pNewNode->pNext=NULL;
?? ?
?? ?if(g_head==NULL){
?? ??? ?g_head=pNewNode;
?? ?}
?? ?else{
?? ??? ?pNewNode->pNext=g_head;
?? ??? ?g_head=pNewNode;
?? ?}
?? ?printf("請(qǐng)輸入姓名: ");
?? ?scanf("%s",pNewNode->stu1.name);
?? ?printf("請(qǐng)輸入年齡: ");
?? ?scanf("%d",&pNewNode->stu1.age);
?? ?printf("請(qǐng)輸入學(xué)號(hào) : ");
?? ?scanf("%d",&pNewNode->stu1.stuNum);
?? ?printf("請(qǐng)輸入成績(jī) : ");
?? ?scanf("%d",&pNewNode->stu1.score);
printf("該學(xué)生信息輸入完畢!\n\n");
}
void printdate(){
?? ?Node* p=g_head;
?? ?printf("\t姓名\t年齡\t學(xué)號(hào)\t成績(jī)\n");
?? ?while(p!=NULL)
?? ?{
?? ??? ?printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
?? ??? ?p=p->pNext;
?? ?}
}
void save(){
?? ?
?? ?FILE *fp=fopen("E:\\stu.data","w");
?? ?if(fp==NULL){
?? ??? ?printf("文件打開(kāi)失敗");
?? ??? ?return;
?? ?}
?? ?Node* p=g_head;
?? ?while(p!=NULL){
?? ??? ?fwrite(&p->stu1,1,sizeof(student),fp);
?? ??? ?p=p->pNext;
?? ?}
?? ?fclose(fp);
?? ?printf("文件保存成功!\n");
}
void rs(){
?? ?FILE* fp=fopen("E:\\stu.data","r");
?? ?if(fp==NULL){
?? ??? ?printf("文件打開(kāi)失敗");
?? ?}
?? ?printf("文件讀取成功!\n 查看文件請(qǐng)按2\n");
?? ?student stu;
?? ?
?? ?while(fread(&stu,1,sizeof(student),fp)){
?? ??? ?Node* pNewNode=(Node*)malloc(sizeof(Node));
?? ??? ?pNewNode->pNext=NULL;
?? ??? ?memcpy(pNewNode,&stu,sizeof(student));
?? ??? ?if(g_head==NULL){
?? ??? ??? ?g_head=pNewNode;
?? ??? ?}
?? ??? ?else{
?? ??? ??? ?pNewNode->pNext=g_head;
?? ??? ??? ?g_head=pNewNode;
?? ??? ?}
?? ?}
?? ?}
void count(){
?? ?int a=0;
?? ?FILE* fp=fopen("E:\\stu.data","r");
?? ?if(fp==NULL){
?? ??? ?printf("文件打開(kāi)失敗");
?? ??? ?return;
?? ?}
?? ?Node* p=g_head;
?? ?while(p!=NULL){
?? ??? ?p=p->pNext;
?? ??? ?a++;
?? ?}
?? ?printf("總?cè)藬?shù)%d",a);
}
void find(){
?? ?int num;
?? ?printf("請(qǐng)輸入要查找的學(xué)生學(xué)號(hào): \n");
?? ?scanf("%d",&num);
?? ?Node* p=g_head;
?? ?while(p!=NULL){
?? ??? ?if(p->stu1.stuNum==num){
?? ??? ??? ?printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
?? ??? ?}
?? ??? ?p=p->pNext;
?? ?}
printf("have not");
}
void change(){
?? ?int num;
?? ?printf("請(qǐng)輸入要修改的學(xué)生的學(xué)號(hào): ");
?? ?scanf("%d",&num);
?? ?Node* p=g_head;
?? ?while(p!=NULL){
?? ??? ?if(p->stu1.stuNum==num){
?? ??? ??? ?printf("請(qǐng)輸入姓名: \n");
?? ??? ??? ?scanf("%s",p->stu1.name);
?? ??? ??? ?printf("請(qǐng)輸入年齡: \n");
?? ??? ??? ?scanf("%d",&p->stu1.age);
?? ??? ??? ?printf("請(qǐng)輸入學(xué)號(hào): \n");
?? ??? ??? ?scanf("%d",&p->stu1.stuNum);
?? ??? ??? ?printf("請(qǐng)輸入成績(jī): \n");
?? ??? ??? ?scanf("%d",&p->stu1.score);
?? ??? ??? ?printf("信息更改完畢!");
?? ??? ?}
?? ??? ?p=p->pNext;
?? ?}
?? ?if(p==NULL){
?? ??? ?printf("該學(xué)生不存在!\n");
?? ?}
}
void del(){
?? ?int num;
?? ?printf("請(qǐng)輸入要?jiǎng)h除的學(xué)號(hào)");
?? ?scanf("%d",&num);
?? ?Node* p=g_head;
?? ?Node*p1,*p2;
?? ?if(p->stu1.stuNum==num){
?? ??? ?p1=p->pNext;
?? ??? ?free(p1);
?? ?}
?? ?if(p->pNext!=NULL){
?? ??? ?p2=p->pNext;
?? ??? ?p->pNext=p->pNext->pNext;
?? ??? ?free(p2);
?? ?}
printf("學(xué)號(hào)為%d的信息刪除成功!\n",num);
}
int main()
{
?? ?menu();
?? ?while(1)
?? ?{
?? ??? ?char ch=getch();
?? ??? ?switch(ch){
?? ??? ?case '1':input();break;
?? ??? ?case '2':printdate();break;
?? ??? ?case '3':save();break;
?? ??? ?case '4':rs();break;
?? ??? ?case '5':count();break;
?? ??? ?case '6':find();break;
?? ??? ?case '7':change();break;
?? ??? ?case '8':del();break;
?? ??? ?case '0':exit(0);
?? ??? ?
?? ??? ?}
?? ?}
return 0;
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++11中l(wèi)onglong超長(zhǎng)整型和nullptr初始化空指針
本文介紹?C++11?標(biāo)準(zhǔn)中新添加的?long?long?超長(zhǎng)整型和?nullptr?初始化空指針,在?C++11?標(biāo)準(zhǔn)下,相比?NULL?和?0,使用?nullptr?初始化空指針可以令我們編寫的程序更加健壯,本文結(jié)合示例代碼給大家詳細(xì)講解,需要的朋友跟隨小編一起看看吧2022-12-12
C語(yǔ)言實(shí)現(xiàn)輸入ascii碼,輸出對(duì)應(yīng)的字符方式
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)輸入ascii碼,輸出對(duì)應(yīng)的字符方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
一些C語(yǔ)言中字符串的算法問(wèn)題解決實(shí)例小結(jié)
這篇文章主要介紹了一些C語(yǔ)言中字符串的算法問(wèn)題解決實(shí)例小結(jié),包括將字符串轉(zhuǎn)化為int類型的數(shù)及旋轉(zhuǎn)字符串等操作,需要的朋友可以參考下2016-03-03
C語(yǔ)言實(shí)現(xiàn)flappy bird游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)flappy bird小游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

