学生管理系统 增删改查 随便写的有错误处还请指出
效果
代码
public.h
#ifndef _PUBLIC_H
#define _PUBLIC_H
#include "studentMangager.h"
// 打印日志宏
#define LOG_FILE(_code, _information) \
errLog(__FILE__,__LINE__,(_code),(_information),LOG_FILE);
#define LOG_SCREEN(_code, _information) \
errLog(__FILE,__LINE__,(_code),(_information),LOG_SCREEN);
#ifdef LOG_PRINT_FILE
#define LOG(_code, _information) LOG_FILE((_code), (_information))
#else
#define LOG(_code, _information) LOG_SCREEN((_code), (_information))
#endif
// 功能:获取当前时间 HH24:MI:SS
// 参数:_current_time 返回获取的当前时间字符串
// 返回:_current_time
char* get_current_time(char* _current_time);
// 功能:获取当前日期 YYYYMMDD
// 参数:_current_date 返回获取的当前日期字符串
// 返回:_current_date
char* get_current_date(char* _current_date);
// 功能:打印日志
// 参数:_current_file 当前文件
// 参数:_current_line 当前行
// 参数:_current_code 日志错误码
// 参数: _information 日志信息
// 参数:_log_type LOG_FILE-打印到日志中 LOG_SCREEN-打印屏幕输出 默认打印文本中
int errLog(const char* _current_file, int _current_line, int _current_code,
const char* _information, int _log_type);
// 功能:从键盘获取输入用*覆盖输入内容 用来获取密码输入
// 参数:_password 获取输入的字符串
// 参数:_max_size 获取输入的最大长度
void get_password(char* _password, int _max_size);
// 功能:设置字体颜色为绿色 windows下
void SetPrintBlue();
// 功能:设置当前光标位置 windows下
void SetPosition(SHORT _x, SHORT _y);
#endif //! _PUBLIC_H
studentMangager.h
#ifndef _STUENTMANAGER_H
#define _STUENTMANAGER_H
#define _CRT_SECURE_NO_WARNINGS
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <Windows.h>
// 字符串数组大小
#define MAXSIZE 256
#define MIDSIZE 50
#define MAX_SIZE_ACCOUNT 20
#define MAX_SIZE_WRONG_ACCOUNT 5
// 错误返回码
#define ERR_OPEN_FILE 1000
#define ERR_LOG_TYPE 1001
#define ERR_ACCOUNT_PASSWORD 1002
#define ERR_ACCOUNT_MAX_SIZE_WRONG 1003
#define ERR_MALLOC_NULL 1004
#define ERR_FULL_STUDENT_SIZE 1005
#define ERR_NOT_FOUND 1006
// 日志打印类型
enum LOG_TYPE
{
LOG_FILE = 0,
LOG_SCREEN,
LOG_OTHER
};
// 性别
enum SEX
{
MAN = 0,
WOMAN = 1
};
// 选项
enum OPENTION
{
OP_ADD = 1,
OP_DELETE,
OP_UPDATE,
OP_SEARCH
};
// 账号信息
typedef struct _ACCOUNT
{
char account[MAX_SIZE_ACCOUNT];
char password[MAX_SIZE_ACCOUNT];
}ACCOUNT;
typedef unsigned int NUMBER;
typedef double SCORE;
#define LOG_PRINT_FILE 1
#define DEFAULT_STUDENT_SIZE 1000
#define SIZE_G_ACCOUNT ((int)(sizeof(g_account)/sizeof(g_account[0])))
// 学生信息
typedef struct _STUDENT
{
char name[MIDSIZE];
char major[MIDSIZE];
NUMBER sno;
NUMBER sex;
SEX scs;
SCORE act_score;
SCORE multi_score;
}STUDENT;
typedef struct _STUDENTLIST
{
STUDENT* student;
}STUDENTLIST;
#define GET_STUDENT_LIST_SNO(_list) ((_list)->student->sno)
#endif //! _STUENTMANAGER_H
process.h
#ifndef _PROCESS_H
#define _PROCESS_H
#include "public.h"
#include "studentMangager.h"
// 全局账户数组
extern ACCOUNT g_account[];
// 全局学生信息
extern STUDENTLIST* g_student_list;
// 功能:获取账号
// 参数:_account 存放获取的账号信息的指针
void get_account(ACCOUNT* _account);
// 功能:验证账号信息是否正确
// 参数:_account 需要验证的账号信息
// 返回:0-成功
int confirm_account(ACCOUNT* _account);
// 功能:初始化登录信息
// 返回:0-成功
int Init_login_account();
// 功能:初始化学生信息
// 返回:0-成功
int Init_student_list();
// 功能:复制学生信息
// 参数:_det_student 目的学生信息指针
// 参数:_src_student 源信息
void set_student_info(STUDENT* _det_student, STUDENT* _src_student);
// 功能:增加学生信息
// 参数:_student 存放获取的信息
// 返回:0-成功 ERR_FULL_STUDENT_SIZE-列表满了
int add_student_inner(STUDENT* _student);
// 功能:增加学生信息
// 参数:_student 存放获取的信息
// 返回:add_student_inner(STUDENT* _student);
int add_student(STUDENT* _student);
// 功能:删除学生信息
// 参数:_sno 需要删除学生的学号
// 返回:0-成功
int del_student_by_sno(NUMBER _sno);
// 功能:搜索学生信息
// 参数:_student 返回获搜索的信息
// 参数:_sno 需要删除学生的学号
// 返回:0-成功 ERR_NOT_FOUND-未找到
int search_student_by_sno(STUDENT* _student, NUMBER _sno);
// 功能:修改学生信息
// 参数:_student 存放获取的信息
// 参数:_sno 需要修改的学生的学号
// 返回:0-成功 ERR_NOT_FOUND-未找到
int update_student_by_sno(STUDENT* _student, NUMBER _sno);
// 功能:主业务循环处理
// 参数:_index 选择的功能序号
void deal(NUMBER _index);
// 功能:清楚学生信息
// 返回:0-成功
int clear_student();
// 功能:清楚学生信息链表
// 返回:0-成功
int clear_student_list();
// 功能:清理
int clear();
// 功能:初始化
void Init();
// 功能:主业务循环
void run();
// 功能:结束 调用清理
void fini();
#endif //! _PROCESS_H
public.cpp
#include "public.h"
char* get_current_time(char* _current_time)
{
time_t t;
time(&t);
struct tm* tt;
tt = localtime(&t);
sprintf(_current_time, "%02d:%02d:%02d",
tt->tm_hour, tt->tm_min, tt->tm_sec);
_current_time[strlen(_current_time)] = '\0';
return _current_time;
}
char* get_current_date(char* _current_date)
{
time_t t;
time(&t);
struct tm* tt;
tt = localtime(&t);
sprintf(_current_date, "%04d%02d%02d",
1900 + tt->tm_year, 1 + tt->tm_mon, tt->tm_mday);
_current_date[strlen(_current_date)] = '\0';
return _current_date;
}
int errLog(const char* _current_file, int _current_line, int _current_code,
const char* _information, int _log_type)
{
char currentTime[22] = { 0 };
char currentDate[9] = { 0 };
char logPath[MAXSIZE] = { 0 };
FILE* fp = NULL;
get_current_time(currentTime);
switch (_log_type)
{
case LOG_FILE:
get_current_date(currentDate);
sprintf(logPath, "%s.log", currentDate);
fp = fopen(logPath, "a");
if (NULL == fp) return ERR_OPEN_FILE;
fprintf(fp, "%s file = [%s] line = [%d] code = [%d] message = %s\n",
currentTime, _current_file, _current_line, _current_code,
_information);
fclose(fp);
break;
case LOG_SCREEN:
printf("%s file = [%s] line = [%d] code = [%d] message = %s\n",
currentTime, _current_file, _current_line, _current_code,
_information);
break;
default:
return ERR_LOG_TYPE;
}
return 0;
}
void get_password(char* _password, int _max_size)
{
int i = 0;
char ch;
while (true)
{
ch = _getch();
if (ch == '\r') break;
else if (ch == '\b')
{
if (i > 0)
printf("\b \b"), i--;
continue;
}
printf("*");
if (i >= _max_size) break;
_password[i++] = ch;
}
_password[i] = 0;
}
void SetPrintBlue()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
}
void SetPosition(SHORT _x, SHORT _y)
{
COORD pos = { _x,_y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
process.cpp
#include "process.h"
ACCOUNT g_account[] =
{
{"admin000","admin000"},
{"admin001","admin001"},
{"admin002","admin002"},
};
STUDENTLIST* g_student_list;
void get_account(ACCOUNT* _account)
{
int input; //这里不对scanf的返回值进行判断 假定输入均为正确
SetPrintBlue();
SetPosition(30, 6);
printf(" ------------------ Welcome ------------------ ");
SetPrintBlue();
SetPosition(40, 10);
printf("Input UserName:");
SetPosition(40, 11);
printf("Input PassWord:");
SetPosition(56, 10);
input = scanf("%s", _account->account);
SetPosition(56, 11);
get_password(_account->password, MAX_SIZE_ACCOUNT);
}
int confirm_account(ACCOUNT* _account)
{
ACCOUNT* p = g_account;
NUMBER i = 0;
while (i < SIZE_G_ACCOUNT)
{
if (0 == strcmp(p->account, _account->account) &&
0 == strcmp(p->password, _account->password))
return 0;
else
{
char msg[MAXSIZE] = {0};
sprintf(msg, "wrong account ! account:[%s] password:[%s]",
_account->account, _account->password);
LOG(ERR_ACCOUNT_PASSWORD, msg);
}
i++, p++;
}
return ERR_ACCOUNT_PASSWORD;
}
int Init_login_account()
{
system("cls");
ACCOUNT account;
memset(&account, 0x00, sizeof(ACCOUNT));
int count = 0;
while (true)
{
system("cls");
get_account(&account);
if (0 == confirm_account(&account))
return 0;
count++;
if (MAX_SIZE_WRONG_ACCOUNT <= count)
{
char msg[MAXSIZE] = {0};
sprintf(msg, "max size wrong account!");
LOG(ERR_ACCOUNT_MAX_SIZE_WRONG, msg);
return ERR_ACCOUNT_MAX_SIZE_WRONG;
}
}
return ERR_ACCOUNT_MAX_SIZE_WRONG;
}
int Init_student_list()
{
g_student_list = (STUDENTLIST*)malloc(
sizeof(STUDENTLIST) * DEFAULT_STUDENT_SIZE);
if (NULL == g_student_list)
{
char msg[MAXSIZE] = {0};
sprintf(msg, "init student list fail!");
LOG(ERR_MALLOC_NULL, msg);
return ERR_MALLOC_NULL;
}
memset(g_student_list, 0x00,sizeof(STUDENTLIST) * DEFAULT_STUDENT_SIZE);
return 0;
}
void set_student_info(STUDENT* _det_student, STUDENT* _src_student)
{
memset(_det_student, 0x00, sizeof(STUDENT));
strcpy(_det_student->name, _src_student->name);
strcpy(_det_student->major, _src_student->major);
_det_student->sno = _src_student->sno;
_det_student->sex = _src_student->sex;
_det_student->scs = _src_student->scs;
_det_student->act_score = _src_student->act_score;
_det_student->multi_score = _src_student->multi_score;
}
int add_student_inner(STUDENT* _student)
{
STUDENTLIST* p = g_student_list;
NUMBER i = 0;
while (i < DEFAULT_STUDENT_SIZE)
{
if (NULL == p->student)
{
p->student = _student;
return 0;
}
i++, p++;
}
char msg[MAXSIZE] = {0};
sprintf(msg, "student list is full!");
LOG(ERR_FULL_STUDENT_SIZE, msg);
return ERR_FULL_STUDENT_SIZE;
}
int add_student(STUDENT* _student)
{
STUDENT* student = (STUDENT*)malloc(sizeof(STUDENT));
if (NULL == student)
{
char msg[MAXSIZE] = {0};
sprintf(msg, "init student fail!");
LOG(ERR_MALLOC_NULL, msg);
return ERR_MALLOC_NULL;
}
set_student_info(student, _student);
return add_student_inner(student);
}
int del_student_by_sno(NUMBER _sno)
{
STUDENTLIST* p = g_student_list;
NUMBER i = 0;
while (i < DEFAULT_STUDENT_SIZE)
{
if (NULL != p && NULL != p->student &&
_sno == GET_STUDENT_LIST_SNO(p))
{
free(p->student);
p->student = NULL;
return 0;
}
i++, p++;
}
return 0;
}
int search_student_by_sno(STUDENT* _student, NUMBER _sno)
{
STUDENTLIST* p = g_student_list;
NUMBER i = 0;
while (i < DEFAULT_STUDENT_SIZE)
{
if (NULL != p && NULL != p->student &&
_sno == GET_STUDENT_LIST_SNO(p))
{
set_student_info(_student, p->student);
return 0;
}
i++, p++;
}
char msg[MAXSIZE] = {0};
sprintf(msg, "student sno:[%d] not found!", _sno);
LOG(ERR_NOT_FOUND, msg);
return ERR_NOT_FOUND;
}
int update_student_by_sno(STUDENT* _student, NUMBER _sno)
{
STUDENTLIST* p = g_student_list;
NUMBER i = 0;
while (i < DEFAULT_STUDENT_SIZE)
{
if (NULL != p && NULL != p->student &&
_sno == GET_STUDENT_LIST_SNO(p))
{
set_student_info(p->student, _student);
return 0;
}
i++, p++;
}
char msg[MAXSIZE] = {0};
sprintf(msg, "student sno:[%d] not found!", _sno);
LOG(ERR_NOT_FOUND, msg);
return ERR_NOT_FOUND;
}
void deal(NUMBER _index)
{
switch (_index)
{
case OP_ADD:
{
STUDENT student;
memset(&student, 0x00, sizeof(STUDENT));
printf("姓名 专业 学号 性别(0-男 1-女) 班级 入学成绩 综合成绩\n ");
scanf("%s %s %d %d %d %lf %lf", student.name,student.major,
&student.sno, &student.sex, &student.scs, &student.act_score,
&student.multi_score);
add_student(&student);
}
break;
case OP_DELETE:
{
NUMBER sno = 0;
printf("学号\n");
scanf("%d", &sno);
del_student_by_sno(sno);
}
break;
case OP_UPDATE:
{
STUDENT student;
NUMBER sno = 0;
memset(&student, 0x00, sizeof(STUDENT));
printf("学号\n");
scanf("%d", &sno);
printf("姓名 专业 学号 性别(0-男 1-女) 班级 入学成绩 综合成绩\n ");
scanf("%s %s %d %d %d %lf %lf", student.name,student.major,
&student.sno, &student.sex, &student.scs, &student.act_score,
&student.multi_score);
update_student_by_sno(&student, sno);
}
break;
case OP_SEARCH:
{
STUDENT student;
NUMBER sno = 0;
memset(&student, 0x00, sizeof(STUDENT));
printf("学号\n");
scanf("%d", &sno);
search_student_by_sno(&student, sno);
printf("姓名 专业 学号 性别(0-男 1-女) 班级 入学成绩 综合成绩\n ");
printf("%s %s %d %d %d %f %f\n", student.name,student.major,
student.sno, student.sex, student.scs, student.act_score,
student.multi_score);
}
break;
default:
break;
}
printf("\n");
}
void Init()
{
if (0 != Init_login_account()) exit(-1);
if (0 != Init_student_list()) exit(-1);
}
void run()
{
int input; //这里不对scanf的返回值进行判断 假定输入均为正确
system("cls");
system("color 70");
while (true)
{
int index = 0;
printf("*****************************************************\n");
printf("*1、增\n");
printf("*2、删\n");
printf("*3、改\n");
printf("*4、查\n");
printf("*5、退出\n");
printf("*6、清屏\n");
printf("*****************************************************\n");
printf("*请输入选项: ");
char input_index[MAXSIZE] = { 0 };
input = scanf("%s", input_index);
if (1 != input)
continue;
input_index[sizeof(input_index) - 1] = '\0';
index = atoi(input_index);
switch (index)
{
case 1:
case 2:
case 3:
case 4:
deal(index);
break;
case 5:
exit(0);
break;
case 6:
system("cls");
break;
default:
printf("\n*不支持该选项\n");
break;
}
}
}
int clear_student()
{
STUDENTLIST* p = g_student_list;
NUMBER i = 0;
while (i < DEFAULT_STUDENT_SIZE)
{
if (NULL != p && NULL != p->student)
free(p->student), p->student = NULL;
i++, p++;
}
return 0;
}
int clear_student_list()
{
if (NULL != g_student_list)
free(g_student_list), g_student_list = NULL;
return 0;
}
int clear()
{
clear_student();
clear_student_list();
return 0;
}
void fini()
{
clear();
}
#include "process.h"
int main()
{
Init();
run();
fini();
return 0;
}