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 80typedef 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;
}