本文實例為大家分享了C語言實現學生選課管理系統的具體代碼,供大家參考,具體內容如下
這是我們大一C語言課程設計的最終作品,涉及文件讀寫操作、鏈表的一系列操作。
源代碼由頭文件all.h、主函數文件main.cpp以及功能函數文件student.cpp、manager.cpp、common.cpp組成。
涉及的文件讀寫操作需要先手動創建文件,文件路徑可以在all.h的宏定義中更改
使用vs2017的c++編譯器,兼容c語言,現貼上源代碼和運行截圖,供感興趣的童鞋參考,水平有限還望多多包涵。
運行截圖
all.h
#pragma once#define MAXN 50#define STU_F "C://stu_cour_info_system//date//student.txt" //文件存放路徑及文件名#define COUR_F "C://stu_cour_info_system//date//course.txt" //文件存放路徑及文件名#define MAN_F "C://stu_cour_info_system//date//manager.txt" //文件存放路徑及文件名 #define STU_F_BACKUP "C://stu_cour_info_system//backup//student.txt" //備份文件存放路徑及文件名#define COUR_F_BACKUP "C://stu_cour_info_system//backup//course.txt" //備份文件存放路徑及文件名#define MAN_F_BACKUP "C://stu_cour_info_system//backup//manager.txt" //備份文件存放路徑及文件名 struct student //學生信息結構體{ char name[MAXN]; //姓名 char num[MAXN]; //學號 char sch_cla[20]; //學院和班級 double score_all = 0; //應選總學分 double score_sel = 0; //已選課總學分 char password[9]; //學生登陸密碼,共8位 int course_sum; //選課總數 char course[50][MAXN]; //記錄選課課程的編號 struct student *next; //鏈表指針}; struct course //課程信息結構體{ char num[MAXN]; //課程編號 char name[MAXN]; //課程名稱 char nature[MAXN]; //課程性質 char term[MAXN]; //開課學期 double time_all; //總學時 double time_teach; //授課學時 double time_exp; //實驗或上機學時 double score; //該課程學分 int stu_max; //能夠容納的學生總數 int stu_sum; //已經選課學生總數 char stu[100][MAXN]; //已經選課學生學號 struct course *next; //鏈表指針}; /*通用函數*/int judge_num(char *ch); //用于檢測輸入的字符串ch是否全部為數字,全部為數字則返回1,否則返回0int input_num(); //用于輸入數字,如果輸入的全部為數字則返回該數字,否則一直停在輸入狀態double input_double(); //用于輸入小數int input_limit(int n); //輸入選項,控制輸入的數字范圍為0-n,在這個范圍內則返回該數字,否則一直停在輸入狀態int input_limit0(int n);int keyexam(char *actual_key, char *input_key); //密碼檢測函數,x為密碼位數,actul_key[]為真實密碼,input_key為輸入密碼,密碼匹配返回1否則返回0student* load_stu(); //將學生文件生成鏈表,返回鏈表頭指針void store_stu(student *p_head); //將學生鏈表存入學生文件,需傳入鏈表頭指針course* load_cour(); //將課程文件生成鏈表,返回鏈表頭指針void store_cour(course *p_head); //將課程鏈表存入課程文件,需傳入鏈表頭指針 student* locate_end(student *head); //傳入頭節點,返回尾節點course* locate_end(course *head); //傳入頭節點,返回尾節點void help(); //顯示幫助信息int link_count(student* head);int link_count(course* head); //計算鏈表節點數量int judge_ascii_num(char in[]); //檢測輸入是否全部為ASCII的數字,是返回1否則返回0 /*管理員函數*/int man_login(); //管理員登陸函數,密碼正確返回1,否則返回0int man_menu(); //管理員主菜單,返回對應的輸入值void student_add(student *head); //增加學生的函數void student_delete(student *head); //刪除學生的函數void man_search_stu(student *head); //搜索學生的函數void student_modify(student *head); //管理員修改學生信息void course_add(course *head); //增加課程的函數void course_delete(course *head); //刪除課程的函數void course_modify(course *head); //修改課程信息的函數void print_all(); //打印所有學生和課程的函數void man_modifyKey(); //管理員修改密碼void man_backups_recover(); //管理員備份和恢復數據void courseshowone_man(course *ad); //管理員顯示一門課程信息void courseshowall_man(course *ad); //管理員顯示所有課程信息 /*學生函數*/student* stu_login(student* head); //學生登陸函數,學號和密碼都正確則返回鏈表中該學生的指針,錯誤則返回nullint stu_menu(); //學生菜單,返回對應的輸入值void student_showone(student *p); //顯示一個學生的信息void student_showall(student *head); //顯示所有學生的信息student *studentnamefind_char(student *head, char tar[]); //根據學生姓名查找學生,如果學生為頭節點則返回空指針,不為頭節點則返回前一個節點的指針,不存在則返回尾節點student *studentnamefind_num(student *head, char tar[]); //根據學生學號查找學生,如果學生為頭節點則返回空指針,不為頭節點則返回前一個節點的指針,不存在則返回尾節點void stu_modifyKey(student* stu); //學生修改密碼void coursechoose(course* head, student* stu); //選課函數,min為最少要達到的學分void courseshow_nature(course *head); //根據課程性質查找并顯示課程信息void courseshow_term(course *head); //根據開課學期查找并顯示課程信息void cour_nat_te(course* head); //根據課程性質與開課學期組合查找課程信息void coursenumfindtip(course *head); //查課子函數void coursenamefindtip(course *head); //查課子函數2void stu_dele_cour(course* head, student* stu); //刪除已選課程 /*課程函數*/void course_showone(course *ad); //顯示一門的信息void course_showall(course *head); //顯示所有課程的信息void search_course(course *head); //搜索課程的函數course *coursenamefind_char(course* head, char tar[]); //根據課程名稱查找課程節點,如果課程為頭節點則返回空指針,不為頭節點則返回前一個節點的指針,不存在則返回尾節點course *coursenumfind_num(course* head, char tar[]); //根據課程編號查找課程,如果課程為頭節點則返回空指針,不為頭節點則返回前一個節點的指針,不存在則返回尾節點
main.cpp
#include<stdio.h>#include<string.h>#include<stdlib.h>#include"all.h" student* stu_head;course* cour_head;course *cour_tail; student *stu_tail; //這四個指針是全局變量 int main(){ /*對四個指針變量初始化*/ stu_head = load_stu(); //stu_head指向學生鏈表頭 stu_tail = locate_end(stu_head); //stu_tail指向學生鏈表尾 cour_head = load_cour(); //cour_head指向課程鏈表頭 cour_tail = locate_end(cour_head); //cour_tail指向課程鏈表尾 //{/*這段代碼用于生成原始管理員密碼:10個0; // 編譯鏈接后執行一次然后注釋掉*/ // FILE *fp = fopen(MAN_F, "w+"); // for (int i = 0; i < 10; i++) // { // fputc('0', fp); // } // fclose(fp); //} printf("/n/t/t--------------------------------歡迎使用選課系統!---------------------------------/n"); int status=-1; while (1)//判斷登陸類型 { status = -1; printf("/n/n/t/t*****請選擇登陸類型 [1]學生 [2]管理員 [3]顯示幫助 [0]退出系統:"); status = input_limit0(3); if ((status == 0) || (status == 1) || (status == 2)||(status==3)) break; else printf("/n/t/t##### 輸入錯誤!只能選擇1、2或0 #######/n"); } while (1) { if (status == 2)//管理員登陸 { if (man_login() == 1) { printf("/n/n/t/t-----------------------------------管理員登陸成功!--------------------------------/n/n"); int menu_return = man_menu(); while (1) { switch (menu_return) { case 1: student_add(stu_head); break; case 2: student_delete(stu_head); break; case 3: student_modify(stu_head); break; case 4: man_search_stu(stu_head); break; case 5: course_add(cour_head); break; case 6: course_delete(cour_head); break; case 7: course_modify(cour_head); break; case 8: search_course(cour_head); break; case 9: print_all(); break; case 10: man_modifyKey(); break; case 11: man_backups_recover(); break; case 0: break; default: printf("/t/t/t#####輸入錯誤,請重新輸入######!/n"); } if (menu_return == 0) break; menu_return = man_menu(); } } else printf("/t/t######密碼錯誤,請重新登陸!######/n"); } else if (status == 1)//student login { student* p_stu = stu_login(stu_head); if (p_stu != NULL)//p_stu為指向該學生的指針 { printf("/t/t*******請輸入登陸密碼:"); char input[100]; scanf("%s", input); while (getchar() != '/n'); if (strcmp(p_stu->password, input) == 0) { printf("/n/n/t/t-----------------------------%s 歡迎進入選課系統! ---------------------------------/n/n", p_stu->name); int menu_return = stu_menu(); while (1) { switch (menu_return) { case 1: course_showall(cour_head); break; case 2: search_course(cour_head); break; case 3: coursechoose(cour_head,p_stu); break; case 4: stu_dele_cour(cour_head, p_stu); break; case 5: student_showone(p_stu); break; case 6: stu_modifyKey(p_stu); break; case 0: break; default: printf("/t/t######輸入錯誤,請重新輸入!######/n"); } if (menu_return == 0) break; menu_return = stu_menu(); } } else { printf("/t/t#####密碼錯誤,請重新登陸!#######/n"); } } else printf("/t/t#####該學號不存在,請重新登陸!######/n"); p_stu = NULL; } else if (status == 3)//顯示幫助信息 help(); else if (status == 0)//關閉系統 return 0; while (1)//再次判斷登陸類型 { status = -1; printf("/n/n/t/t*****請選擇登陸類型 [1]學生 [2]管理員 [3]顯示幫助 [0]退出系統:"); status = input_limit0(3); if ((status == 0) || (status == 1) || (status == 2) || (status == 3)) break; else printf("/n/t/t##### 輸入錯誤!只能選擇1、2或0 #######/n"); } }}
common.cpp
#include<stdio.h>#include<string.h>#include"all.h"#include<stdlib.h>#include<malloc.h>#include<ctype.h> extern student* stu_head;extern course* cour_head;extern course *cour_tail;extern student *stu_tail; // 這四個指針變量在main.cpp中已經聲明為全局變量 int keyexam(char *actual_key, char *input_key)//密碼正確則返回1,否則返回0{ if (strcmp(actual_key, input_key) == 0) return 1; else return 0;} student* load_stu()//經調試已經正確{ int count = 0; FILE *fp; if ((fp = fopen(STU_F, "r")) == NULL) { printf("/t/tcannot open the file:STU_F/n"); exit(0); } student* temp = (student*)malloc(sizeof(student)); while (!feof(fp)) { if (fread(temp, sizeof(student), 1, fp)) count++; } free(temp); temp = NULL; if (count == 0) return NULL; else { rewind(fp); student *p_head = NULL;//文件中有鏈表信息,則創建一個頭指針 p_head = (student*)malloc(sizeof(student)); fread(p_head, sizeof(student), 1, fp);//用文件內容初始化鏈表節點 p_head->next = NULL; count--; student* p_new = p_head; student* p_end = p_head; for (int i = 0; i < count; i++) { p_new = (student*)malloc(sizeof(student)); fread(p_new, sizeof(student), 1, fp); p_new->next = NULL; p_end->next = p_new; p_end = p_new; } fclose(fp); return p_head; }} void store_stu(student * p_head)//經調試已正確{ FILE *fp; if ((fp = fopen(STU_F, "w")) == NULL)//以只寫方式打開文件,覆蓋以前數據 { printf("/t***STU_F missed,please quit the system and check for that!/n/n"); exit(0); } while (p_head != NULL)//將鏈表所有節點寫入緩沖區 { fwrite(p_head, sizeof(student), 1, fp);//將鏈表一個節點寫入緩沖區 p_head = p_head->next; //p_head指向下一個節點 } fclose(fp);//保存文件,清空緩沖區} course * load_cour(){ int count = 0; FILE *fp; if ((fp = fopen(COUR_F, "r")) == NULL) { printf("/t/tcannot open the file:COUR_F/n"); exit(0); } course* temp = (course*)malloc(sizeof(course)); while (!feof(fp)) { if (fread(temp, sizeof(course), 1, fp)) count++; } free(temp); temp = NULL; if (count == 0) return NULL; else { rewind(fp); course *p_head = NULL;//文件中有鏈表信息,則創建一個頭指針 p_head = (course*)malloc(sizeof(course)); fread(p_head, sizeof(course), 1, fp);//用文件內容初始化鏈表節點 p_head->next = NULL; count--; course* p_new = p_head; course* p_end = p_head; for (int i = 0; i < count; i++) { p_new = (course*)malloc(sizeof(course)); fread(p_new, sizeof(course), 1, fp); p_new->next = NULL; p_end->next = p_new; p_end = p_new; } fclose(fp); return p_head; } } void store_cour(course * p_head){ FILE *fp; if ((fp = fopen(COUR_F, "w")) == NULL)//以只寫方式打開文件,覆蓋以前數據 { printf("/t***COUR_F missed,please quit the system and check for that!/n/n"); exit(0); } while (p_head != NULL)//將鏈表所有節點寫入緩沖區 { fwrite(p_head, sizeof(course), 1, fp);//將鏈表一個節點寫入緩沖區 p_head = p_head->next; //p_head指向下一個節點 } fclose(fp);//保存文件,清空緩沖區} student * locate_end(student * head){ student* end = head; while (head != NULL) { end = head; head = head->next; } return end;} course* locate_end(course * head){ course* end = head; while (head != NULL) { end = head; head = head->next; } return end;} void help(){ int input = -1; while (1) { printf("/n/t/t*****請選擇需要的幫助類型 [1]學生幫助 [2]管理員幫助 [3]版權信息 [0]返回主頁:"); scanf("%d", &input); while (getchar() != '/n'); if (input == 0) break; else if (input == 1)//學生使用說明 { printf("/n/t/t--------------------------------學生使用說明---------------------------------------/n"); printf("/t/t密碼:初始密碼為學號,忘記密碼請聯系管理員/n"); printf("/t/t/n"); printf("/t/t/n"); printf("/t/t/n"); printf("/t/t-----------------------------------------------------------------------------------/n/n"); } else if (input == 2)//管理員使用說明 { printf("/n/t/t-------------------------------管理員使用說明---------------------------------------/n"); printf("/t/t密碼:初始密碼為10個0,首次使用務必修改密碼!/n"); printf("/t/t警告:該軟件的文件夾必須放在C盤根目錄下,否則不能使用!/n"); printf("/t/t提示:請定時備份數據,防止數據丟失!/n"); printf("/t/t提示:學生學號和課程編號只能是數字!/n"); printf("/t/t------------------------------------------------------------------------------------/n/n"); } else if (input == 3)//版權信息 { printf("/n/t/t------------------------------版權信息-------------------------------------------------/n"); printf("/t/t軟件名稱:學生及課程信息管理系統/n"); printf("/t/t開發人員:焦智洋、文詩波、韓旭/n"); printf("/t/t版本:beta1.0/n"); printf("/t/t版權保護:版權所有,翻版必究/n"); printf("/t/t---------------------------------------------------------------------------------------/n/n"); } else printf("/n/t/t#####輸入錯誤,請重新輸入!#####/n"); }} int link_count(student * head){ int count = 0; while (head != NULL) { count++; head = head->next; } return count;} int link_count(course * head){ int count = 0; while (head != NULL) { count++; head = head->next; } return count;} int judge_ascii_num(char in[]){ int i = 0; while (in[i]!='/0') { if ((in[i] < '0') || (in[i] > '9')) return 0; i++; } return 1;} int judge_num(char *ch){ int sum = 0, len = strlen(ch); for (int i = 0; i < len; i++) { if (ch[i] >= '0'&&ch[i] <= '9') sum++; } if (sum == len) return 1; else return 0;} int input_num(){ int num; char buf[MAXN]; while ((scanf("%s", buf) == 0) || (judge_num(buf) == 0)) { while (getchar() != '/n'); printf("/n/t/t###輸入錯誤,請重新輸入:"); } num = atoi(buf); return num;} double input_double(){ double temp = -1.00; while (1) { scanf("%lf", &temp); while (getchar() != '/n'); if (temp < 0) printf("/n/t/t#####輸入錯誤,請重新輸入:"); else break; } return temp;} double judge_input_dou(){ printf("/n/t/t是否修改該項?[1]修改 [0]不修改:"); int judge = 0; scanf("%d", &judge); while (getchar() != '/n'); if (judge == 1) { while (1) { printf("/n/t/t請輸入修改后的內容:"); double in = 0.00; scanf("%lf", &in); while (getchar() != '/n'); if (in<=0) printf("/n/t/t#####輸入錯誤,請重新輸入!######"); else { return in; break; } } } else return -1.0;} course *coursenamefind_char(course* head, char tar[])//根據課程名稱查找課程節點,返回前一節點地址{ course *p, *q; p = head; q = NULL; while (p != NULL) { if (strcmp(tar, p->name) == 0) return q; q = p; p = p->next; } return q;} course *coursenumfind_num(course* head, char tar[])//根據課程編號查找課程{course *p, *q; p = head; q = NULL; while (p != NULL) { if (strcmp(tar, p->num)==0) return q; q = p; p = p->next; } return q; } student *studentnamefind_char(student *head, char tar[]){ student *p, *q; p = head; q = NULL; while (p != NULL) { if (strcmp(tar, p->name) == 0) return q; q = p; p = p->next; } return q;} student* studentnamefind_num(student *head, char tar[]){ student *p, *q; p = head; q = NULL; while (p != NULL) { if (strcmp(tar, p->num) == 0) return q; q = p; p = p->next; } return q;} int input_limit(int n){ int m; while (1) { m = input_num(); if (m > 0 && m <= n) break; printf("/n/t/t####無此選項,請重新輸入!####/n"); } return m;} int input_limit0(int n){ int m; while (1) { m = input_num(); if (m >= 0 && m <= n) break; printf("/n/t/t####無此選項,請重新輸入!####/n"); } return m;} void student_showone(student *p){ printf("/n/t/t------------------------------------------------------"); printf("/n/t/t學號:%s", p->num); printf("/n/t/t姓名:%s", p->name); printf("/n/t/t學院與班級:%s", p->sch_cla); printf("/n/t/t密碼:%s", p->password); if (p->course_sum == 0) printf("/n/t/t該學生無已選課程!/n"); else { printf("/n/t/t已選課程:/n"); for (int i = 0; i < p->course_sum; i++) printf("/t/t/t[%d]:%s/n", i + 1, p->course[i]); } printf("/t/t選課總數:%d", p->course_sum); printf("/n/t/t已選課程總學分:%lf", p->score_sel); printf("/n/t/t已得學分統計:%lf", p->score_all); printf("/n/t/t------------------------------------------------------");} void student_showall(student *head){ student *p; p = head; while (p != NULL) { student_showone(p); p = p->next; }} void course_showone(course *ad){ if (ad == NULL) printf("/t/t無課程/n"); else { printf("/n/t/t------------------------------------------------------"); printf("/n/t/t課程名稱:%s/n", ad->name); printf("/n/t/t課程編號:%s/n", ad->num); printf("/t/t課程性質:%s/n", ad->nature); printf("/t/t開課學期:%s/n", ad->term); printf("/t/t總學時:%lf/n", ad->time_all); printf("/t/t授課學時:%lf/n", ad->time_teach); printf("/t/t實驗或上機學時:%lf/n", ad->time_exp); printf("/t/t學分:%lf/n/n", ad->score); printf("/n/t/t------------------------------------------------------"); }} void course_showall(course *head){ course *p; p = head; while (p != NULL) { course_showone(p); p = p->next; } return;} void cour_nat_te(course* head){ while (1) { printf("/n/t/t請輸入要查找課程的性質,返回上級菜單請輸入0000:"); char input1[50]; scanf("%s", input1); while (getchar() != '/n'); if (strcmp(input1, "0000") == 0) break; printf("/n/t/t請輸入要查找課程的開課學期:"); char input2[50]; scanf("%s", input2); while (getchar() != '/n'); int count = 0; while (head != NULL) { if ((strcmp(input1, head->nature) == 0) && (strcmp(input2, head->term) == 0)) { course_showone(head); count++; } head = head->next; } if (count == 0) printf("/n/t/t#####沒有符合查找條件的課程!#####/n"); }} void search_course(course *head){ int flag; while (true) { flag = 0; printf("/n/t/t請輸入查找條件 [1]課程編號 [2]課程名稱 [3]課程性質/n/t/t[4]開課學期 [5]課程性質與開課學期 [0]返回上級菜單:"); scanf("%d", &flag); while (getchar() != '/n'); switch (flag) { case(1):coursenumfindtip(head); break; case(2):coursenamefindtip(head); break; case(3):courseshow_nature(head); break; case(4):courseshow_term(head); break; case(5):cour_nat_te(head); break; case(0):break; default: printf("/n/t/t#####輸入錯誤,請重新輸入!#####"); } if (flag == 0) break; }}
manager.cpp
#include<stdio.h>#include<string.h>#include"all.h"#include<stdlib.h>#include<malloc.h>#include<ctype.h> extern student* stu_head;extern course* cour_head;extern course *cour_tail;extern student *stu_tail; // 這四個指針變量在main.cpp中已經聲明為全局變量 int man_login(){ FILE *fp; if ((fp = fopen(MAN_F, "r")) == NULL) { printf("/t/t#######無法找到文件:MAN_F######/n"); return -1; } char key[11]; fread(key, 10, 1, fp); key[10] = '/0'; fclose(fp); char input[1000]; printf("/t/t*******請輸入管理員密碼:"); scanf("%s", input); while (getchar() != '/n'); if (strcmp(input, key) == 0) return 1; else return 0;} int man_menu(){ printf("/n/t/t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~管理員菜單~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); printf("/n/t/t---[1]:添加學生"); printf("/n/t/t---[2]:刪除學生"); printf("/n/t/t---[3]:修改學生信息"); printf("/n/t/t---[4]:查找學生信息/n"); printf("/n/t/t---[5]:添加課程"); printf("/n/t/t---[6]:刪除課程"); printf("/n/t/t---[7]:修改課程信息"); printf("/n/t/t---[8]:查找課程信息/n"); printf("/n/t/t---[9]:打印所有課程/學生信息"); printf("/n/t/t---[10]:修改管理員密碼"); printf("/n/t/t---[11]:數據備份與恢復"); printf("/n/t/t---[0]:注銷/n"); printf("/n/t/t----請從<0-10>中選擇操作類型:"); int n = input_limit0(11); return n;} void student_add(student *head) //增加學生的函數{ while (1) { char input[50]; while (1) { printf("/n/t/t請輸入要添加學生的學號(數字),返回上級菜單請輸入0000:"); scanf("%s", input); while (getchar() != '/n'); if (judge_ascii_num(input) == 1) break; else printf("/n/t/t####輸入錯誤!#####"); } if (strcmp(input, "0000") == 0) break; if (studentnamefind_num(stu_head, input) != stu_tail) { printf("/t/t###已存在該學生,請重新輸入!####/n"); continue; } /*初始化學生信息*/ student* p = (student *)malloc(sizeof(student)); strcpy(p->num, input); printf("/t/t請輸入學生姓名:"); scanf("%s", p->name); while (getchar() != '/n'); printf("/t/t請輸入學生學院和班級(用--隔開):"); scanf("%s", p->sch_cla); while (getchar() != '/n'); printf("/t/t請輸入當前該學生總學分:"); p->score_all = input_double(); p->course_sum = 0; p->score_sel = 0.0; strcpy(p->password, p->num); if (stu_head == NULL)//當前鏈表中沒有節點 { p->next = NULL; stu_head = p; stu_tail = p; } else { if (strcmp(p->num, stu_head->num) < 0)//新增節點的標號比頭節點還小 { p->next = stu_head; stu_head = p; } else if (strcmp(p->num, stu_tail->num) > 0)//新增節點的標號比尾節點還大 { p->next = NULL; stu_tail->next = p; stu_tail = p; } else//新增節點的標號在頭節點和尾節點之間 { student* temp = stu_head; while (temp != stu_tail) { if (strcmp(p->num, temp->num) > 0) { p->next = temp->next; temp->next = p; break; } } } } printf("/n/t/t學生信息錄入成功!/n"); } store_stu(stu_head);} void student_delete(student *head){ while (true) { printf("/n/t/t請輸入要刪除學生的學號或姓名,取消刪除請輸入0000:"); char input[50]; scanf("%s", input); while (getchar() != '/n'); if (strcmp(input, "0000") == 0) break; student *p; if ((input[0] >= 48) && (input[0] <= 57)) p = studentnamefind_num(stu_head, input); else p = studentnamefind_char(stu_head, input); if (p == stu_tail)//沒有該學生 { printf("/n/t/t####沒有該學生信息!#####"); continue; } printf("/n/t/t確定刪除%s?[1]是 [0]否:", input); int in = 0; scanf("%d", &in); while (getchar() != '/n'); if (in == 1) { if (p == NULL)//鏈表頭節點 { if (stu_head->next == NULL) { stu_tail = NULL; } student* temp = stu_head; stu_head = stu_head->next; free(temp); temp = NULL; } else if (p->next == stu_tail)//鏈表尾節點 { p->next = NULL; free(stu_tail); stu_tail = p; } else//鏈表中間節點 { student* delete_point = p->next; p->next = delete_point->next; free(delete_point); delete_point = NULL; } printf("/n/t/t已經刪除該學生的信息!/n"); } else printf("/n/t/t####未刪除該學生的信息!####/n"); } store_stu(stu_head);} void student_modify(student *head){ char input[100]; student* find = NULL; while (1) { printf("/t/t請輸入要修改學生的學號或姓名,取消修改請輸入0000:"); scanf("%s", input); while (getchar() != '/n'); if (strcmp(input, "0000") == 0) break; if ((input[0] >= 48) && (input[0] <= 57))//按學號查找 find = studentnamefind_num(head, input); else//按姓名查找 find = studentnamefind_char(head, input); if (find == stu_tail) { printf("/n/t/t####你所查找的學生不存在!"); continue; } else { char temp[50]; if (find == NULL) find = head; else find = find->next; printf("/n/t/t請分別輸入需要修改的內容,若某一項不需要修改請輸入0"); printf("/n/t/t原姓名:%s 修改后的姓名:", find->name); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) strcpy(find->name, temp); printf("/t/t原學院和班級:%s 修改后的學院和班級(中間用--隔開):", find->sch_cla); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) strcpy(find->sch_cla, temp); printf("/t/t原密碼:%s 修改后的密碼:", find->password); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) strcpy(find->password, temp); printf("/t/t原總學分:%lf (增加學分請使用+,減少學分請使用-,例如“-10” “+10”):", find->score_all); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) { int change = (int)temp[0]; temp[0] = '0'; double number = atof(temp); if (change == 43) find->score_all += number; else if (change == 45) find->score_all -= number; } printf("/t/t修改完成!/n/n"); } } store_stu(stu_head);} void course_add(course *head){ while (1) { char input[50]; while (1) { printf("/n/t/t請輸入要添加課程的編號,返回上一級菜單請輸入0000:"); scanf("%s", input); while (getchar() != '/n'); if (judge_ascii_num(input) == 1) break; else printf("/n/t/t#####輸入錯誤!####"); } if (strcmp(input, "0000") == 0) break; if (coursenumfind_num(cour_head, input) != cour_tail) { printf("/t/t###已存在該課程,請重新輸入!####/n"); continue; } course* p = (course*)malloc(sizeof(course)); /*初始化課程信息*/ strcpy(p->num, input); printf("/n/t/t請輸入課程名稱:"); scanf("%s", p->name); while (getchar() != '/n'); printf("/n/t/t請輸入課程性質:"); scanf("%s", p->nature); while (getchar() != '/n'); printf("/n/t/t請輸入課程總學時:"); p->time_all = input_double(); printf("/n/t/t請輸入課程授課學時:"); p->time_teach = input_double(); printf("/n/t/t請輸入課程實驗或上機學時:"); p->time_exp = input_double(); printf("/n/t/t請輸入課程最大容納人數:"); p->stu_max = input_num(); printf("/n/t/t請輸入課程學分:"); p->score = input_double(); printf("/n/t/t請輸入課程開課學期:"); scanf("%s", p->term); while (getchar() != '/n'); p->stu_sum = 0; if (cour_head == NULL)//鏈表中沒有節點 { p->next = NULL; cour_head = p; cour_tail = p; } else { if (strcmp(p->num, cour_head->num) < 0)//新增節點的標號比頭節點還小 { p->next = cour_head; cour_head = p; } else if (strcmp(p->num, cour_tail->num) > 0)//新增節點的標號比尾節點還大 { p->next = NULL; cour_tail->next = p; cour_tail = p; } else//新增節點的標號在頭節點和尾節點之間 { course* temp = cour_head; while (temp != cour_tail) { if (strcmp(p->num, temp->num) > 0) { p->next = temp->next; temp->next = p; break; } } } } printf("/n/t/t課程信息錄入成功!/n"); } store_cour(cour_head);} void course_delete(course *head){ course *p; char input[50]; while (true) { printf("/n/t/t請輸入要刪除課程的編號或名稱,取消刪除請輸入0000:"); scanf("%s", input); while (getchar() != '/n'); if (strcmp(input, "0000") == 0) break; if ((input[0] >= 48) && (input[0] <= 57)) p = coursenumfind_num(head, input); else p = coursenamefind_char(head, input); if (p == cour_tail)//沒有該課程 { printf("/n/t/t####沒有該課程信息!####"); continue; } printf("/n/t/t確定刪除%s?[1]是 [0]否:", input); int in = 0; scanf("%d", &in); while (getchar() != '/n'); if (in == 1) { if (p == NULL)//鏈表頭節點 { if (head->next == NULL) cour_tail = NULL; course* temp = head; head = head->next; cour_head = head; free(temp); } else if (p->next == cour_tail)//鏈表尾節點 { free(cour_tail); p->next = NULL; cour_tail = p; } else//鏈表中間節點 { course* delete_point = p->next; p = (p->next)->next; free(delete_point); } printf("/n/t/t%s已經刪除!/n", input); } else printf("/n/t/t#####未刪除課程%s的信息!######/n", input); } store_cour(cour_head);} void course_modify(course *head){ char input[100]; course* find = NULL; while (1) { printf("/t/t請輸入要修改課程的編號號或名稱,取消修改請輸入0000:"); scanf("%s", input); while (getchar() != '/n'); if (strcmp(input, "0000") == 0) break; if ((input[0] >= 48) && (input[0] <= 57))//按學號查找 find = coursenumfind_num(head, input); else//按姓名查找 find = coursenamefind_char(head, input); if (find == cour_tail) { printf("/n/t/t####你所查找的課程不存在!####"); continue; } else { char temp[50]; if (find == NULL) find = head; else find = find->next; printf("/n/t/t請分別輸入需要修改的內容,若某一項不需要修改請輸入0"); printf("/n/t/t原名稱:%s 修改后的名稱:", find->name); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) strcpy(find->name, temp); printf("/t/t原性質:%s 修改后的性質:", find->nature); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) strcpy(find->nature, temp); printf("/t/t原開課學期:%s 修改后的開課學期:", find->term); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) strcpy(find->term, temp); //int time_all; //總學時 //int time_teach; //授課學時 //int time_exp; //實驗或上機學時 //int score; //該課程學分 //int stu_max; //能夠容納的學生總數 printf("/t/t原總學時:%lf 修改后的總學時:", find->time_all); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) find->time_all = atof(temp); printf("/t/t原授課學時:%lf 修改后的授課學時:", find->time_teach); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) find->time_teach = atof(temp); printf("/t/t原實驗或上機學時:%lf 修改后的實驗或上機學時:", find->time_exp); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) find->time_exp = atof(temp); printf("/t/t原課程學分:%lf 修改后的課程學分:", find->score); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) find->score = atof(temp); printf("/t/t原能容納學生總數:%d 修改后的能容納學生總數:", find->stu_max); scanf("%s", &temp); while (getchar() != '/n'); if (strcmp(temp, "0") != 0) find->stu_max = atoi(temp); printf("/t/t修改完成!/n/n"); } } store_cour(cour_head);} void print_all(){ while (1) { printf("/n/t/t選擇操作類型: [1]打印所有學生信息 [2]打印所有課程信息 [0]返回主菜單:"); int input = 0; scanf("%d", &input); while (getchar() != '/n'); if (input == 1) { printf("/n/t/t學生總數:%d", link_count(stu_head)); student_showall(stu_head); } else if (input == 2) { printf("/n/t/t課程總數:%d/n", link_count(cour_head)); courseshowall_man(cour_head); } else if (input == 0) break; else printf("/n/t/t####輸入錯誤!請重新輸入!#####/n"); }} void man_modifyKey(){ printf("/t/t******請輸入原密碼(10位):"); char input[20]; scanf("%s", input); while (getchar() != '/n'); FILE *fp; if ((fp = fopen(MAN_F, "r")) == NULL) { printf("/t/t-------- 無法打開文件:MAN_F/n"); exit(0); } char keyOld[11]; fread(keyOld, 10, 1, fp); keyOld[10] = '/0'; if (strcmp(keyOld, input) == 0)//修改 { fclose(fp); char new1[100]; char new2[100]; while (1) { printf("/t/t*******請輸入新密碼(10位):"); scanf("%s", new1); while (getchar() != '/n'); printf("/t/t******請再次確認新密碼(10位):"); scanf("%s", new2); while (getchar() != '/n'); if (keyexam(new1, new2) == 1) { fp = fopen(MAN_F, "w"); fwrite(new1, 10, 1, fp); fclose(fp); printf("/n/t/t------密碼修改成功!/n"); break; } else printf("/t/t####前后密碼不一致,請重新輸入!####/n"); } } else { fclose(fp); printf("/t/t####原密碼輸入錯誤,請重新選擇操作類型####/n"); }} void man_backups_recover(){ printf("/t/t*****[1]數據備份 [2]數據恢復 [3]取消/n"); printf("/t/t*****選擇操作類型:"); int input = 0; scanf("%d", &input); while (getchar() != '/n'); if (input == 1) { FILE *fp_source; FILE *fp_destination; {//課程信息備份 if ((fp_source = fopen(COUR_F, "r")) == NULL) { printf("/t/t##課程數據備份失?。簾o法找到COUR_F文件!##/n"); exit(0); } fp_destination = fopen(COUR_F_BACKUP, "w"); char temp; int count = 0; while (!feof(fp_source)) { if (fread(&temp, 1, 1, fp_source)) count++; } rewind(fp_source); for (int i = 0; i < count; i++) { fread(&temp, 1, 1, fp_source); fwrite(&temp, 1, 1, fp_destination); } fclose(fp_destination); fclose(fp_source); } {//學生信息備份 if ((fp_source = fopen(STU_F, "r")) == NULL) { printf("/t/t##學生數據備份失?。簾o法找到STU_F文件!##/n"); exit(0); } fp_destination = fopen(STU_F_BACKUP, "w"); char temp; int count = 0; while (!feof(fp_source)) { if (fread(&temp, 1, 1, fp_source)) count++; } rewind(fp_source); for (int i = 0; i < count; i++) { fread(&temp, 1, 1, fp_source); fwrite(&temp, 1, 1, fp_destination); } fclose(fp_destination); fclose(fp_source); } {//管理員信息備份 if ((fp_source = fopen(MAN_F, "r")) == NULL) { printf("/t/t##管理員數據備份失?。簾o法找到MAN_F文件!##/n"); exit(0); } fp_destination = fopen(MAN_F_BACKUP, "w"); char temp; for (int i = 0; i < 10; i++) { fread(&temp, 1, 1, fp_source); fwrite(&temp, 1, 1, fp_destination); } fclose(fp_destination); fclose(fp_source); } printf("/n/t/t所有數據已經備份完畢!備份目錄為C:/ stu_cour_info_system/backup/n"); } else if (input == 2) { printf("/t/t選擇恢復類型:[1]課程數據 [2]學生數據 [3]管理員數據:"); int in = 0; scanf("%d", &in); while (getchar() != '/n'); FILE *fp_source; FILE *fp_destination; if (in == 1)//course information recover { //課程信息恢復 if ((fp_source = fopen(COUR_F_BACKUP, "r")) == NULL) { printf("/t/t###課程信息恢復失??!未能找到C://stu_cour_info_system//backup//course_back.txt###/n"); exit(0); } fp_destination = fopen(COUR_F, "w"); char temp; int count = 0; while (!feof(fp_source)) { if (fread(&temp, 1, 1, fp_source)) count++; } rewind(fp_source); for (int i = 0; i < count; i++) { fread(&temp, 1, 1, fp_source); fwrite(&temp, 1, 1, fp_destination); } fclose(fp_destination); fclose(fp_source); printf("/n/t/t課程信息恢復成功!/n"); cour_head = load_cour(); cour_tail = locate_end(cour_head); } else if (in == 2)//student information recover { //學生信息恢復 if ((fp_source = fopen(STU_F_BACKUP, "r")) == NULL) { printf("/t/t###學生信息恢復失??!未能找到C://stu_cour_info_system//backup//student_back!###/n"); exit(0); } fp_destination = fopen(STU_F, "w"); char temp; int count = 0; while (!feof(fp_source)) { if (fread(&temp, 1, 1, fp_source)) count++; } rewind(fp_source); for (int i = 0; i < count; i++) { fread(&temp, 1, 1, fp_source); fwrite(&temp, 1, 1, fp_destination); } fclose(fp_destination); fclose(fp_source); printf("/n/t/t學生數據恢復成功!/n"); stu_head = load_stu(); stu_tail = locate_end(stu_head); } else if (in == 3)//manager information recover { //恢復管理員數據 if ((fp_source = fopen(MAN_F_BACKUP, "r")) == NULL) { printf("/t/t###管理員數據恢復失敗!未能找到C://stu_cour_info_system//backup//course_back/n"); exit(0); } fp_destination = fopen(MAN_F, "w"); char temp; for (int i = 0; i < 10; i++) { fread(&temp, 1, 1, fp_source); fwrite(&temp, 1, 1, fp_destination); } fclose(fp_destination); fclose(fp_source); printf("/n/t/t管理員數據恢復成功!/n"); } else printf("/t/t###輸入錯誤!###/n"); } else if (input == 3) ; else printf("/t/t###輸入錯誤,請重新輸入!####/n");} void man_search_stu(student *head){ while (1) { printf("/n/t/t請輸入學號或姓名進行查找,返回上級菜單請輸入0000:"); char input[50]; scanf("%s", input); while (getchar() != '/n'); if (strcmp(input, "0000") == 0) break; student* p_stu; if ((input[0] >= 48) && (input[0] <= 57)) p_stu = studentnamefind_num(head, input); else p_stu = studentnamefind_char(head, input); if (p_stu == stu_tail) { printf("/n/t/t#####沒有該學生的信息,請重新輸入!#####/n"); continue; } if (p_stu == NULL) p_stu = head; else p_stu = p_stu->next; printf("/n/t/t該學生信息如下:"); student_showone(p_stu); }} void courseshowone_man(course *ad){ if (ad == NULL) printf("無課程"); else { printf("/n/t/t------------------------------------------------------"); printf("/n/t/t課程編號:%s/n", ad->num); printf("/t/t課程名稱:%s/n", ad->name); printf("/t/t課程性質:%s/n", ad->nature); printf("/t/t開課學期:%s/n", ad->term); printf("/t/t總學時:%lf/n", ad->time_all); printf("/t/t授課學時:%lf/n", ad->time_teach); printf("/t/t實驗或上機學時:%lf/n", ad->time_exp); printf("/t/t學分:%lf/n", ad->score); printf("/t/t總容納學生人數:%d/n", ad->stu_max); printf("/t/t已選學生人數:%d/n", ad->stu_sum); printf("/t/t已選學生:/n"); if (ad->stu_sum == 0) printf("/t/t沒有人選該課程/n"); else for (int i = 0; i <ad->stu_sum; i++) { if (i % 5 == 0) printf("/t/t"); student* temp = studentnamefind_num(stu_head, ad->stu[i]); if (temp == NULL) temp = stu_head; else temp = temp->next; printf("[%d] %s", i + 1, temp->name); if (i > 0 && i % 5 == 0) printf("/n"); } printf("/n/t/t------------------------------------------------------"); }} void courseshowall_man(course *head){ course *p; p = head; if (p == NULL) { printf("/t/t無課程/n"); return; } while (p != NULL) { courseshowone_man(p); p = p->next; }}
studetn.cpp
#include<stdio.h>#include<string.h>#include"all.h"#include<stdlib.h>#include<malloc.h>#include<ctype.h> extern student* stu_head;extern course* cour_head;extern course *cour_tail; extern student *stu_tail;// 這四個指針變量在main.cpp中已經聲明為全局變量 student* stu_login(student* head){ student* copy_head = head; char input[100]; printf("/t/t*******請輸入學號:"); scanf("%s", input); while (getchar() != '/n'); student* temp; temp = studentnamefind_num(head, input); if (temp == NULL)//the first student return copy_head; else if ((temp != NULL) && (temp->next == NULL))//no such a student return NULL; else return temp->next;} int stu_menu(){ printf("/n/t/t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~學生菜單~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); printf("/n/t/t---[1]:打印課程信息"); printf("/n/t/t---[2]:查找課程信息"); printf("/n/t/t---[3]:選擇新的課程"); printf("/n/t/t---[4]:刪除已選課程"); printf("/n/t/t---[5]:查看個人信息"); printf("/n/t/t---[6]:修改登陸密碼"); printf("/n/t/t---[0]:注銷"); printf("/n/n/t/t---請從<0-6>中選擇操作類型:"); int n = input_limit0(6); return n;} void courseshow_nature(course *head){ course *p; char ad[MAXN]; while (true) { int flag = 0, sum = 0; p = head; printf("/t/t請輸入要查找課程的性質,取消查找請輸入0000:"); scanf("%s", ad); while (getchar() != '/n'); if (strcmp(ad, "0000") == 0) break; while (p != NULL) { if (strcmp(ad, p->nature) == 0) { course_showone(p); flag = 1; sum++; } p = p->next; } if (flag == 0) printf("/n/t/t沒有該課程!####"); } return;} void courseshow_term(course *head){ course *p; char ad[MAXN]; while (true) { int flag = 0, sum = 0; p = head; printf("/t/t請輸入要查找課程的開課學期,取消查找請輸入0000:"); scanf("%s", ad); while (getchar() != '/n'); if (strcmp(ad, "0000") == 0) break; while (p != NULL) { if (strcmp(ad, p->term) == 0) { course_showone(p); flag = 1; sum++; } p = p->next; } if (flag == 0) printf("/n/t/t#####沒有該課程!#####"); } return;} void coursechoose( course* head, student* stu)//選課函數{ if (stu->score_sel <stu->score_all ) printf("/n/t/t你的總學分少于%lf,趕快去選課吧!/n", stu->score_all); while (1) { printf("/n/t/t請輸入課程的編號或名稱進行選課,取消選課請輸入0000:"); char input[50]; scanf("%s", input); while(getchar()!='/n'); if (strcmp(input, "0000")==0) break; course* find = NULL; if ((input[0] >= 48) && (input[0] <= 57)) find = coursenumfind_num(head, input); else find = coursenamefind_char(head, input); if (find == cour_tail) { printf("/n/t/t#####該課程不存在!請重新輸入#####/n"); continue; } else if (find == NULL) find = head; else find = find->next; if (find->stu_sum == find->stu_max) printf("/n/t/t####該課程人數已滿,無法選擇!#####/n"); else { int judge = 0; for (int i = 0; i < stu->course_sum; i++) { if (strcmp(stu->course[i], find->num)==0) {judge = 1; break;} } if (judge == 1) { printf("/n/t/t####該課程已經選擇過了!####/n"); continue; } else { stu->course_sum++; strcpy(stu->course[stu->course_sum - 1], find->num); stu->score_sel += find->score; find->stu_sum++; strcpy(find->stu[find->stu_sum - 1], stu->num); printf("/n/t/t該課程選擇成功!/n"); } } } store_cour(cour_head); store_stu(stu_head);} void coursenumfindtip(course *head){ char buf[MAXN]; course *result; while (true) { printf("/t/t請輸入要查找課程的編號,取消查找請輸入0000:"); scanf("%s", buf); while (getchar() != '/n'); if (strcmp(buf, "0000") == 0) break; result = coursenumfind_num(head, buf); if (result != cour_tail) { if (result == NULL) result = head; else result = result->next; course_showone(result); } else printf("/t/t#####沒有該課程!#####/n"); }} void coursenamefindtip(course *head){ char buf[MAXN]; course *result; while (1) { printf("/t/t請輸入要查找課程的名稱,取消查找請輸入0000:"); scanf("%s", buf); while (getchar() != '/n'); if (strcmp(buf, "0000") == 0) break; result = coursenamefind_char(head, buf); if (result != cour_tail) { if (result == NULL) result = head; else result = result->next; course_showone(result); } else printf("/t/t#####無所查找的課程!#####/n"); }} void stu_dele_cour( course * head, student * stu){ while (1) { printf("/n/t/t請輸入需要刪除課程的編號,返回上級菜單請輸入0000:"); char input[50]; scanf("%s", input); while (getchar() != '/n'); if (strcmp(input, "0000") == 0) break; int i = 0, result = -1; /*刪除該學生記錄中選課的信息*/ for (; i < stu->course_sum; i++) { if (strcmp(input, stu->course[i]) == 0) { result = i; break; } } if (result == -1) {printf("/n/t/t####你沒有選擇這門課程!請重新輸入!");continue;} strcpy(stu->course[result], stu->course[stu->course_sum - 1]); stu->course_sum--; course* find= coursenumfind_num(head, input); if (find == NULL) find = head; else find =find->next; /*刪除該課程記錄中學生的信息*/ i = 0, result = 0; for (; i < find->stu_sum; i++) { if(strcmp(input,find->stu[i])==0) { result = i; break;} } strcpy(find->stu[result], find->stu[find->stu_sum - 1]); find->stu_sum --; printf("/n/t/t課程刪除成功!/n"); } store_stu(stu_head); store_cour(cour_head);} void stu_modifyKey(student* stu){ printf("/t/t******請輸入原密碼(8位):"); char input[20]; scanf("%s", input); while (getchar() != '/n'); char keyOld[10]; strcpy(keyOld, stu->password); if (keyexam(keyOld, input) == 1)//修改 { char new1[100]; char new2[100]; while (1) { printf("/t/t*******請輸入新密碼(8位):"); scanf("%s", new1); while (getchar() != '/n'); printf("/t/t******請再次確認新密碼(8位):"); scanf("%s", new2); while (getchar() != '/n'); if (keyexam(new1, new2) == 1) { strcpy(stu->password, new1); store_stu(stu_head); printf("/n/t/t------密碼修改成功!/n"); break; } else printf("/t/t####前后密碼不一致,請重新輸入!####/n"); } } else printf("/t/t####原密碼輸入錯誤,請重新選擇操作類型####/n");}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選