最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言實(shí)現(xiàn)簡單學(xué)生信息管理系統(tǒng)

 更新時(shí)間:2022年07月25日 08:56:44   作者:自然的像植物  
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

學(xué)生信息管理系統(tǒng)的功能有,也可以自己增加或者改進(jìn)一些函數(shù)功能。

在main函數(shù)里調(diào)用這8個(gè)函數(shù)

學(xué)生信息包含姓名、年齡、學(xué)號(hào)、成績,需要定義一個(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)就可以無限調(diào)用該函數(shù)進(jìn)行數(shù)據(jù)錄入。

void input(){
?? ?printf("請輸入學(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("請輸入姓名: ");
?? ?scanf("%s",pNewNode->stu1.name);
?? ?printf("請輸入年齡: ");
?? ?scanf("%d",&pNewNode->stu1.age);
?? ?printf("請輸入學(xué)號(hào) : ");
?? ?scanf("%d",&pNewNode->stu1.stuNum);
?? ?printf("請輸入成績 : ");
?? ?scanf("%d",&pNewNode->stu1.score);
printf("該學(xué)生信息輸入完畢!\n\n");
}

查看信息:

1)需要對鏈表進(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成績\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("文件打開失敗");
?? ??? ?return;
?? ?}
?? ?Node* p=g_head;
?? ?while(p!=NULL){
?? ??? ?fwrite(&p->stu1,1,sizeof(student),fp);
?? ??? ?p=p->pNext;
?? ?}
?? ?fclose(fp);
?? ?printf("文件保存成功!\n");
}

文件讀?。?/strong>

1)先用文件指針指向一個(gè)使用的文件,
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)需要將每一個(gè)節(jié)點(diǎn)排好隊(duì)列,這里用頭插法,每復(fù)制一個(gè)就會(huì)開辟一個(gè)結(jié)點(diǎ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;

?? ??? ?}
?? ?}
?? ?}

統(tǒng)計(jì)人數(shù):

1)遍歷鏈表的時(shí)候定義一個(gè)整型數(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é)號(hào),先遍歷再if判斷輸入的學(xué)號(hào)和p->stu1.num是否相等,相等的話輸出:

void find(){
?? ?int num;
?? ?printf("請輸入要查找的學(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("無該學(xué)生信息");
}

修改信息:

1)定義整型數(shù)據(jù),輸入學(xué)號(hào),先遍歷再if判斷輸入的學(xué)號(hào)和p->stu1.num是否相等,相等的話重新輸入:

void change(){
?? ?int num;
?? ?printf("請輸入要修改的學(xué)生的學(xué)號(hào): ");
?? ?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é)號(hào): \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)需要定義兩個(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("請輸入要?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)叉,請按0退出!\n查看文件之前請先讀取文件!\n");
}
void input(){
?? ?printf("請輸入學(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("請輸入姓名: ");
?? ?scanf("%s",pNewNode->stu1.name);
?? ?printf("請輸入年齡: ");
?? ?scanf("%d",&pNewNode->stu1.age);
?? ?printf("請輸入學(xué)號(hào) : ");
?? ?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é)號(hào)\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é)號(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("請輸入要修改的學(xué)生的學(xué)號(hào): ");
?? ?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é)號(hào): \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("請輸入要?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;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

兰坪| 小金县| 喜德县| 保靖县| 永吉县| 新竹市| 新邵县| 泌阳县| 凤台县| 苍梧县| 日喀则市| 桐梓县| 安阳县| 宜章县| 东海县| 怀集县| 丹阳市| 田阳县| 福海县| 岢岚县| 长寿区| 景泰县| 阜新市| 岳普湖县| 榆林市| 宕昌县| 安徽省| 加查县| 浙江省| 长寿区| 承德市| 比如县| 敦化市| 松桃| 杭锦后旗| 江津市| 阿克陶县| 庆安县| 曲阳县| 太保市| 达州市|