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

在main函數(shù)里調用這8個函數(shù)
學生信息包含姓名、年齡、學號、成績,需要定義一個結構體(結構體是全局變量,所以需要全局聲明):
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;
錄入學生信息:
1)創(chuàng)建一個新節(jié)點,結點里包括結構體數(shù)據(jù),和下一個指針。
2)需要判斷頭結點是不是空的,如果是空的,則此時需要將新節(jié)點付給頭結點,如果不是空的,那么該結點的下一個指針=頭結點(頭插法,能用就行)。
3)然后就輸入數(shù)據(jù)scanf("%s",&p->stu1.age);
4)最后提示一下該學生信息輸入完畢!
5)現(xiàn)在只輸入了一個學生信息,此時只需要在主函數(shù)里寫一個循環(huán)就可以無限調用該函數(shù)進行數(shù)據(jù)錄入。
void input(){
?? ?printf("請輸入學生信息\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("請輸入學號 : ");
?? ?scanf("%d",&pNewNode->stu1.stuNum);
?? ?printf("請輸入成績 : ");
?? ?scanf("%d",&pNewNode->stu1.score);
printf("該學生信息輸入完畢!\n\n");
}查看信息:
1)需要對鏈表進行遍歷然后printf;
2)新建一個結點指針然后 Node* p=g_head; while(p!=NULL){ printf p=p->next}(注:以上代碼只是功能的描述)
代碼:
void printdate(){
?? ?Node* p=g_head;
?? ?printf("\t姓名\t年齡\t學號\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ù)里的結構體里,
3)需要判斷一下是否讀取到了數(shù)據(jù),將文件里的數(shù)據(jù)先讀到結構體數(shù)組里
4)然后while循環(huán)里 ,,將結構體數(shù)組里的數(shù)據(jù)復制給鏈表,
5)需要將每一個節(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("總人數(shù)%d",a);
}查找學生:
1)定義整型數(shù)據(jù),輸入學號,先遍歷再if判斷輸入的學號和p->stu1.num是否相等,相等的話輸出:
void find(){
?? ?int num;
?? ?printf("請輸入要查找的學生學號: \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("無該學生信息");
}修改信息:
1)定義整型數(shù)據(jù),輸入學號,先遍歷再if判斷輸入的學號和p->stu1.num是否相等,相等的話重新輸入:
void change(){
?? ?int num;
?? ?printf("請輸入要修改的學生的學號: ");
?? ?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("請輸入學號: \n");
?? ??? ??? ?scanf("%d",&p->stu1.stuNum);
?? ??? ??? ?printf("請輸入成績: \n");
?? ??? ??? ?scanf("%d",&p->stu1.score);
?? ??? ??? ?printf("信息更改完畢!");
?? ??? ?}
?? ??? ?p=p->pNext;
?? ?}
?? ?if(p==NULL){
?? ??? ?printf("該學生不存在!\n");
?? ?}
}刪除信息:
1)思路是先遍歷,找到了free就可以了,
2)需要定義兩個節(jié)點指針,如果找到了是頭結點直接將頭結點付給定義的節(jié)點,然后free,
如果不是頭結點,將該節(jié)點付給定義的節(jié)點,p->next=p->next->next;
然后free
void del(){
?? ?int num;
?? ?printf("請輸入要刪除的學號");
?? ?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("學號為%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("請輸入學生信息\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("請輸入學號 : ");
?? ?scanf("%d",&pNewNode->stu1.stuNum);
?? ?printf("請輸入成績 : ");
?? ?scanf("%d",&pNewNode->stu1.score);
printf("該學生信息輸入完畢!\n\n");
}
void printdate(){
?? ?Node* p=g_head;
?? ?printf("\t姓名\t年齡\t學號\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("總人數(shù)%d",a);
}
void find(){
?? ?int num;
?? ?printf("請輸入要查找的學生學號: \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("請輸入要修改的學生的學號: ");
?? ?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("請輸入學號: \n");
?? ??? ??? ?scanf("%d",&p->stu1.stuNum);
?? ??? ??? ?printf("請輸入成績: \n");
?? ??? ??? ?scanf("%d",&p->stu1.score);
?? ??? ??? ?printf("信息更改完畢!");
?? ??? ?}
?? ??? ?p=p->pNext;
?? ?}
?? ?if(p==NULL){
?? ??? ?printf("該學生不存在!\n");
?? ?}
}
void del(){
?? ?int num;
?? ?printf("請輸入要刪除的學號");
?? ?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("學號為%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;
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++11中l(wèi)onglong超長整型和nullptr初始化空指針
本文介紹?C++11?標準中新添加的?long?long?超長整型和?nullptr?初始化空指針,在?C++11?標準下,相比?NULL?和?0,使用?nullptr?初始化空指針可以令我們編寫的程序更加健壯,本文結合示例代碼給大家詳細講解,需要的朋友跟隨小編一起看看吧2022-12-12

