題目:學生信息管理系統--(鏈表)數據結構
做者:姜瑩,汪凱雙,信宇ide
主要功能:考慮到學生成績管理系統一經創建不只進行查詢並且須要頻繁的進行插入和刪除,運用數據結構中的單鏈表結構對學生信息進行存儲。簡單實現了對學生信息的增,刪,改,查,排序,對學生信息的文件操做(保存,讀取)。spa
#include <stdio.h> #include<string.h> #include<stdlib.h> using namespace std; typedef struct student //學生信息存儲結構 { char no[11]; //學號 char name[15]; //姓名 int score[3]; //成績 int sum; //總分 float average; //平均分 int order; //按成績排序 struct student *next; } student; int Menu_select(); //菜單 struct student *Create(student *head); //創建 void Print(student *head); //打印 struct student *Compute(student *head); //計算 struct student *Append(student *head); //追加 struct student *Del(student *head); //刪除 struct student *Insert(student *head); //插入 struct student *change(student *head); //修改 void Search(student *head); //查找 void Save(student *head); //保存文件 struct student *Load(student *head); //讀取文件 struct student *Sort(student *head); int main() { student *head=NULL; while(1) { switch(Menu_select()) { case 1: head=Create(head); break; case 2: Print(head); break; case 3: head=Compute(head); break; case 4: head=Append(head); break; case 5: head=Del(head); break; case 6: head=Insert(head); break; case 7: Search(head); break; case 8: head=change(head); break; case 9: Save(head); break; case 10: head=Load(head); break; case 11: head=Sort(head); break; case 12: exit(0); } } return 0; } /*****************************菜單****************************/ int Menu_select() { int choice; printf("***************************菜單***************\n"); printf(" 1 輸入 \n"); printf(" 2 打印 \n"); printf(" 3 計算 \n"); printf(" 4 追加 \n"); printf(" 5 刪除 \n"); printf(" 6 插入 \n"); printf(" 7 查找 \n"); printf(" 8 修改 \n"); printf(" 9 保存 \n"); printf(" 10 讀入 \n"); printf(" 11 排序 \n"); printf(" 12 退出 \n"); printf("Enter your choice(0-11):"); scanf("%d",&choice); return choice; } /*****************************創建鏈表************************/ struct student *Create(student *head) //ok { student *p,*q;; head=(student *)malloc(sizeof(student)); head->next=NULL; q=head; while(1) { p=(student*)malloc(sizeof(student)); printf("請輸入學號:"); scanf("%s",p->no); if(p->no[0]=='@') { free(p); break; } printf("請輸入姓名:"); scanf("%s",p->name); printf("請輸入數學成績:"); scanf("%d",&p->score[0]); printf("請輸入英語成績:"); scanf("%d",&p->score[1]); printf("請輸入數據結構成績:"); scanf("%d",&p->score[2]); p->sum=0; p->average=0; p->order=0; q->next=p; q=p; } q->next=NULL; return head; } /*****************************打印****************************/ void Print(student *head) //ok { student *p=head; printf("學號 姓名 數學 英語 數據結構 總分 平均分 排名\n"); while(p->next) { p=p->next; printf("%s %s %d %d %d %d %.2f %d \n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); } } /*****************************計算****************************/ struct student *Compute(student *head)//OK { student *p=head->next; while(p) { p->sum=p->score[0]+p->score[1]+p->score[2]; p->average=p->sum/3.0; p=p->next; } return head; } /*****************************追加****************************/ struct student *Append(student *head)//OK { student *p,*q=head,*t; p=(student*)malloc(sizeof(student)); printf("請輸入要增長的學生信息:\n"); printf("請輸入學號:"); scanf("%s",p->no); printf("請輸入姓名:"); scanf("%s",p->name); printf("請輸入數學成績:"); scanf("%d",&p->score[0]); printf("請輸入英語成績:"); scanf("%d",&p->score[1]); printf("請輸入數據結構成績:"); scanf("%d",&p->score[2]); p->sum=0; p->average=0; p->order=0; while(q) { t=q; q=q->next; } t->next=p; t=p; t->next=NULL; return head; } /*****************************刪除****************************/ struct student *Del(student *head) //OK { char number[11]; student *p=head,*q; printf("輸入要刪除的學生學號:"); scanf("%s",number); while(p->next&&strcmp(number,p->no)) { q=p; p=p->next; } q->next=p->next; return head; } /*****************************插入****************************/ struct student *Insert(student *head) //ok { char no[11]; student *p,*q=head,*t; printf("請輸入要插入的學生位置:"); scanf("%s",no); p=(student *)malloc(sizeof(student)); printf("請輸入要增長的學生信息:\n"); printf("請輸入學號:"); scanf("%s",p->no); printf("請輸入姓名:"); scanf("%s",p->name); printf("請輸入數學成績:"); scanf("%d",&p->score[0]); printf("請輸入英語成績:"); scanf("%d",&p->score[1]); printf("請輸入數據結構成績:"); scanf("%d",&p->score[2]); p->sum=0; p->average=0; p->order=0; while(q->next&&strcmp(q->next->no,no)) { q=q->next; t=q; } p->next=t->next; t->next=p; return head; } /****************************修改*****************************/ struct student *change(student *head) //ok { char no[11]; student *q=head->next; printf("請輸入要修改的學生學號:"); scanf("%s",no); while(q&&strcmp(q->no,no)) { q=q->next; } printf("請輸入學生學號:"); scanf("%s",q->no); printf("請輸入學生姓名:"); scanf("%s",q->name); printf("請輸入學生數學成績:"); scanf("%d",&q->score[0]); printf("請輸入學生英語成績:"); scanf("%d",&q->score[1]); printf("請輸入學生數據結構成績:"); scanf("%d",&q->score[2]); return head; }; /****************************查找*****************************/ void Search(student *head) //ok { char no[15]; student *p=head->next; printf("請輸入要查詢的學生學號:"); scanf("%s",no); while(p&&strcmp(no,p->no)) p=p->next; printf("學號 姓名 數學 英語 數據結構 總分 平均分 排名\n"); printf("%s %s %d %d %d %d %.2f %d \n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); } /****************************文件保存*************************/ void Save(student *head) //ok { FILE *fp; if((fp=fopen("student.txt","w"))==NULL) { printf("文件打開失敗\n"); getchar(); exit(1); } student *p=head->next; while(p) { fprintf(fp,"%s %s %d %d %d %d %f %d\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); p=p->next; } fclose(fp); } /****************************文件讀取*************************/ struct student *Load(student *head) //ok { head=(student*)malloc(sizeof(student)); head->next=NULL; FILE *fp; if((fp=fopen("student.txt","r"))==NULL) { printf("文件打開失敗\n"); getchar(); exit(1); } student *p=(student*)malloc(sizeof(student)); student *t; head->next=p; while(fscanf(fp,"%s%s%d%d%d%d%f%d",p->no,p->name,&p->score[0],&p->score[1],&p->score[2],&p->sum,&p->average,&p->order)==8) { t=p; p=(student*)malloc(sizeof(student)); t->next=p; } free(p); t->next=NULL; fclose(fp); return head; } /****************************排序*****************************/ struct student *Sort(student *head) { int i=1; student *q, *s, *pre ,*p,*a; p=head->next; q=p->next; p->next=NULL; while(q) { s=q; q=q->next; pre=head; p=head->next; while(p!=NULL && p->sum > s->sum) { pre=p; p=p->next; } s->next=p; pre->next=s; } a=head->next; while(a) { a->order=i++; a=a->next; } return head; }