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