C語言鏈表實現學生信息管理系統程序設計
本文實例為大傢分享瞭C語言鏈表實現學生信息管理系統的具體代碼,供大傢參考,具體內容如下
事先存入的數據:
菜單
創建鏈表並倒序輸出
輸出鏈表中的全部信息
寫入信息並保存至文件中(覆蓋原有文件)
隨機讀取
指定查找
添加信息
指定刪除
特殊查找
特殊刪除
退出系統
#include<iostream> #include<string.h> #include<stdlib.h> #include<iomanip> #include<fstream> #include<time.h> int length=0; using namespace std; char a[10],b[10],c[10],d[10],e[10],f[10],g[10]; //定義學生信息的結構體類型,包括:學號、姓名、專業、班級、3門成績 typedef struct StuNode { char grad[10];//學號 char name[10];//姓名 char spec[10];//專業 char stu_class[10];//班級 int score1; int score2; int score3; struct StuNode *next; }student, *StuLink; void Sort(StuLink &head)//從小到大進行冒泡排序 { StuLink tmp,pre,p,q; if (head->next) { p = head->next->next; head->next->next = NULL; while (p) { pre = head; //pre是q的前驅 q = pre->next; while (q && strcmp(q->grad,p->grad)<0)//從鏈表第二個結點開始找比當前插入值大的結點 { pre = pre->next; q = q->next; } tmp = p->next;//將p插入到結點pre和q之間 p->next = q; pre->next = p; p = tmp; } } } student *CreateList()//初始化:創建鏈表 { void Output(StuLink &p); StuLink head = (student*)malloc(sizeof(student)); StuLink p,q; p = head; q = head; char grad[10];//學號 char name[10];//姓名 char spec[10];//專業 char stu_class[10];//班級 int score1; int score2; int score3; FILE *r= fopen("2.txt","r"); if(r==NULL) { printf("打開文件失敗!"); return NULL; } fscanf(r,"%s%s%s%s%s%s%s",&a,&b,&c,&d,&e,&f,&g);//讀取標題 while(fscanf(r,"%s%s%s%s%d%d%d",grad,name,spec,stu_class,&score1,&score2,&score3)!=EOF) { q = (student*)malloc(sizeof(student)); strcpy(q->grad,grad); strcpy(q->name,name); strcpy(q->spec,spec); strcpy(q->stu_class,stu_class); q->score1 = score1; q->score2 = score2; q->score3 = score3; p->next = q; p = q; length++; } p->next = NULL; Sort(head); //倒序輸出 StuLink k = head,t; while(k->next) k = k->next; while(k!=head) { //倒序輸出 t = head; while(t->next!=k) t = t->next;//t為k前驅 k = t; } return head; } void Output(StuLink &p)//輸出信息 { printf("%5s %-5s %-3s %s %3d %3d %3d\n",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); } void Print_List(StuLink &head)//打印整個鏈表 { StuLink p = head->next; while(p) { Output(p); p = p->next; } } void Save(StuLink &head)//寫入文件。 { StuLink p = (student*)malloc(sizeof(student)),q = head->next; char grad[10],name[10],spec[10],stu_class[10]; int score1,score2,score3; printf("請輸入學生信息:\n"); scanf("%s%s%s%s%d%d%d",p->grad,p->name,p->spec,p->stu_class,&p->score1,&p->score2,&p->score3); FILE *w =fopen("2.txt","a"); if(w==NULL) { printf("打開文件失敗!\n"); return; } else printf("寫入成功!\n"); fprintf(w,"\n%s %s %s %s %d %d %d",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); fclose(w); //存入鏈表 p->next = q; head->next = p; Sort(head); } void Fetch(StuLink &H)//隨機讀取某個學生的信息。 { StuLink p = H->next; int i = time(NULL) % length; int j = i; while(j) { p = p->next; j--; } printf("第%d名學生\n",i+1); printf(" 學號 姓名 專業 班級 成績1 成績2 成績3\n"); Output(p); } student *Search_num(StuLink &H) { //查找指定學號的學生,返回指向該學生結點的指針。 char grad[10]; printf("請輸入查詢信息的學號:"); scanf("%s",grad); StuLink p = H->next; while(p) { if(strcmp(p->grad,grad)==0) return p; p = p->next; } return NULL; } void InsertList(StuLink &H) { //在函數中輸入一個學生的信息,將該學生信息插入到鏈表中的相應位置,並保持此鏈表按學號的有序性。 StuLink p = H->next, q = H; StuLink insert = (student*)malloc(sizeof(student)); printf("請輸入學生信息:\n"); scanf("%s%s%s%s%d%d%d",insert->grad,insert->name,insert->spec,insert->stu_class,&insert->score1,&insert->score2,&insert->score3); while(p) { if(strcmp(p->grad,insert->grad) > 0 ) { q->next = insert;//應插入q和p之間 insert->next = p; break; } q = q->next;//q是p的前驅 p = p->next; } if(!p)//insert的學號大於所有已知值 { q->next = insert; insert->next = NULL; } p = H->next; printf("\n"); Print_List(H); } void Delete_num(StuLink &H)//從鏈表中刪除指定學號的學生。 { StuLink p = H->next, q = H; char grad[10]; printf("請輸入想刪除的學生的學號:\n"); scanf("%s",grad); while(p) { if(strcmp(p->grad,grad)==0) { q->next = p->next; free(p); break; } q = q->next; p = p->next; } FILE *w =fopen("2.txt","w"); p=H->next; while(p) { fprintf(w,"%5s %-5s %-3s %s %3d %3d %3d\n",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); p = p->next; } fclose (w); Print_List(H); } student *Search_major_subject_score(StuLink &H) { //查找某個專業的、某門課程的成績小於某個分數的學生,返回指向該學生結點的指針。 char spec[10]; float score; StuLink p = H->next; int lesson; printf("請輸入專業、課程序號和門限分數:\n"); scanf("%s%d%f",spec,&lesson,&score); while(p) { if(strcmp(p->spec,spec)==0) if(lesson==1 && p->score1<score || lesson==2 && p->score2<score || lesson==3 && p->score3<score) return p; p = p->next; } return NULL; } void Delete_major_subject(StuLink &H) { //從鏈表中刪除某個專業的、某門課程的成績小於某個分數的學生。 char spec[10]; float score; StuLink p = H->next, q = H; int lesson,flag = 0; printf("請輸入專業、課程序號和門限分數:\n"); scanf("%s%d%f",spec,&lesson,&score); while(p) { if(strcmp(p->spec,spec)==0 && lesson == 1 && p->score1 < score) // cs 1 94 { Output(p); q->next = p->next; free(p); p = q->next; flag = 1; } else if(strcmp(p->spec,spec)==0 && lesson == 2 && p->score2 < score) // cs 1 94 { Output(p); q->next = p->next; free(p); p = q->next; flag = 1; } else if(strcmp(p->spec,spec)==0 && lesson == 3 && p->score3 < score) // cs 1 94 { Output(p); q->next = p->next; free(p); p = q->next; flag = 1; } else { q = p; p = p->next; } } if(flag==0) printf("不存在此學生!\n"); else printf("成功刪除\n"); FILE *w =fopen("2.txt","w"); p=H->next; while(p) { fprintf(w,"%5s %-5s %-3s %s %3d %3d %3d\n",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); p = p->next; } fclose (w); Print_List(H); } void write(StuLink &H)//寫入其他文件 { StuLink p = H->next; FILE *w = fopen("other.txt","w"); fprintf(w,"%s %s %s %s %s %s %s",a,b,c,d,e,f,g); while(p) { fprintf(w,"\n%s %s %s %s %d %d %d",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); p = p->next; } fclose(w); printf("寫入成功!"); } char menu() { char ch1; printf(" \t\t\t\t 歡迎訪問學生信息登記系統! \n"); printf("\t\t\t ___________________________________________________________\n"); printf("\t\t\t\t\t 1 Creatlist 寫入信息創建鏈表並倒序輸出\n"); printf("\t\t\t\t\t 2 Output 輸出全部信息\n"); printf("\t\t\t\t\t 3 save 保存文件\n"); printf("\t\t\t\t\t 4 Fetch 隨機讀取\n"); printf("\t\t\t\t\t 5 Search num 指定查找\n"); printf("\t\t\t\t\t 6 Insertlist 添加信息 \n"); printf("\t\t\t\t\t 7 Delete num 指定刪除\n"); printf("\t\t\t\t\t 8 Search_major _subject_score 特殊查找\n"); printf("\t\t\t\t\t 9 Delete_major _subject_score 特殊刪除\n"); printf("\t\t\t\t\t 10 Exit 退出系統\n"); printf("\t\t\t ___________________________________________________________\n"); } void read(StuLink &head)//讀取文件函數 { StuLink p = (student*)malloc(sizeof(student)),q = head->next; char grad[10],name[10],spec[10],stu_class[10]; int score1,score2,score3; FILE *w =fopen("2.txt","a"); if(w==NULL) { printf("打開文件失敗!\n"); return; } else { StuLink p = head->next; while(p) { Output(p); p = p->next; } printf("輸出成功!\n"); fprintf(w,"\n%s %s %s %s %d %d %d",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); } fclose(w); //存入鏈表 } int main() { StuLink H = CreateList(),temp; int k; menu(); scanf("%d",&k); k = int(k); while(k) { k = int(k); switch(k) { case 0: break; case 1: { StuLink head = (student*)malloc(sizeof(student)); StuLink p,q; p = head; q = head; char grad[10];//學號 char name[10];//姓名 char spec[10];//專業 char stu_class[10];//班級 int score1; int score2; int score3; FILE *r= fopen("2.txt","r"); if(r==NULL) { printf("打開文件失敗!"); } fscanf(r,"%s%s%s%s%s%s%s",&a,&b,&c,&d,&e,&f,&g);//讀取標題 while(fscanf(r,"%s%s%s%s%d%d%d",grad,name,spec,stu_class,&score1,&score2,&score3)!=EOF) { q = (student*)malloc(sizeof(student)); strcpy(q->grad,grad); strcpy(q->name,name); strcpy(q->spec,spec); strcpy(q->stu_class,stu_class); q->score1 = score1; q->score2 = score2; q->score3 = score3; p->next = q; p = q; length++; } p->next = NULL; Sort(head); StuLink k = head,t; while(k->next) k = k->next; printf("倒序輸出:\n 學號 姓名 專業 班級 成績1 成績2 成績3\n"); while(k!=head) { //倒序輸出 t = head; while(t->next!=k) t = t->next;//t為k前驅 Output(k); k = t; } } menu(); break; case 2: Print_List(H); menu(); break; case 3: Save(H); menu(); break; case 4: Fetch(H); menu(); break; case 5: temp = Search_num(H); if(temp) { printf("指針為:%d\n",temp); printf(" 學號 姓名 專業 班級 成績1 成績2 成績3\n"); Output(temp); } else printf("不存在此學生!\n"); menu(); break; case 6: InsertList(H); menu(); break; case 7: Delete_num(H); menu(); break; case 8: temp = Search_major_subject_score(H); if(temp) { printf("指針為:%d\n",temp); printf(" 學號 姓名 專業 班級 成績1 成績2 成績3\n"); Output(temp); } else printf("不存在此學生!\n"); menu(); break; case 9: Delete_major_subject(H); menu(); break; case 99: read(H); menu(); break; case 10: printf("\n ~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); //退出提示 printf(" Goodbye! \n"); printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); exit(0);//將程序退出 default: printf("輸入有誤,請重新輸入!\n"); } printf("請輸入選項:"); scanf("%d",&k); } return 0; }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。