本文實例為大家分享了C語言實現學生選課系統的具體代碼,供大家參考,具體內容如下
代碼:
#include<stdio.h>#include<windows.h>#include<stdlib.h>#include<conio.h> typedef unsigned char uint8_t;typedef unsigned short uint16_t;typedef unsigned int uint32_t; #define CLASS_CLS system("cls")#define CLASS_NAME 80 typedef struct class { char name[CLASS_NAME]; /* 課程名稱 -- 唯一性 */ uint32_t nature; /* 課程性質(必修或者選修) */ uint32_t total_period; /* 課程總學時 */ uint32_t teach_period; /* 授課學時 */ uint32_t exper_period; /* 上機學時 */ uint32_t start_time; /* 課程開始時間 */ uint8_t score; /* 課程學分 */ uint8_t is_exsit; /* 課程是否存在 */ struct class *next; } class_t; // 課程結構體 class_t *head = NULL;static uint32_t count = 1; void play(char *text, int display, int time, int nu) //動畫打印{ CLASS_CLS; int i, len; for(i = 0; i <= nu; i++) { printf("/n"); } for(i = 0; i < 25; i++) { printf(" "); } len = strlen(text); for(i = 0; i < len; i++) { printf("%c", text[i]); Sleep(display); } Sleep(time);} void titile(char *text, char *str){ CLASS_CLS; uint8_t i; for(i = 0; i < 25; i++) { printf(" "); } printf("%s/n", text); for(i = 0; i <= 60; i++) { printf("%s", str); } printf("/n");} void menu(void){ titile("【學生選課系統】", "-"); printf("/n/t|-----------------------------------|"); printf("/n/t| [1]--增加課程 |"); printf("/n/t| [2]--瀏覽課程 |"); printf("/n/t| [3]--查詢課程 |"); printf("/n/t| [4]--刪除課程 |"); printf("/n/t| [5]--修改課程 |"); printf("/n/t| [Q]--退出系統 |"); printf("/n/t|-----------------------------------|");} void get_bat_data(void){ class_t *point, *q; uint32_t count = 0; FILE *fp = fopen("c://student_elective.dat", "rb"); rewind(fp); point = (class_t *)malloc(sizeof(class_t)); head = point; while(!feof(fp)) { count++; fread(point, sizeof(class_t), 1, fp); point->next = (class_t *)malloc(sizeof(class_t)); q = point; point = point->next; } q->next = NULL; fclose(fp);} void save_bat_data(void){ class_t *point = head; FILE *fp = fopen("c://student_elective.dat", "w+"); while(NULL != point) { count++; fwrite(point, sizeof(class_t), 1, fp); point = point->next; } fclose(fp);} uint32_t num_check(void){ char ch; uint32_t sum = 0; while(1) { ch = getch(); if('/n' == ch || '/r' == ch) { return sum; } else if('/b' == ch) { sum /= 10; printf("/b /b"); } else if(('0' <= ch) && ('9' >= ch)) { sum *= 10; sum += ch - '0'; printf("%d", ch - '0'); } } } void create(void){ class_t *point, *q; char tmp[CLASS_NAME], ch; uint8_t flag = 0; while(1) { if(1 != count) { printf("是否繼續增加課程(y/n):"); gets(tmp); if(strcmp(tmp, "n") == 0) { break; } } point = (class_t *)malloc(sizeof(class_t)); point->is_exsit = 0; printf("/n====請輸入第%d個選修課程信息====/n", count); printf("選擇課程名稱:"); gets(point->name); q = head; while(NULL != q) { if(strcmp(q->name, point->name) == 0) { flag = 1; printf("課程名稱重復或者不合格,請重新輸入.../n"); break; } q = q->next; } if(1 == flag) { continue; } printf("課程性質:"); printf("/n[B]--【必修】 [X]--【選修】"); while(1) { ch = getch(); if(ch == 'b' || ch == 'B') { point->nature = 1; break; } if(ch == 'x' || ch == 'X') { point->nature = 2; break; } } printf("/n輸入總學時:(只接受數字!)"); point->total_period = num_check(); printf("/n輸入授課學時:(只接受數字!)"); point->teach_period = num_check(); printf("/n輸入上機學時:(只接受數字!)"); point->exper_period = num_check(); printf("/n輸入本課程學分:(只接受數字!)"); point->score = num_check(); printf("/n輸入開課學期:(只接受數字!)"); point->start_time = num_check(); point->is_exsit = 1; point->next = head; head = point; count++; } printf("信息錄入完畢,按任意鍵繼續……"); getch();} void display(void){ class_t *point = head; CLASS_CLS; titile("【查看課程】", "-"); printf("/n名稱 /t性質/t總學時/t授課學時/t上機學時/t學分/t開課學期"); while(NULL != point) { if(1 == point->is_exsit) { printf("/n%-14s ", point->name); if(1 == point->nature) { printf("必修課"); } else { printf("選修課"); } printf(" %d時 %d時 %d時 %d分 %d時", point->total_period, point->teach_period, point->exper_period, point->score, point->start_time); } point = point->next; } getch();}// 對照學生管理系統自行拓展void search(void){ } void modify(void){ } void delete(void){ } int main(void){ uint8_t value; uint8_t movie = 1; char choice[3]; FILE *fp = fopen("c://student_elective.dat", "a"); fclose(fp); system("color 30"); system("mode con:cols=100 lines=35"); system("title 【選修課系統】"); if(1 == movie) { play("歡迎使用【選修課系統】", 80, 1500, 10); } while(1) { CLASS_CLS; menu(); do { gets(choice); value = atoi(choice); } while((value > 12) || (value < 0)); switch(value) { case 1: create(); break; case 2: display(); break; case 3: search(); break; case 4: modify(); break; case 5: delete(); break; case 6: save_bat_data(); break; case 7: get_bat_data(); break; case 8: exit(1); break; default: break; } } return 0;}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選