1 Star 3 Fork 0

wonderwhyy/student_mgt_console

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 8.17 KB
一键复制 编辑 原始数据 按行查看 历史
wonderwhyy 提交于 2020-12-13 18:54 . 上传代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tools.h"
/**
* 学生信息结构体
*/
typedef struct student
{
//学号
char* no;
//姓名
char* name;
//专业
char* major;
//邮箱
char* email;
//电话
char* phone;
struct student* next;
} student;
//全局变量用来存储学生信息
student* stu_list;
//学号二叉搜索树
b_s_t stu_no_bst;
//学生信息本地文件路径
const char* file_path = "./stu.txt";
//学生信息统一分隔符,利用strtok处理“1041101;王五;软件工程;123@163.com;12345678912”这种信息
const char* delimiter = ";";
//新建一个学生采用头插法,插入到list中
void create_stu(char* no, char* name, char* major, char* email, char* phone);
//从文件中读取学生信息到stu_list
void read_file_2_list(const char * file_path);
//将list序列化到本地文件,释放list
void write_list_2_file(const char* file_path);
//释放学生结构体
void free_stu(student* s);
//释放学生结构体列表
void free_stu_list();
//打印一个学生信息
void print_student(student* s);
//打印所有的学生
void printAll();
//添加一个学生
void add_student();
//根据学号查找学生
student* get_student_by_no(char* no);
//搜索学生
void search_student();
//修改学生
void update_student();
//删除学生
void delete_student();
//导入学生
void import_student();
int main() {
//从文件读取数据加载到list中
read_file_2_list(file_path);
puts("+++++++++++++++++++学生信息管理系统+++++++++++++++++++\n");
//用来储存输入的命令
char c;
while (1)
{
puts("+++++++++++++++++++主菜单+++++++++++++++++++");
puts("现在您可以输入一个命令来管理学生信息.");
puts("1 : 录入学生.");
puts("2 : 修改学生.");
puts("3 : 删除学生.");
puts("4 : 查找学生.");
puts("5 : 浏览学生.");
puts("6 : 导入学生.");
puts("0 : 退出!");
puts("请输入指令:");
scanf(" %c", &c);
if (c == '0')
{
puts("谢谢使用");
break;
}
switch (c)
{
case '1':
add_student();
break;
case '2':
update_student();
break;
case '3':
delete_student();
break;
case '4':
search_student();
break;
case '5':
printAll();
break;
case '6':
import_student();
break;
default:
break;
}
}
//将list中的数据保存到文件中
write_list_2_file(file_path);
//释放list
free_stu_list();
return 0;
}
void create_stu(char* no, char* name, char* major, char* email, char* phone)
{
//校验学号是否重复
student* temp = get_student_by_no(no);
if (temp != NULL) {
puts("该学号已存在!");
return;
}
student* s = (student*)malloc(sizeof(student));
s->no = _strdup(no);
s->name = _strdup(name);
s->major = _strdup(major);
s->email = _strdup(email);
s->phone = _strdup(phone);
s->next = stu_list;
stu_list = s;
stu_no_bst = insert_node(stu_no_bst, no, s);
}
void read_file_2_list(const char* file_path)
{
FILE* fp = fopen(file_path, "r");
if (fp != NULL) {
char buf[400];
while (fscanf(fp, "%s", buf) != EOF)
{
char* no = strtok(buf, delimiter);
char* name = strtok(NULL, delimiter);
char* major = strtok(NULL, delimiter);
char* email = strtok(NULL, delimiter);
char* phone = strtok(NULL, delimiter);
create_stu(no, name, major, email, phone);
}
}
fclose(fp);
}
void write_list_2_file(const char* file_path)
{
FILE* fp = fopen(file_path, "w");
student* s = stu_list;
while (s != NULL)
{
fprintf(fp, "%s;%s;%s;%s;%s\n", s->no, s->name, s->major, s->email, s->phone);
s = s->next;
}
fclose(fp);
}
void free_stu(student* s)
{
free(s->no);
free(s->name);
free(s->major);
free(s->email);
free(s->phone);
free(s);
}
void free_stu_list()
{
student* s = stu_list;
student* next = NULL;
while (s)
{
next = s->next;
free_stu(s);
s = next;
}
}
void print_student(student* s)
{
printf("学号:%s\t姓名:%s\t专业:%s\t邮箱:%s\t电话:%s\n", s->no, s->name, s->major, s->email, s->phone);
}
void printAll()
{
student* s = stu_list;
while (s)
{
print_student(s);
s = s->next;
}
}
void add_student()
{
char no[30];
char name[30];
char major[30];
char email[30];
char phone[30];
puts("请输入学号:");
scanf("%s", no);
//校验学号是否重复
student* s = get_student_by_no(no);
if (s != NULL) {
puts("该学号已存在!");
return;
}
puts("请输入姓名:");
scanf("%s", name);
puts("请输入专业:");
scanf("%s", major);
puts("请输入邮箱:");
scanf("%s", email);
puts("请输入手机号:");
scanf("%s", phone);
create_stu(no, name, major, email, phone);
puts("录入完成!");
}
student* get_student_by_no(char* no)
{
/*student* s = stu_list;
while (s != NULL)
{
if (strcmp(s->no, no) == 0)
{
return s;
}
s = s->next;
}*/
b_s_t bst = find(stu_no_bst, no);
if (bst)
{
return (student*) bst->vp;
}
return NULL;
}
void search_student()
{
char c;
char buf[30];
while (1)
{
puts("+++++++++++++++++++查找学生菜单+++++++++++++++++++");
puts("1 : 按学生的学号寻找记录.");
puts("2 : 按学生的姓名寻找记录.");
puts("0 : 返回上一级菜单.");
scanf(" %c", &c);
if (c == '0')
{
return;
}
if (c == '1')
{
puts("请输入学生学号:");
scanf("%s", buf);
student* s = get_student_by_no(buf);
if (s)
{
print_student(s);
}
else
{
puts("该学生学号不存在!");
}
}
else if (c == '2')
{
puts("请输入学生姓名:");
scanf("%s", buf);
student* s = stu_list;
while (s != NULL)
{
if (strcmp(s->name, buf) == 0)
print_student(s);
s = s->next;
}
}
}
}
void update_student()
{
char c;
char buf[30];
while (1)
{
puts("+++++++++++++++++++修改学生菜单+++++++++++++++++++");
puts("1 : 按学生的学号寻找记录.");
puts("0 : 返回上一级菜单.");
scanf(" %c", &c);
if (c == '0')
{
return;
}
if (c == '1')
{
puts("请输入学生学号:");
scanf("%s", buf);
student* s = get_student_by_no(buf);
if (s)
{
print_student(s);
puts("是否进行修改:y/N");
scanf(" %c", &c);
if (c == 'Y' || c == 'y')
{
//修改学生的操作
puts("请输入姓名:");
scanf("%s", buf);
free(s->name);
s->name = _strdup(buf);
puts("请输入专业:");
scanf("%s", buf);
free(s->major);
s->major = _strdup(buf);
puts("请输入邮箱:");
scanf("%s", buf);
free(s->email);
s->email = _strdup(buf);
puts("请输入手机号:");
scanf("%s", buf);
free(s->phone);
s->phone = _strdup(buf);
puts("修改成功!");
}
}
else
{
puts("该学生学号不存在!");
}
}
}
}
void delete_student()
{
char c;
char buf[30];
while (1)
{
puts("+++++++++++++++++++删除学生菜单+++++++++++++++++++");
puts("1 : 按学生的学号寻找记录.");
puts("0 : 返回上一级菜单.");
scanf(" %c", &c);
if (c == '0')
{
return;
}
if (c == '1')
{
puts("请输入学生学号:");
scanf("%s", buf);
student* s = get_student_by_no(buf);
if (s)
{
print_student(s);
puts("是否进行删除:y/N");
scanf(" %c", &c);
if (c == 'Y' || c == 'y')
{
student* p = NULL;
student* r = stu_list;
while (r)
{
if (strcmp(buf,r->no) == 0) {
if (p == NULL) {
stu_list = r->next;
}
else
{
p->next = r->next;
}
stu_no_bst = delete_node(stu_no_bst, r->no);
r->next = NULL;
free(r);
puts("删除成功!");
break;
}
p = r;
r = r->next;
}
}
}
else
{
puts("该学生学号不存在!");
}
}
}
}
void import_student()
{
char c;
char buf[255];
while (1)
{
puts("+++++++++++++++++++导入学生菜单+++++++++++++++++++");
puts("1 : 输入导入文件路径,请确保内部数据格式为如下所示:\n1041101;王五;软件工程;123@163.com;12345678912.");
puts("0 : 返回上一级菜单.");
scanf(" %c", &c);
if (c == '0')
{
return;
}
if (c == '1')
{
puts("请输入文件路径:");
scanf("%s", buf);
puts("是否进行导入:y/N");
scanf(" %c", &c);
if (c == 'Y' || c == 'y')
{
read_file_2_list(buf);
puts("导入成功!");
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/wonderwhyy/student_mgt_console.git
git@gitee.com:wonderwhyy/student_mgt_console.git
wonderwhyy
student_mgt_console
student_mgt_console
master

搜索帮助