本文實例為大家分享了C語言實現學生選課系統的具體代碼,供大家參考,具體內容如下
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <string.h> typedef struct curr{char name[20]; //課程姓名 int number; //課程序號 char teacher[20]; //課程教師姓名 int time; //課程課時 int classroom; //課程教室 struct curr *next; //鏈表next }curr,*pcurr;typedef struct stu{int number; //學生序號 char name[20]; //學生姓名 char sex[20]; //學生性別 struct curr *choices; //學生選課 struct stu *next; //鏈表next }stu,*pstu;pcurr creat_curr(pcurr curr_head); //創建鏈表,課程信息 pcurr find_curr(pcurr curr_head,int number); //查找鏈表, 課程信息 pcurr add_curr(pcurr curr_head,pcurr new_curr); //課程信息加入鏈表 void out_curr(pcurr curr_head); //輸出課程信息 pcurr del_curr(pcurr curr_head,pcurr del_point); //刪除課程信息 pstu creat_stu(pstu stu_head); //創建鏈表,學生信息 pstu find_stu(pstu stu_head,int number); //查找鏈表, 學生信息 pstu add_stu(pstu stu_head,pstu new_stu); //學生信息,加入鏈表void out_stu(pstu stu_head); //輸出學生信息 pstu del_stu(pstu stu_head,pstu del_point); //刪除學生信息pstu choice_curr(pstu stu_head,int number1,pcurr curr_head); //學生選課信息 void out_choice(pstu stu_head); //輸出學生選課信息 void title(){printf("/t---------歡迎使用成都信息工程大學--學生選課系統------------/n");printf("/t--------- ------------/n");printf("/t--------- 1.輸入課程信息 ------------/n");printf("/t--------- 2.瀏覽課程信息 ------------/n");printf("/t--------- 3.刪除課程信息 ------------/n");printf("/t--------- 4.輸入學生信息 ------------/n");printf("/t--------- 5.瀏覽學生信息 ------------/n");printf("/t--------- 6.刪除學生信息 ------------/n");printf("/t--------- 7.學生選課 ------------/n");printf("/t--------- 8.所有學生選課信息 ------------/n");printf("/t--------- 9.退出程序 ------------/n");printf("/t--------- ------------/n");printf("/t--------- 特別鳴謝:網絡工程專業劉尚文同學 ------------/n");}int main(){int a,n=0,i;int number=0;int number1=0;pcurr curr_head=NULL;pstu stu_head=NULL;pcurr new_curr=NULL;pstu new_stu=NULL;pcurr del_point=NULL;char choice[20];do{system("cls");title();printf("請在1-9中選擇:");scanf("%d",&a);switch(a){case 1: system("cls");new_curr=creat_curr(curr_head);curr_head=add_curr(curr_head,new_curr);break;case 2:system("cls");printf("/t/t--------- 全部課程信息 ------------/n");printf("/t課程姓名/t課程序號/t課程教師姓名/t課程課時/t課程教室/n");out_curr(curr_head);system("pause"); break;case 3:system("cls");printf("/t課程姓名/t課程序號/t課程教師姓名/t課程課時/t課程教室/n");out_curr(curr_head); printf("請輸入打算刪除的課程的序號:/n");scanf("%d",&number);curr_head=del_curr(curr_head,find_curr(curr_head,number));printf("刪除成功!/n");system("pause");break;case 4:system("cls");new_stu=creat_stu(stu_head);stu_head=add_stu(stu_head,new_stu);break;case 5:system("cls");printf("/t/t--------- 全部學生信息 ------------/n");printf("/t學生姓名/t學生學號/t學生性別/n");out_stu(stu_head);system("pause"); break;case 6:system("cls");printf("請輸入打算刪除的學生的學號:/n");scanf("%d",&number);stu_head=del_stu(stu_head,find_stu(stu_head,number));printf("刪除成功!/n");system("pause");break;case 7:system("cls");printf("/t學生姓名/t學生學號/t學生性別/n");out_stu(stu_head);printf("請輸入選課同學學號:");scanf("%d",&number1);choice_curr(stu_head,number1,curr_head);system("pause");break;;case 8:system("cls");printf("/t/t/t/t--------- 全部選課信息 ------------/n");printf("/n");printf("/t學生姓名/t學生學號/t學生性別/t課程姓名/t課程序號/t課程教師姓名/t課程課時/t課程教室/n");out_choice(stu_head);system("pause"); break;case 9:return 0;break;}} while(a!=0);return 0;}//創建鏈表,課程信息 pcurr creat_curr(pcurr curr_head){pcurr new_curr=(pcurr)malloc(sizeof(curr));printf("/n");printf("/t/t--------- 輸入課程信息 ------------/n");printf("/n");printf("請輸入課程姓名:");scanf("%s",new_curr->name);printf("請輸入課程序號:");scanf("%d",&new_curr->number);printf("請輸入課程教師姓名:");scanf("%s",new_curr->teacher);printf("請輸入課程課時:");scanf("%d",&new_curr->time);printf("請輸入課程教室:");scanf("%d",&new_curr->classroom);while (find_curr(curr_head,new_curr->number)!=NULL){printf("此序號已經有數據,請重新輸入.");scanf("%d",&new_curr->number);}new_curr->next=NULL;return new_curr;}//查找鏈表 pcurr find_curr(pcurr curr_head,int number){if(curr_head==NULL)return NULL;if(curr_head->number==number)return curr_head;return find_curr(curr_head->next, number); }//課程信息加入鏈表 pcurr add_curr(pcurr curr_head,pcurr new_curr){if(curr_head==NULL)return new_curr;new_curr->next=curr_head;return new_curr;}//輸出課程信息 void out_curr(pcurr curr_head){while(curr_head){printf("/t%s/t/t%d/t/t%s/t/t%d/t/t%d/n",curr_head->name,curr_head->number,curr_head->teacher,curr_head->time,curr_head->classroom);curr_head=curr_head->next;}}//刪除課程信息 pcurr del_curr(pcurr curr_head,pcurr del_point){pcurr point;if(del_point == NULL){printf("沒有此序號信息,請重新輸入!/n");return curr_head;}point=NULL;if(del_point == curr_head ){point=curr_head->next;free(curr_head);return point;}point=curr_head;while(point){if(point->next == del_point){point->next=del_point->next;free(del_point);return curr_head;}point = point->next;}} //創建鏈表,學生信息 pstu creat_stu(pstu stu_head){pstu new_stu=(pstu)malloc(sizeof(stu));printf("/n");printf("/t/t--------- 輸入學生信息 ------------/n");printf("/n");printf("請輸入學生姓名:");scanf("%s",new_stu->name);printf("請輸入學生學號:");scanf("%d",&new_stu->number);printf("請輸入學生性別:");scanf("%s",new_stu->sex);while (find_stu(stu_head,new_stu->number)!=NULL){printf("此學號已經有數據,請重新輸入.");scanf("%d",&new_stu->number);}new_stu->choices=NULL;new_stu->next=NULL;return new_stu;}//查找鏈表 pstu find_stu(pstu stu_head,int number){if(stu_head==NULL)return NULL;if(stu_head->number==number)return stu_head;return find_stu(stu_head->next, number); }//學生信息加入鏈表 pstu add_stu(pstu stu_head,pstu new_stu){if(stu_head==NULL)return new_stu;new_stu->next=stu_head;return new_stu;}//輸出學生信息 void out_stu(pstu stu_head){while(stu_head){printf("/t%s/t/t%d/t/t%s/n",stu_head->name,stu_head->number,stu_head->sex);stu_head=stu_head->next;}}//刪除學生信息 pstu del_stu(pstu stu_head,pstu del_point){pstu point;if(del_point == NULL){printf("沒有此學號信息,請重新輸入!/n");return stu_head;}point=NULL;if(del_point == stu_head ){point=stu_head->next;free(stu_head);return point;}point=stu_head;while(point){if(point->next == del_point){point->next=del_point->next;free(del_point);return stu_head;}point = point->next;}}//學生選課 pstu choice_curr(pstu stu_head,int number1,pcurr curr_head){int number;pcurr point=NULL,point1=NULL;pcurr choice_point=NULL;pstu stu_point=find_stu(stu_head,number1);if(stu_point!=NULL){printf("/t課程姓名/t課程序號/t課程教師姓名/t課程課時/t課程教室/n");out_curr(curr_head);printf("請輸入所選課程學號:");scanf("%d",&number);point=find_curr(curr_head,number);if(point!=NULL){choice_point=(pcurr)malloc(sizeof(curr));memcpy(choice_point,point,sizeof(curr));choice_point->next=NULL;if(stu_point->choices==NULL){stu_point->choices=choice_point;}else {choice_point->next=stu_point->choices;stu_point->choices=choice_point;}printf("恭喜你!選課成功!/n");return stu_head;}else{printf("沒有所選課程序號!");return stu_head;}}else{printf("沒有所選學生學號!");return stu_head;}}//輸出學生選課信息 void out_choice(pstu stu_head){pcurr point=NULL;while(stu_head){point=stu_head->choices;printf("/t%s/t/t%d/t/t%s/n",stu_head->name,stu_head->number,stu_head->sex);while(point){printf("/t/t/t/t/t/t/t%s/t/t%d/t/t%s/t/t%d/t/t%d/n",point->name,point->number,point->teacher,point->time,point->classroom);point=point->next;}stu_head=stu_head->next;}}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選