|
发表于 2022-1-7 11:08:34
|
显示全部楼层
本楼为最佳答案
可能我理解错了,我理解的是一个课程对应一个学分
还有选课系统不知道是要做什么
这个 查看课程信息 和 教师信息查看 、学生信息查看 有什么区别
main.c#include "teacher_system.h"
#include "student_system.h"
#include "file_operation.h"
#include "course_selection_system.h"
#include <stdio.h>
static void main_menu(void) {
printf("0.退出系统\n");
printf("1.教师系统\n");
printf("2.学生系统\n");
printf("3.选课系统\n");
}
int main(void) {
teacher_system_init();
student_system_init();
course_selection_system_init();
load_file("data.dat");
while(1) {
main_menu();
printf("请选择: ");
size_t sel;
scanf("%lu", &sel);
switch(sel) {
case 0: goto L;
case 1: teacher_system(); break;
case 2: student_system(); break;
case 3: course_selection_system(); break;
}
}
L:
save_file("data.dat");
teacher_system_deinit();
student_system_deinit();
course_selection_system_deinit();
return 0;
}
teacher_system.h#ifndef _TEACHER_SYSTEM_H_
#define _TEACHER_SYSTEM_H_
#include <stdio.h>
void teacher_system_init(void);
void teacher_system_deinit(void);
void teacher_system_load_file(FILE *fp);
void teacher_system_save_file(FILE *fp);
void teacher_system(void);
#endif
teacher_system.c#include "teacher_system.h"
#include "file_operation.h"
#include "list.h"
#include <string.h>
#include <stdlib.h>
typedef struct {
char id[128];
char name[128];
double credit;
} course_info_t;
typedef struct {
char id[128];
char name[128];
list_t *course_info;
} teacher_t;
static list_t *list;
static void teacher_system_menu(void) {
printf("0.返回上级菜单\n");
printf("1.教师信息注册\n");
printf("2.教师信息注销\n");
printf("3.教师信息查看\n");
printf("4.教师信息修改\n");
printf("5.保存数据到文件\n");
printf("6.从文件读取数据\n");
}
static teacher_t *teacher_init(void) {
teacher_t *teacher = malloc(sizeof(*teacher));
teacher->course_info = list_init();
return teacher;
}
static void teacher_deinit(teacher_t *teacher) {
list_deinit(teacher->course_info);
free(teacher);
}
static void get_teacher(teacher_t *teacher) {
printf("请输入教师编号: ");
scanf("%s", teacher->id);
printf("请输入教师姓名: ");
scanf("%s", teacher->name);
printf("输入 # 结束课程信息输入\n");
list_clean(teacher->course_info);
while(1) {
course_info_t course_info;
printf("请输入课程编号: ");
scanf("%s", course_info.id);
if(course_info.id[0] == '#') break;
printf("请输入课程名称: ");
scanf("%s", course_info.name);
printf("请输入课程学分: ");
scanf("%lf", &course_info.credit);
list_append(teacher->course_info, &course_info, sizeof(course_info));
}
}
static void info_register(void) {
teacher_t *teacher = teacher_init();
get_teacher(teacher);
list_append(list, &teacher, sizeof(teacher));
}
static size_t list_find(const char id[]) {
for(size_t i = 0; i < list_size(list); ++i) {
teacher_t *teacher;
list_get(list, i, &teacher, sizeof(teacher));
if(!strcmp(teacher->id, id)) return i;
}
return (size_t)-1;
}
static void info_unregister(void) {
printf("请输入教师编号: ");
char id[128];
scanf("%s", id);
size_t index = list_find(id);
if(index != (size_t)-1) {
teacher_t *teacher;
list_get(list, index, &teacher, sizeof(teacher));
teacher_deinit(teacher);
list_delete(list, index);
} else printf("注销失败!\n");
}
static void info_view(void) {
for(size_t i = 0; i < list_size(list); ++i) {
teacher_t *teacher;
list_get(list, i, &teacher, sizeof(teacher));
printf("教师编号: %s\n", teacher->id);
printf("教师姓名: %s\n", teacher->name);
for(size_t i = 0; i < list_size(teacher->course_info); ++i) {
course_info_t course_info;
list_get(teacher->course_info, i, &course_info, sizeof(course_info));
printf("课程编号: %s\n", course_info.id);
printf("课程名称: %s\n", course_info.name);
printf("课程学分: %lf\n", course_info.credit);
}
}
}
static void info_change(void) {
printf("请输入教师编号: ");
char id[128];
scanf("%s", id);
size_t index = list_find(id);
if(index != (size_t)-1) {
teacher_t *teacher;
list_get(list, index, &teacher, sizeof(teacher));
get_teacher(teacher);
list_set(list, index, &teacher, sizeof(teacher));
} else printf("修改失败!\n");
}
static void save(void) {
printf("请输入文件名: ");
char filename[128];
scanf("%s", filename);
save_file(filename);
}
static void load(void) {
printf("请输入文件名: ");
char filename[128];
scanf("%s", filename);
load_file(filename);
}
static void teacher_system_clean(void) {
teacher_system_init();
}
void teacher_system_init(void) {
teacher_system_deinit();
list = list_init();
}
void teacher_system_deinit(void) {
for(size_t i = 0; i < list_size(list); ++i) {
teacher_t *teacher;
list_get(list, i, &teacher, sizeof(teacher));
teacher_deinit(teacher);
}
list_deinit(list);
list = NULL;
}
void teacher_system_load_file(FILE *fp) {
if(!fp) return;
teacher_system_clean();
size_t size;
fread(&size, sizeof(size), 1, fp);
for(size_t i = 0; i < size; ++i) {
teacher_t *teacher = teacher_init();
fread(teacher->id, sizeof(teacher->id), 1, fp);
fread(teacher->name, sizeof(teacher->name), 1, fp);
{
size_t size;
fread(&size, sizeof(size), 1, fp);
for(size_t i = 0; i < size; ++i) {
course_info_t course_info;
fread(course_info.id, sizeof(course_info.id), 1, fp);
fread(course_info.name, sizeof(course_info.name), 1, fp);
fread(&course_info.credit, sizeof(course_info.credit), 1, fp);
list_append(teacher->course_info, &course_info, sizeof(course_info));
}
}
list_append(list, &teacher, sizeof(teacher));
}
}
void teacher_system_save_file(FILE *fp) {
if(!fp) return;
size_t size = list_size(list);
fwrite(&size, sizeof(size), 1, fp);
for(size_t i = 0; i < size; ++i) {
teacher_t *teacher;
list_get(list, i, &teacher, sizeof(teacher));
fwrite(teacher->id, sizeof(teacher->id), 1, fp);
fwrite(teacher->name, sizeof(teacher->name), 1, fp);
{
size_t size = list_size(teacher->course_info);
fwrite(&size, sizeof(size), 1, fp);
for(size_t i = 0; i < size; ++i) {
course_info_t course_info;
list_get(teacher->course_info, i, &course_info, sizeof(course_info));
fwrite(course_info.id, sizeof(course_info.id), 1, fp);
fwrite(course_info.name, sizeof(course_info.name), 1, fp);
fwrite(&course_info.credit, sizeof(course_info.credit), 1, fp);
}
}
}
}
void teacher_system(void) {
while(1) {
teacher_system_menu();
printf("请选择: ");
size_t sel;
scanf("%lu", &sel);
switch(sel) {
case 0: goto L;
case 1: info_register(); break;
case 2: info_unregister(); break;
case 3: info_view(); break;
case 4: info_change(); break;
case 5: save(); break;
case 6: load(); break;
}
}
L: ;
}
student_system.h#ifndef _STUDENT_SYSTEM_H_
#define _STUDENT_SYSTEM_H_
#include <stdio.h>
void student_system_init(void);
void student_system_deinit(void);
void student_system_load_file(FILE *fp);
void student_system_save_file(FILE *fp);
void student_system(void);
#endif
student_system.c#include "student_system.h"
#include "file_operation.h"
#include "list.h"
#include <stdlib.h>
#include <string.h>
typedef struct {
char teacher_id[128];
char course_id[128];
} course_info_t;
typedef struct {
char id[128];
char name[128];
char major[128];
list_t *course_info;
} student_t;
static list_t *list;
static void student_system_menu(void) {
printf("0.返回上级菜单\n");
printf("1.学生信息注册\n");
printf("2.学生信息注销\n");
printf("3.学生信息查看\n");
printf("4.学生信息修改\n");
printf("5.保存数据到文件\n");
printf("6.从文件读取数据\n");
}
static student_t *student_init(void) {
student_t *student = malloc(sizeof(*student));
student->course_info = list_init();
return student;
}
static void student_deinit(student_t *student) {
list_deinit(student->course_info);
free(student);
}
static void get_student(student_t *student) {
printf("请输入学生编号: ");
scanf("%s", student->id);
printf("请输入学生姓名: ");
scanf("%s", student->name);
printf("请输入所学专业: ");
scanf("%s", student->major);
printf("输入 # 结束课程信息输入\n");
list_clean(student->course_info);
while(1) {
course_info_t course_info;
printf("请输入教师编号: ");
scanf("%s", course_info.teacher_id);
if(course_info.teacher_id[0] == '#') break;
printf("请输入课程编号: ");
scanf("%s", course_info.course_id);
list_append(student->course_info, &course_info, sizeof(course_info));
}
}
static void info_register(void) {
student_t *student = student_init();
get_student(student);
list_append(list, &student, sizeof(student));
}
static size_t list_find(const char id[]) {
for(size_t i = 0; i < list_size(list); ++i) {
student_t *student;
list_get(list, i, &student, sizeof(student));
if(!strcmp(student->id, id)) return i;
}
return (size_t)-1;
}
static void info_unregister(void) {
printf("请输入学生编号: ");
char id[128];
scanf("%s", id);
size_t index = list_find(id);
if(index != (size_t)-1) {
student_t *student;
list_get(list, index, &student, sizeof(student));
student_deinit(student);
list_delete(list, index);
} else printf("注销失败!\n");
}
static void info_view(void) {
for(size_t i = 0; i < list_size(list); ++i) {
student_t *student;
list_get(list, i, &student, sizeof(student));
printf("学生编号: %s\n", student->id);
printf("学生姓名: %s\n", student->name);
printf("所学专业: %s\n", student->major);
for(size_t i = 0; i < list_size(student->course_info); ++i) {
course_info_t course_info;
list_get(student->course_info, i, &course_info, sizeof(course_info));
printf("教师编号: %s\n", course_info.teacher_id);
printf("课程编号: %s\n", course_info.course_id);
}
}
}
static void info_change(void) {
printf("请输入学生编号: ");
char id[128];
scanf("%s", id);
size_t index = list_find(id);
if(index != (size_t)-1) {
student_t *student;
list_get(list, index, &student, sizeof(student));
get_student(student);
list_set(list, index, &student, sizeof(student));
} else printf("修改失败!\n");
}
static void save(void) {
printf("请输入文件名: ");
char filename[128];
scanf("%s", filename);
save_file(filename);
}
static void load(void) {
printf("请输入文件名: ");
char filename[128];
scanf("%s", filename);
load_file(filename);
}
static void student_system_clean(void) {
student_system_init();
}
void student_system_init(void) {
student_system_deinit();
list = list_init();
}
void student_system_deinit(void) {
for(size_t i = 0; i < list_size(list); ++i) {
student_t *student;
list_get(list, i, &student, sizeof(student));
student_deinit(student);
}
list_deinit(list);
list = NULL;
}
void student_system_load_file(FILE *fp) {
if(!fp) return;
student_system_clean();
size_t size;
fread(&size, sizeof(size), 1, fp);
for(size_t i = 0; i < size; ++i) {
student_t *student = student_init();
fread(student->id, sizeof(student->id), 1, fp);
fread(student->name, sizeof(student->name), 1, fp);
fread(student->major, sizeof(student->major), 1, fp);
{
size_t size;
fread(&size, sizeof(size), 1, fp);
for(size_t i = 0; i < size; ++i) {
course_info_t course_info;
fread(course_info.teacher_id, sizeof(course_info.teacher_id), 1, fp);
fread(course_info.course_id, sizeof(course_info.course_id), 1, fp);
list_append(student->course_info, &course_info, sizeof(course_info));
}
}
list_append(list, &student, sizeof(student));
}
}
void student_system_save_file(FILE *fp) {
if(!fp) return;
size_t size = list_size(list);
fwrite(&size, sizeof(size), 1, fp);
for(size_t i = 0; i < size; ++i) {
student_t *student;
list_get(list, i, &student, sizeof(student));
fwrite(student->id, sizeof(student->id), 1, fp);
fwrite(student->name, sizeof(student->name), 1, fp);
fwrite(student->major, sizeof(student->major), 1, fp);
{
size_t size = list_size(student->course_info);
fwrite(&size, sizeof(size), 1, fp);
for(size_t i = 0; i < size; ++i) {
course_info_t course_info;
list_get(student->course_info, i, &course_info, sizeof(course_info));
fwrite(course_info.teacher_id, sizeof(course_info.teacher_id), 1, fp);
fwrite(course_info.course_id, sizeof(course_info.course_id), 1, fp);
}
}
}
}
void student_system(void) {
while(1) {
student_system_menu();
printf("请选择: ");
size_t sel;
scanf("%lu", &sel);
switch(sel) {
case 0: goto L;
case 1: info_register(); break;
case 2: info_unregister(); break;
case 3: info_view(); break;
case 4: info_change(); break;
case 5: save(); break;
case 6: load(); break;
}
}
L: ;
}
course_selection_system.h#ifndef _COURSE_SELECTION_SYSTEM_H_
#define _COURSE_SELECTION_SYSTEM_H_
void course_selection_system_init(void);
void course_selection_system_deinit(void);
void course_selection_system(void);
#endif
course_selection_system.c#include "course_selection_system.h"
void course_selection_system_init(void) {
}
void course_selection_system_deinit(void) {
}
void course_selection_system(void) {
}
file_operation.h#ifndef _FILE_OPERATION_H_
#define _FILE_OPERATION_H_
void load_file(const char *filename);
void save_file(const char *filename);
#endif
file_operation.c#include "file_operation.h"
#include "teacher_system.h"
#include "student_system.h"
#include <stdio.h>
void load_file(const char *filename) {
FILE *fp = fopen(filename, "r");
if(!fp) return;
teacher_system_load_file(fp);
student_system_load_file(fp);
}
void save_file(const char *filename) {
FILE *fp = fopen(filename, "w");
if(!fp) return;
teacher_system_save_file(fp);
student_system_save_file(fp);
}
list.h#ifndef _LIST_H_
#define _LIST_H_
#include <stddef.h>
#include <stdbool.h>
struct list_node_tag {
void *data; size_t size;
struct list_node_tag *next;
};
typedef struct {
struct list_node_tag *head;
size_t size;
} list_t;
list_t *list_init(void);
void list_deinit(list_t *list);
bool list_clean(list_t *list);
bool list_insert(list_t *list, size_t index, const void *data, size_t size);
bool list_delete(list_t *list, size_t index);
bool list_get(const list_t *list, size_t index, void *data, size_t size);
bool list_set(list_t *list, size_t index, const void *data, size_t size);
bool list_append(list_t *list, const void *data, size_t size);
size_t list_size(const list_t *list);
bool list_empty(const list_t *list);
#endif
list.c#include "list.h"
#include <stdlib.h>
#include <memory.h>
list_t *list_init(void) {
list_t *list = malloc(sizeof(*list));
if(!list) return NULL;
list->head = NULL;
list->size = 0;
return list;
}
void list_deinit(list_t *list) {
if(!list) return;
list_clean(list);
free(list);
}
bool list_clean(list_t *list) {
if(!list) return false;
while(!list_empty(list)) list_delete(list, 0);
return true;
}
bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
if(!list) return false;
if(list_size(list) < index) return false;
if(!data) return false;
struct list_node_tag **current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *node = malloc(sizeof(*node));
if(!node) return false;
node->data = malloc(size);
if(!node->data) {free(node); return false;}
memcpy(node->data, data, size);
node->size = size;
node->next = *current;
*current = node;
++list->size;
return true;
}
bool list_delete(list_t *list, size_t index) {
if(!list) return false;
if(list_size(list) <= index) return false;
struct list_node_tag **current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *temp = *current;
*current = temp->next;
free(temp->data); free(temp);
--list->size;
return true;
}
bool list_get(const list_t *list, size_t index, void *data, size_t size) {
if(!list) return false;
if(list_size(list) <= index) return false;
if(!data) return false;
struct list_node_tag *const *current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *temp = *current;
if(temp->size > size) return false;
memcpy(data, temp->data, temp->size);
return true;
}
bool list_set(list_t *list, size_t index, const void *data, size_t size) {
bool res = list_delete(list, index);
return res ? list_insert(list, index, data, size) : res;
}
bool list_append(list_t *list, const void *data, size_t size) {
if(!list) return false;
return list_insert(list, list_size(list), data, size);
}
size_t list_size(const list_t *list) {
if(!list) return 0;
return list->size;
}
bool list_empty(const list_t *list) {
if(!list) return true;
return list_size(list) == 0;
}
|
|