💯 博客内容:复习数据结构
😀 作 者:陈大大陈
🚀 个人简介:一个正在努力学技术的准前端,专注基础和实战分享 ,欢迎私信!
💖 欢迎大家:这里是CSDN,我总结知识和写笔记的地方,喜欢的话请三连,有问题请私信 😘 😘 😘
这个程序底层是easyx图形库,通过在easyx上封装的一个工具类(即输入框、按钮、label等),是在此基础上开发的这个系统。
我们期末的数据结构实训已经结束了,我凭借着一手粥批图形界面员工管理系统给老师整不会了,
也是顺利得到优秀,接下来就给大家展示一下我一周的成果!
大家记得三连加关注,接下来我会一直改进这个代码,把它改造的越来越好的。
目录
运行截图
源码分享
employ.h
employ.c
DK_DUI.cpp
DK_DUI.h
main.cpp
程序插入的图片
运行截图
会有一个从零到百分之一百的加载动画,这里就不展示了。
点击职工管理会进入职工管理界面。
我手动添加了几个干员信息。
接下来点击排序职工可以按不同方式排序。
下图是点击ID倒序排序的结果:
可以按ID和姓名两种方式查询:
下图是查找ID为1的职工的结果。
也可添加职工,添加如下干员的信息。
点击添加:
点击删除可以删除该条职工信息。
删除星熊的信息。
删除成功。
点击编辑,可以编辑干员的信息。
如图,将诗怀雅的职位从吉祥物改为龙门高级警司。
点击修改。
可以看到诗怀雅的职位成功被改变。高级警司捏!
点击退出系统,会弹出上面的窗口,点击确定程序自动保存并退出。
信息会保存在一个文本文档里,下次可以直接读取。
第一个数据是职工的个数。
源码分享
employ.h
#pragma once
#ifdef _cplusplus
Extern "C"{
#endif
typedef struct Employee
{
char id[10];
char name[15];
char sex[10];
char birt[9];
char educ[20];
char job[15];
char tele[15];
char addr[20];
struct Employee* pNext;
}Employee;
Employee* CreateEmployee();
Employee* CreateEmployeeForm(char* id, char* name, char* sex, char* birt, char* educ, char* job, char* tele, char* addr);
#ifdef _cplusplus
}
#endif
employ.c
#define _CRT_SECURE_NO_WARNINGS
#include "Employee.h"
#include<malloc.h>
#include<assert.h>
#include<string.h>
#include<stdio.h>
Employee* CreateEmployee()
{
Employee* emp = (Employee*)calloc(1, sizeof(Employee));
assert(emp);
return emp;
}
Employee* CreateEmployeeForm(char* id, char* name, char* sex, char* birt, char* educ, char* job,char* tele, char* addr)
{
Employee* emp = CreateEmployee();
strcpy(emp->id, id);
strcpy(emp->name, name);
strcpy(emp->sex, sex);
strcpy(emp->birt, birt);
strcpy(emp->educ, educ);
strcpy(emp->job, job);
strcpy(emp->tele, tele);
strcpy(emp->addr, addr);
return emp;
}
const char** emp_toStringArray(Employee* emp)
{
static const char* info[8];
static char buf[8][20];
sprintf(buf[0], "%s", emp->id);
sprintf(buf[1], "%s", emp->name);
sprintf(buf[2], "%s", emp->sex);
sprintf(buf[3], "%s", emp->birt);
sprintf(buf[4], "%s", emp->educ);
sprintf(buf[5], "%s", emp->job);
sprintf(buf[6], "%s", emp->addr);
sprintf(buf[7], "%s", emp->tele);
for (int i = 0; i < 8; i++)
{
info[i] = buf[i];
}
return info;
}
DK_DUI.cpp
#include "DK_GUI.h"
#include <malloc.h>
#include <stdio.h>
/*palette*/
//按钮调色板
Palette ButtonPalette = { RGB(240,240,240),RGB(166,166,166),BLACK ,BLACK ,RGB(179,202,234),RGB(179,202,234) ,RGB(196,213,255) };
//编辑框调色板
Palette EditPalette = { RGB(242,245,252),RGB(252,252,252),BLACK ,BLACK ,RGB(133,145,162),RGB(32,61,119),RGB(252,252,252) };
void drawRoundRect(Rect* rect);
void drawRoundRect2(Rect* rect)
{
roundrect(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h, 10, 10);
}
DrawShapeCallBack gDrawFunc = drawRoundRect2;//绘制圆角矩形//
const int bufferSize = 100;
struct GUIState /*UI状态*/
{
//Mouse
int mx;
int my; //鼠标当前的坐标//
bool mouseDown;
int hotItem; //鼠标当前悬停的item
int activeItem; //拥有鼠标焦点的item
//key
int kbdItem; //拥有键盘焦点的item
int keyEntered; //按下的按键
int keyModifier; //修饰符按键
//char
char ch[bufferSize];
int lastWidget;
}state = { 0,0,false };
/*控件*/
bool GUI_button(int id, Rect rect, const char* text)
{
//第一步//
if (pointInRect({ state.mx, state.my }, rect))
{
state.hotItem = id;
//如果没有获取焦点的item,并且鼠标按下了,就获取焦点
if (state.activeItem == 0 && state.mouseDown)
state.activeItem = id;
}
//第二步//
if (state.hotItem == id) //如果鼠标在当前按钮上悬停
{
if (state.activeItem == id)//有焦点,焦点变色
{
setfillcolor(ButtonPalette.focusColor);
setlinecolor(ButtonPalette.focusColor);
}
else//鼠标所在位置变色
{
setfillcolor(ButtonPalette.bgHoverColor);
setlinecolor(ButtonPalette.borderHoverColor);
}
settextcolor(ButtonPalette.fgHoverColor);//前景颜色
}
else
{
setfillcolor(ButtonPalette.bgNormalColor);//背景颜色
setlinecolor(ButtonPalette.borderNormalColor);//前景平时颜色
settextcolor(ButtonPalette.fgNormalColor);//前景文字颜色
}
//绘制按钮
gDrawFunc(&rect);//绘制圆角矩形//
//绘制文本
outtextxy(rect.x + (rect.w - textwidth(text)) / 2, rect.y + (rect.h - textheight(text)) / 2, text);
//鼠标悬停在项目上时触发该代码 返回true
if (state.mouseDown == false && state.hotItem == id && state.activeItem == id)
return true;
return false;
}
/*bool GUI_button(int id, Rect rect, const char* text)
{
//如果没有控件有键盘焦点,获取它
if (state.kbdItem == 0)
state.kbdItem = id;
if (pointInRect({ state.mx, state.my }, rect))
{
state.hotItem = id;
//如果没有获取焦点的item,并且鼠标按下了,就获取焦点
if (state.activeItem == 0 && state.mouseDown)
state.activeItem = id;
}
//如果鼠标在当前按钮上悬停
if(state.hotItem == id)
{
//并且有焦点
if (state.activeItem == id)
{
setfillcolor(BROWN);
setlinecolor(ButtonPalette.focusColor);
//setfillcolor(BLACK);
//setlinestyle(PS_DASHDOT, 1);
//setlinecolor(RED);
//Rect r = { rect.x - 1,rect.y - 1,rect.w + 2,rect.h + 2 };
//gDrawFunc(&r);
//setlinestyle(PS_SOLID, 1);
}
else
{
setfillcolor(ButtonPalette.bgHoverColor);
setlinecolor(ButtonPalette.borderHoverColor);
//setfillcolor(gTheme->bgHoverColor);
//setlinecolor(gTheme->borderHoverColor);
//settextcolor(gTheme->fgHoverColor);
}
settextcolor(ButtonPalette.fgHoverColor);
}
else
{
setfillcolor(ButtonPalette.bgNormalColor);
setlinecolor(ButtonPalette.borderNormalColor);
settextcolor(ButtonPalette.fgNormalColor);
//setfillcolor(gTheme->bgNormalColor);
//setlinecolor(gTheme->borderNormalColor);
//settextcolor(gTheme->fgNormalColor);
}
if (state.kbdItem == 0)
state.kbdItem = id;
if (state.kbdItem == id)
{
setfillcolor(ButtonPalette.bgNormalColor);
setlinecolor(ButtonPalette.focusColor);
//setfillcolor(BLACK);
//setlinestyle(PS_DASHDOT, 1);
//setlinecolor(RED);
//Rect r = { rect.x - 1,rect.y - 1,rect.w + 2,rect.h + 2 };
//gDrawFunc(&r);
//setlinestyle(PS_SOLID, 1);
}
//绘制按钮
gDrawFunc(&rect);
//绘制文本
outtextxy(rect.x + (rect.w - textwidth(text)) / 2,
rect.y + (rect.h - textheight(text)) / 2, text);
if (state.kbdItem == id)
{
switch (state.keyEntered)
{
case VK_TAB:
state.kbdItem = 0;
if (state.keyModifier == 1)
state.kbdItem = state.lastWidget;
state.keyEntered = 0;
break;
case VK_RETURN:
return true;
break;
}
}
state.lastWidget = id;
//鼠标弹起时触发点击
if (state.mouseDown == false && state.hotItem == id && state.activeItem == id)
return true;
return false;
}*/
void drawImg(int x, int y, IMAGE* src)
{
// 变量初始化
DWORD* pwin = GetImageBuffer(); //窗口缓冲区指针
DWORD* psrc = GetImageBuffer(src); //图片缓冲区指针
int win_w = getwidth(); //窗口宽高
int win_h = getheight();
int src_w = src->getwidth(); //图片宽高
int src_h = src->getheight();
// 计算贴图的实际长宽
int real_w = (x + src_w > win_w) ? win_w - x : src_w; // 处理超出右边界
int real_h = (y + src_h > win_h) ? win_h - y : src_h; // 处理超出下边界
if (x < 0) { psrc += -x; real_w -= -x; x = 0; } // 处理超出左边界
if (y < 0) { psrc += (src_w * -y); real_h -= -y; y = 0; } // 处理超出上边界
// 修正贴图起始位置
pwin += (win_w * y + x);
// 实现透明贴图
for (int iy = 0; iy < real_h; iy++)
{
for (int ix = 0; ix < real_w; ix++)
{
byte a = (byte)(psrc[ix] >> 24);//计算透明通道的值[0,256) 0为完全透明 255为完全不透明
if (a > 100)
{
pwin[ix] = psrc[ix];
}
}
//换到下一行
pwin += win_w;
psrc += src_w;
}
}
void drawImg(int x, int y, int dstW, int dstH, IMAGE* src, int srcX, int srcY)
{
// 变量初始化
DWORD* pwin = GetImageBuffer(); //窗口缓冲区指针
DWORD* psrc = GetImageBuffer(src); //图片缓冲区指针
int win_w = getwidth(); //窗口宽高
int win_h = getheight();
int src_w = src->getwidth(); //图片宽高
int src_h = src->getheight();
// 计算贴图的实际长宽
int real_w = (x + dstW > win_w) ? win_w - x : dstW; // 处理超出右边界
int real_h = (y + dstH > win_h) ? win_h - y : dstH; // 处理超出下边界
if (x < 0) { psrc += -x; real_w -= -x; x = 0; } // 处理超出左边界
if (y < 0) { psrc += (dstW * -y); real_h -= -y; y = 0; } // 处理超出上边界
//printf("realw,h(%d,%d)\n", real_w, real_h);
// 修正贴图起始位置
pwin += (win_w * y + x);
// 实现透明贴图
for (int iy = 0; iy < real_h; iy++)
{
for (int ix = 0; ix < real_w; ix++)
{
byte a = (byte)(psrc[ix + srcX + srcY * src_w] >> 24);//计算透明通道的值[0,256) 0为完全透明 255为完全不透明
if (a > 100)
{
pwin[ix] = psrc[ix + srcX + srcY * src_w];
}
}
//换到下一行
pwin += win_w;
psrc += src_w;
}
}
//绘制img图像
bool GUI_button(int id, Point pos, IMAGE* normalImg, IMAGE* hoverImg)//buttom:按钮
{
bool inRect = pointInRect({ state.mx, state.my }, { pos.x,pos.y,normalImg->getwidth(),normalImg->getheight() });
//判断点是否在矩形范围内
if (inRect)
{
drawImg(pos.x, pos.y, normalImg);//平时显示的效果
}
else
{
drawImg(pos.x, pos.y, hoverImg);//hoverimg:悬停时显示的效果
}
if (inRect && state.mouseDown)//按下鼠标时创建inRect(可变形矩形)//
return true;
return false;
}
bool GUI_edit(int id, Rect rect, char* buffer, size_t size)
{
if (pointInRect({ state.mx, state.my }, rect))//点击输入框时
{
state.hotItem = id;//鼠标移动到项目上
if (state.activeItem == 0 && state.mouseDown)//鼠标选中设为焦点
state.activeItem = id;
}
if (state.activeItem == id || state.hotItem == id)//鼠标移动到被设为焦点的项目上
{
setfillcolor(EditPalette.bgHoverColor);//鼠标悬停时项目显示的颜色
setlinecolor(EditPalette.borderHoverColor);//该边框显示的颜色
settextcolor(EditPalette.fgHoverColor);//前景颜色
}
else//无焦点,颜色不变
{
setfillcolor(EditPalette.bgNormalColor);
setlinecolor(EditPalette.borderNormalColor);
settextcolor(EditPalette.fgNormalColor);
}
//绘制按钮
gDrawFunc(&rect);
//绘制文本
outtextxy(rect.x + 5, rect.y + (rect.h - textheight(buffer)) / 2, buffer);//在指定位置输出字符串
int len = strlen(buffer);//Buffer:缓冲区
bool changed = false;
if (state.kbdItem == id && (GetTickCount() >> 9) & 1)
outtextxy(rect.x + 5 + textwidth(buffer), rect.y + (rect.h - textheight("|")) / 2, "|");//在指定位置输出字符串
if (state.kbdItem == id)//键盘设定焦点
{
switch (state.keyEntered)//判断按下的案件
{
case VK_TAB:
state.kbdItem = 0;
//按tab键,让焦点切换到下一个输入框
if (state.keyModifier == 1)
state.kbdItem = state.lastWidget;
state.keyEntered = 0;
break;
case '\b'://光标向左移动两个字符, 例: 输入:我喜欢用C语言写\b\b\b程序 输出:我喜欢用C程序写
if (len > 1 && buffer[len - 1] < 0 && buffer[len - 2] < 0)
{
buffer[len - 2] = '\0';
changed = true;
}
//删除一个字符
else if (len > 0)
{
buffer[len - 1] = '\0';
changed = true;
}
break;
case '\r'://换行
case '\n':
state.kbdItem = -1; //清除焦点
return changed;
break;
default:
//最多只能输入size-1长度
if (len < size)
{
//只能输入数字2~9、字母、符号、中文
for (int i = 0; i < bufferSize; i++) {
if (state.ch[i] != '\0') {
buffer[len++] = state.ch[i];
buffer[len] = '\0';
changed = true;
}
else {
break;
}
}
}
break;
}
}
if (state.mouseDown == false && state.hotItem == id && state.activeItem == id)//鼠标悬停在项目上时触发该代码
{
state.kbdItem = id;//使用键盘输入
}
state.lastWidget = id;
return changed;
}
//文本框
void GUI_label(Rect rect, const char* text, bool center)
{
if (center)
{
outtextxy(rect.x + (rect.w - textwidth(text)) / 2, rect.y + (rect.h - textheight(text)), text);
}
else
{
outtextxy(rect.x, rect.y, text);
}
}
bool pointInRect(Point pos, Rect rect)//判断点的位置
{
return pos.x > rect.x && pos.x<rect.x + rect.w && pos.y>rect.y && pos.y < rect.y + rect.h;
}
void setDrawShapeCallBack(DrawShapeCallBack fun)//自定义绘图控件
{
gDrawFunc = fun;
}
void unsetDrawShapeCallBack()
{
gDrawFunc = drawRoundRect;
}
void drawRoundRect(Rect* rect)//绘制圆角矩阵
{
fillroundrect(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h, 10, 10);
}
bool loadTexture(IMAGE* img, const char* filename, int w, int h)//加载图片
{
loadimage(img, filename, w, h);
if (img->getwidth() == 0 || img->getheight() == 0)//图片错误,报错
{
printf("<%s> load failed:%s", filename, "请检查路径或文件名是否正确\n");
return false;
}
return true;
}
void GUI_stateInit()
{
setbkmode(TRANSPARENT);//透明背景
}
/*状态函数*/
void GUI_stateUpdate(ExMessage* msg)
{
if (msg->message == WM_MOUSEMOVE)//鼠标移动,坐标变化
{
state.mx = msg->x;
state.my = msg->y;
}
else if (msg->message == WM_LBUTTONDOWN)//点击鼠标左键
{
state.mouseDown = true;
}
else if (msg->message == WM_LBUTTONUP)//左键弹起
{
state.mouseDown = false;
}
else if (msg->message == WM_CHAR)//输入
{
for (int i = 0; i < bufferSize; i++) {
if (state.ch[i] == '\0') {
state.ch[i] = msg->ch;
break;
}
}
}
else if (msg->message == WM_KEYDOWN)//按下键盘
{
state.keyEntered = msg->vkcode;
state.keyModifier = msg->ctrl ? 1 : (msg->shift ? 2 : 0);
}
else if (msg->message == WM_KEYUP)//按键弹起
{
}
}
void GUI_stateBegin()
{
setbkcolor(WHITE);
BeginBatchDraw();
cleardevice();
//每帧开始时,重置鼠标悬停
state.hotItem = 0;
}
void GUI_stateEnd()
{
EndBatchDraw();// 结束批量绘制,并执行未完成的绘制任务
//如果鼠标没有按下
if (!state.mouseDown)
{
//取消焦点
state.activeItem = 0;
}
//如果鼠标按下了
else
{
if (state.activeItem == 0)//设置焦点
state.activeItem = -1;
}
if (state.keyEntered == VK_TAB)//按下tab键
state.kbdItem = 0;
state.keyEntered = 0;
memset(state.ch, '\0', sizeof(state.ch));
}
DK_DUI.h
#ifndef DK_GUI_H_
#define DK_GUI_H_
#include "DK_GUI.h"
#include <malloc.h>
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#ifdef UNICODE
#undef UNICODE
#endif // UNICODE
#include<easyx.h>
#ifdef _START_ID_
#define GUID __LINE__ + _START_ID_
#else
#define GUID __LINE__
#endif // !_START_ID_
struct Rect
{
int x;
int y;
int w;
int h;
};
struct Point
{
int x;
int y;
};
/*按钮*/
bool GUI_button(int id, Rect rect, const char* text);
bool GUI_button(int id, Point pos, IMAGE* normalImg, IMAGE* hoverImg);
/*输入框*/
bool GUI_edit(int id, Rect rect, char* buffer, size_t len);
/*文本框*/
void GUI_label(Rect rect, const char* text, bool center);
/*对话框*/
#define GUI_DialogEx(title,width,height) \
Rect rect = { (getwidth() - width) / 2,(getheight() - height) / 2,width,height };\
setfillcolor(ButtonPalette.bgNormalColor);\
drawRoundRect(&rect);\
\
settextcolor(BLACK);\
GUI_label({ rect.x + 5,rect.y + 5,0,0 }, title, false);\
\
if (GUI_button(GUID, { rect.x + rect.w - 32,rect.y,32,32 }, "X"))
#define GUI_Dialog(title) GUI_DialogEx(title,350,250)
/*helper*/
bool pointInRect(Point pos, Rect rect);
void drawImg(int x, int y, IMAGE* src);
void drawImg(int x, int y, int dstW, int dstH, IMAGE* src, int srcX, int srcY);
void drawRoundRect(Rect* rect);
/*自定义控件绘图*/
typedef void (*DrawShapeCallBack)(Rect* rect);
void setDrawShapeCallBack(DrawShapeCallBack fun);
void unsetDrawShapeCallBack();
/*加载图片*/
bool loadTexture(IMAGE* img, const char* filenmae, int w = 0, int h = 0);
/*UIState*/
void GUI_stateInit();
void GUI_stateUpdate(ExMessage* msg);
void GUI_stateBegin();
void GUI_stateEnd();
/*Palette 调色板*/
struct Palette
{
//背景
COLORREF bgNormalColor; //背景颜色
COLORREF bgHoverColor; //鼠标悬停背景颜色
//前景(文本)
COLORREF fgNormalColor; //前景颜色(文本)
COLORREF fgHoverColor; //前景颜色(鼠标悬停)
//边框
COLORREF borderNormalColor; //边框正常颜色
COLORREF borderHoverColor; //边框鼠标悬停颜色
//焦点
COLORREF focusColor;
};
extern Palette ButtonPalette;
extern Palette EditPalette;
#endif // !DK_GUI_H_
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include"GUI/DK_GUI.h"
#include "Employee.h"
#include"Employee.c"
#include<stdio.h>
#include<Windows.h>
#include<easyx.h>
/*
*课题内容:图形界面版本管理系统
*开发环境:vs2019 + easyx
*/
int g_nValue = 0;
int editButton = 10100;
int deleteButton = 10200;
int editInput = 10300;
int newInput = 10400;
char id[10];
char name[15];
char sex[10];
char birt[9];
char educ[20];
char job[15];
char tele[15];
char addr[20];
char editId[10];
char editName[15];
char editSex[10];
char editBirt[9];
char editEduc[20];
char editJob[15];
char editTele[15];
char editAddr[20];
enum Page
{
MenuPage,//菜单界面
ShowStudentPage, //显示学生界面
AddStudentPage, //添加学生界面
EditStudentPage, //编辑学生界面
SearchStudentPage,//查找学生界面
SortStudentPage,
QuitSystemPage
};
Page page = MenuPage;
//创建员工
Employee* emp[20];
int emp_size = 0;
void write()//写入数据
{
FILE* fp;
int i = 0;
if ((fp = fopen("F:\\date2.txt", "w")) == NULL)
{
printf("无法打开文件!\n");
return;
}
fprintf(fp, "%d\n", emp_size);
while (i < emp_size)
{
fprintf(fp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", emp[i]->id, emp[i]->name, emp[i]->sex, emp[i]->birt, emp[i]->educ, emp[i]->job, emp[i]->tele, emp[i]->addr);
i++;
}
fclose(fp);
}
void read()//读入数据
{
FILE* fp;
int i = 0;
if ((fp = fopen("F:\\date2.txt", "r")) == NULL)
{
printf("请您先在F盘根目录下新建一个文本文件,文件名为date2\n\n\n");
system("pause");
system("cls");
return;
}
fscanf(fp, "%d\n", &emp_size);
printf("%d", emp_size);
while (i < emp_size)
{
emp[i] = { CreateEmployeeForm("001","陈一","男","20040615","大学本科","校长","邯郸市","15831701156") };
fscanf(fp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", emp[i]->id, emp[i]->name, emp[i]->sex, emp[i]->birt, emp[i]->educ, emp[i]->job, emp[i]->tele, emp[i]->addr);
i++;
}
fclose(fp);
}
void swap()
{
int i, j;
if (emp_size == 0)
{
printf("无记录!\n");
}
int a;
FILE* fp = fopen("F:\\date2.txt", "w+");
fseek(fp, 1, SEEK_CUR);
//fscanf(fp, "%d\n", &a);
//printf("%d", a);
for (i = 0; i < emp_size; i++)
{
for (j = 0; j < emp_size - i - 1; j++)
{
if (atoi(emp[j]->id) < atoi(emp[j + 1]->id))
{
Employee* kingdom;
kingdom = emp[j];
emp[j] = emp[j + 1];
emp[j + 1] = kingdom;
}
}
}
//fprintf(fp, "%d\n", a);
fputs("\n", fp);;
for (i = 0; i < emp_size; i++)
{
fprintf(fp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", emp[i]->id, emp[i]->name, emp[i]->sex, emp[i]->birt, emp[i]->educ, emp[i]->job, emp[i]->tele, emp[i]->addr);
}
printf("排序成功");
write();
}
void swap2()
{
int i, j;
if (emp_size == 0)
{
printf("无记录!\n");
}
int a;
FILE* fp = fopen("F:\\date2.txt", "w+");
fseek(fp, 1, SEEK_CUR);
//fscanf(fp, "%d\n", &a);
//printf("%d", a);
for (i = 0; i < emp_size; i++)
{
for (j = 0; j < emp_size - i - 1; j++)
{
if (atoi(emp[j]->id) > atoi(emp[j + 1]->id))
{
Employee* kingdom;
kingdom = emp[j];
emp[j] = emp[j + 1];
emp[j + 1] = kingdom;
}
}
}
//fprintf(fp, "%d\n", a);
fputs("\n", fp);;
for (i = 0; i < emp_size; i++)
{
fprintf(fp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", emp[i]->id, emp[i]->name, emp[i]->sex, emp[i]->birt, emp[i]->educ, emp[i]->job, emp[i]->tele, emp[i]->addr);
}
printf("排序成功");
write();
}
void swap3()
{
int i, j = 0;
FILE* fp = fopen("F:\\date2.txt", "w+");
fseek(fp, 1, SEEK_CUR);
for (i = 0; i < emp_size; i++)
{
for (j = 0; j < emp_size - i - 1; j++)
{
if (strcmp(emp[j]->name, emp[j + 1]->name) < 0)
{
Employee* kingdom;
kingdom = emp[j];
emp[j] = emp[j + 1];
emp[j + 1] = kingdom;
}
}
}
fputs("\n", fp);;
for (i = 0; i < emp_size; i++)
{
fprintf(fp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", emp[i]->id, emp[i]->name, emp[i]->sex, emp[i]->birt, emp[i]->educ, emp[i]->job, emp[i]->tele, emp[i]->addr);
}
printf("排序成功");
write();
}
void swap4()
{
int i, j = 0;
FILE* fp = fopen("F:\\date2.txt", "w");
fseek(fp, 1, SEEK_CUR);
for (i = 0; i < emp_size; i++)
{
for (j = 0; j < emp_size - i - 1; j++)
{
if (strcmp(emp[j]->name, emp[j + 1]->name) > 0)
{
Employee* kingdom;
kingdom = emp[j];
emp[j] = emp[j + 1];
emp[j + 1] = kingdom;
}
}
}
fputs("\n", fp);;
for (i = 0; i < emp_size; i++)
{
fprintf(fp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", emp[i]->id, emp[i]->name, emp[i]->sex, emp[i]->birt, emp[i]->educ, emp[i]->job, emp[i]->tele, emp[i]->addr);
}
printf("排序成功");
write();
}
void test()
{
//IMAGE img, enter, exitimg;
int btnx = (getwidth() - 150) / 2;
int btny = (getheight() - 5 * 35) / 2;
LOGFONT font;
gettextstyle(&font);
settextstyle(36, 0, "黑体");
GUI_label({ 0,btny - 60,getwidth(),0 }, "图形界面职工管理系统", true);
EditPalette.fgNormalColor = RGB(255, 255, 255);
EditPalette.fgHoverColor = RGB(255, 255, 255);
settextstyle(&font);
static const char* btnText[3] = { "职工管理", "退出系统" };
/*所有的按钮和编辑框都要写在begin和end中间*/
//菜单选择
for (int i = 0; i < 2; i++)
{
if (GUI_button(GUID + i, { btnx,btny + i * 55,150,35 }, btnText[i]) && page == MenuPage)
{
//read();
switch (i)
{
case 0:
read();
page = ShowStudentPage;
break;
case 1:
page = QuitSystemPage;
break;
}
}
}
//printf("refresh - %d\n", page);
if (page == ShowStudentPage)
{
if (GUI_button(1678, { 450 ,10,80,30 }, "排序职工"))
{
page = SortStudentPage;
}
if (GUI_button(1000, { 900 ,10,80,30 }, "添加职工")) {
memset(editId, '\0', sizeof(id));
memset(editName, '\0', sizeof(name));
memset(editSex, '\0', sizeof(sex));
memset(editBirt, '\0', sizeof(birt));
memset(editEduc, '\0', sizeof(educ));
memset(editJob, '\0', sizeof(job));
memset(editTele, '\0', sizeof(tele));
memset(editAddr, '\0', sizeof(addr));
page = AddStudentPage;
return;
}
GUI_DialogEx("显示学生", 950, 500)
{
page = MenuPage;
}
static const char* headers[9] = { "编号","姓名","性别","生日","学历","职务","电话","住址" , "操作" };
for (int i = 0; i < 9; i++)
{
GUI_label({ rect.x + 100 * i + 20 ,rect.y + 35 }, headers[i], false);
}
Employee* filterEmp[20];
int show_size = 0;
// todo 4 查询过滤,示例
for (int i = 0; i < emp_size; i++) {
Employee* e = emp[i];
//if (strcmp(e->name, "陈一") == 0) {
filterEmp[show_size++] = e;
//}
}
// todo 5 排序
int height = 40;
for (int i = 0; i < show_size; i++)
{
Employee* aim = filterEmp[i];
const char** infos = emp_toStringArray(aim);
for (int j = 0; j < 8; j++)
{
GUI_label({ rect.x + 100 * j + 20 ,rect.y + height * (i + 2) }, infos[j], false);
}
// 编辑按钮 及 点击触发的事件
if (GUI_button(editButton + i, { rect.x + 100 * 8 - 10,rect.y + height * (i + 2) - 10,50,30 }, "编辑"))
{
g_nValue = i;
memcpy(id, aim->id, strlen(aim->id));
memcpy(name, aim->name, strlen(aim->name));
memcpy(sex, aim->sex, strlen(aim->sex));
memcpy(birt, aim->birt, strlen(aim->birt));
memcpy(educ, aim->educ, strlen(aim->educ));
memcpy(job, aim->job, strlen(aim->job));
memcpy(tele, aim->tele, strlen(aim->tele));
memcpy(addr, aim->addr, strlen(aim->addr));
GUI_DialogEx("编辑职工", 400, 420)
{
page = ShowStudentPage;
}
//page = AddStudentPage;
page = EditStudentPage;
printf("删除编号为 %s 的职工\n", emp[i]->id);
// todo 3 根据编号删除记录
int x = rect.x + (rect.w - 150) / 2 + 20;
int y = rect.y + (rect.h - 5 * 40) / 2 - 70;
GUI_label({ x - 50,y + 10 }, "编号", false);
GUI_label({ x - 50,y + 50 }, "姓名", false);
GUI_label({ x - 50,y + 90 }, "性别", false);
GUI_label({ x - 50,y + 130 }, "生日", false);
GUI_label({ x - 50,y + 170 }, "学历", false);
GUI_label({ x - 50,y + 210 }, "职务", false);
GUI_label({ x - 50,y + 250 }, "电话", false);
GUI_label({ x - 50,y + 290 }, "住址", false);
GUI_edit(newInput + 100, { x,y,150,40 }, id, 20);
GUI_edit(newInput + 200, { x,y + 40,150,40 }, name, 20);
GUI_edit(newInput + 300, { x,y + 80,150,40 }, sex, 20);
GUI_edit(newInput + 400, { x,y + 120,150,40 }, birt, 20);
GUI_edit(newInput + 500, { x,y + 160,150,40 }, educ, 20);
GUI_edit(newInput + 600, { x,y + 200,150,40 }, job, 20);
GUI_edit(newInput + 700, { x,y + 240,150,40 }, tele, 20);
GUI_edit(newInput + 800, { x,y + 280,150,40 }, addr, 20);
if (GUI_button(GUID + 2, { x + 150,y + 40,150,40 }, "编辑"))
{
printf("触发添加\n");
printf("id - %s\n", id);
printf("name - %s\n", name);
printf("sex - %s\n", sex);
printf("birt - %s\n", birt);
printf("educ - %s\n", educ);
printf("job - %s\n", job);
printf("tele - %s\n", tele);
printf("addr - %s\n", addr);
// todo 1 创建记录
emp[emp_size++] = CreateEmployeeForm(id, name, sex, birt, educ, job, tele, addr);
for (int j = i; j < emp_size - 1; j++)
{
emp[j] = emp[j + 1];
}
emp_size--;
}
//emp[emp_size++] = CreateEmployeeForm(editId, editName, editSex, editBirt, editEduc, editJob, editTele, editAddr);
}
if (GUI_button(850, { 25 ,10,80,30 }, "查询职工"))
{
//initgraph(1000, 600, EX_SHOWCONSOLE);
page = SearchStudentPage;
}
//排序
// 删除按钮 及 点击触发的事件
if (GUI_button(deleteButton + i, { rect.x + 100 * 8 + 60 ,rect.y + height * (i + 2) - 10,50,30 }, "删除"))
{
printf("删除编号为 %s 的职工\n", emp[i]->id);
// todo 3 根据编号删除记录
for (int j = i; j < emp_size - 1; j++)
{
emp[j] = emp[j + 1];
}
emp_size--;
write();
}
}
}
else if (page == AddStudentPage)
{
//settextcolor(YELLOW);
GUI_DialogEx("添加职工", 400, 420)
{
page = ShowStudentPage;
}
//输入框
int x = rect.x + (rect.w - 150) / 2 + 20;
int y = rect.y + (rect.h - 5 * 40) / 2 - 70;
GUI_label({ x - 50,y + 10 }, "编号", false);
GUI_label({ x - 50,y + 50 }, "姓名", false);
GUI_label({ x - 50,y + 90 }, "性别", false);
GUI_label({ x - 50,y + 130 }, "生日", false);
GUI_label({ x - 50,y + 170 }, "学历", false);
GUI_label({ x - 50,y + 210 }, "职务", false);
GUI_label({ x - 50,y + 250 }, "电话", false);
GUI_label({ x - 50,y + 290 }, "住址", false);
GUI_edit(newInput + 1, { x,y,150,40 }, id, 20);
GUI_edit(newInput + 2, { x,y + 40,150,40 }, name, 20);
GUI_edit(newInput + 3, { x,y + 80,150,40 }, sex, 20);
GUI_edit(newInput + 4, { x,y + 120,150,40 }, birt, 20);
GUI_edit(newInput + 5, { x,y + 160,150,40 }, educ, 20);
GUI_edit(newInput + 6, { x,y + 200,150,40 }, job, 20);
GUI_edit(newInput + 7, { x,y + 240,150,40 }, tele, 20);
GUI_edit(newInput + 8, { x,y + 280,150,40 }, addr, 20);
if (GUI_button(GUID, { x,y + 340,150,40 }, "添加"))
{
printf("触发添加\n");
printf("id - %s\n", id);
printf("name - %s\n", name);
printf("sex - %s\n", sex);
printf("birt - %s\n", birt);
printf("educ - %s\n", educ);
printf("job - %s\n", job);
printf("tele - %s\n", tele);
printf("addr - %s\n", addr);
// todo 1 创建记录
emp[emp_size++] = CreateEmployeeForm(id, name, sex, birt, educ, job, addr, tele);
write();
}
//emp[emp_size++] = { emp_toStringArray(id,name,sex,birt,educ,job,tele,addr) };
}
else if (page == SortStudentPage)
{
GUI_DialogEx("排序职工", 200, 220)
{
page = ShowStudentPage;
}
if (GUI_button(1679, { 460 ,280,95,30 }, "ID正序排序"))
{
swap2();
}
else if (GUI_button(1680, { 460 ,320,95,30 }, "ID倒序排序"))
{
swap();
}
if (GUI_button(1671, { 460 ,365,95,30 }, "姓名倒序排序"))
{
swap3();
}
else if (GUI_button(1681, { 460 ,240,95,30 }, "姓名正序排序"))
{
swap4();
}
}
else if (page == SearchStudentPage)
{
//输入框
GUI_DialogEx("查询职工", 400, 420)
{
page = ShowStudentPage;
}
//编辑框
int x = rect.x + (rect.w - 150) / 2 + 20;
int y = rect.y + (rect.h - 5 * 40) / 2 - 70;
GUI_label({ x - 50,y + 10 }, "编号", false);
GUI_label({ x - 50,y + 50 }, "姓名", false);
GUI_label({ x - 50,y + 90 }, "性别", false);
GUI_label({ x - 50,y + 130 }, "生日", false);
GUI_label({ x - 50,y + 170 }, "学历", false);
GUI_label({ x - 50,y + 210 }, "职务", false);
GUI_label({ x - 50,y + 250 }, "电话", false);
GUI_label({ x - 50,y + 290 }, "住址", false);
GUI_edit(editInput + 9, { x,y,150,40 }, editId, 20);
GUI_edit(editInput + 11, { x,y + 40,150,40 }, editName, 20);
GUI_edit(editInput + 12, { x,y + 80,150,40 }, editSex, 20);
GUI_edit(editInput + 13, { x,y + 120,150,40 }, editBirt, 20);
GUI_edit(editInput + 14, { x,y + 160,150,40 }, editEduc, 20);
GUI_edit(editInput + 15, { x,y + 200,150,40 }, editJob, 20);
GUI_edit(editInput + 16, { x,y + 240,150,40 }, editTele, 20);
GUI_edit(editInput + 17, { x,y + 280,150,40 }, editAddr, 20);
/*if (GUI_button(GUID, { x,y + 340,150,40 }, "查询"))
{
emp[10] = CreateEmployeeForm(id, name, sex, birt, educ, job, tele, addr);
}*/
for (int i = 0; i < emp_size; i++)
{
if (strcmp(emp[i]->id, editId) == 0)
{
GUI_DialogEx("显示学生", 950, 500)
{
page = MenuPage;
}
static const char* headers[8] = { "编号","姓名","性别","生日","学历","职务","电话","住址" };
for (int i = 0; i < 8; i++)
{
GUI_label({ rect.x + 100 * i + 20 ,rect.y + 35 }, headers[i], false);
}
Employee* filterEmp[20];
int show_size = 0;
// todo 4 查询过滤,示例
for (int i = 0; i < emp_size; i++) {
Employee* e = emp[i];
if (strcmp(e->name, editName) == 0) {
filterEmp[show_size++] = e;
}
else if (strcmp(e->id, editId) == 0) {
filterEmp[show_size++] = e;
}
}
// todo 5 排序
int height = 40;
for (int i = 0; i < show_size; i++)
{
Employee* aim = filterEmp[i];
const char** infos = emp_toStringArray(aim);
for (int j = 0; j < 8; j++)
{
GUI_label({ rect.x + 100 * j + 20 ,rect.y + height * (i + 2) }, infos[j], false);
}
}
}
}
}
else if (page == EditStudentPage)
{
ButtonPalette.bgHoverColor = RGB(44, 44, 44);//按钮鼠标停留颜色
{
GUI_DialogEx("编辑职工", 400, 420)
{
ButtonPalette.bgHoverColor = RGB(44, 44, 44);//按钮鼠标停留颜色
page = ShowStudentPage;
}
//编辑框
int x = rect.x + (rect.w - 150) / 2 + 20;
int y = rect.y + (rect.h - 5 * 40) / 2 - 70;
GUI_label({ x - 50,y + 10 }, "编号", false);
GUI_label({ x - 50,y + 50 }, "姓名", false);
GUI_label({ x - 50,y + 90 }, "性别", false);
GUI_label({ x - 50,y + 130 }, "生日", false);
GUI_label({ x - 50,y + 170 }, "学历", false);
GUI_label({ x - 50,y + 210 }, "职务", false);
GUI_label({ x - 50,y + 250 }, "电话", false);
GUI_label({ x - 50,y + 290 }, "住址", false);
GUI_edit(editInput + 1, { x,y ,150,40 }, editId, 20);
GUI_edit(editInput + 2, { x,y + 40,150,40 }, editName, 20);
GUI_edit(editInput + 3, { x,y + 80,150,40 }, editSex, 20);
GUI_edit(editInput + 4, { x,y + 120,150,40 }, editBirt, 20);
GUI_edit(editInput + 5, { x,y + 160,150,40 }, editEduc, 20);
GUI_edit(editInput + 6, { x,y + 200,150,40 }, editJob, 20);
GUI_edit(editInput + 7, { x,y + 240,150,40 }, editTele, 20);
GUI_edit(editInput + 8, { x,y + 280,150,40 }, editAddr, 20);
if (GUI_button(GUID, { x,y + 340,150,40 }, "修改"))
{
printf("触发修改\n");
printf("id - %s 不可编辑\n", editId);
printf("name - %s\n", editName);
printf("sex - %s\n", editSex);
printf("birt - %s\n", editBirt);
printf("educ - %s\n", editEduc);
printf("job - %s\n", editJob);
printf("tele - %s\n", editTele);
printf("addr - %s\n", editAddr);
// todo 2 根据编号修改记录
emp[emp_size++] = CreateEmployeeForm(editId, editName, editSex, editBirt, editEduc, editJob, editTele, editAddr);
for (int j = g_nValue; j < emp_size - 1; j++)
{
emp[j] = emp[j + 1];
}
emp_size--;
write();
}
}
}
else if (page == QuitSystemPage)
{
GUI_Dialog("退出系统")
{
page = MenuPage;
}
GUI_label({ rect.x,rect.y + 120,rect.w,0 }, "确定退出管理系统吗?", true);
if (GUI_button(GUID, { rect.x + 80,rect.y + 180 ,75,35 }, "取消"))
{
page = MenuPage;
}
Palette btnp = ButtonPalette;
ButtonPalette.fgHoverColor = YELLOW;
ButtonPalette.fgNormalColor = WHITE;
ButtonPalette.bgNormalColor = RGB(100, 200, 250);
ButtonPalette.bgHoverColor = RGB(5, 159, 255);
if (GUI_button(GUID, { rect.x + 180,rect.y + 180 ,75,35 }, "确定"))
{
write();
exit(0);
}
ButtonPalette = btnp;
}
//write;
}
int main()
{
initgraph(1000, 600, EX_SHOWCONSOLE);
GUI_stateInit();
IMAGE img10, img30, img60, img73, img85, img97, img100, enter, exitimg;
int btnx = (getwidth() - 150) / 2;
int btny = (getheight() - 5 * 35) / 2;
LOGFONT font;
gettextstyle(&font);
settextstyle(36, 0, "黑体");
GUI_label({ 0,btny - 60,getwidth(),0 }, "图形界面员工管理系统", true);
ButtonPalette.bgNormalColor = TRANSPARENT;//所有窗口去掉背景色,换成黑色
ButtonPalette.borderNormalColor = RGB(255, 240, 0);//边框平时颜色
ButtonPalette.bgHoverColor = RGB(44, 44, 44);//按钮鼠标停留颜色
ButtonPalette.borderHoverColor = RGB(255, 240, 0);//边框鼠标停留颜色
ButtonPalette.fgHoverColor = RGB(255, 255, 255);//文本颜色
settextstyle(&font);
ButtonPalette.fgNormalColor = RGB(255, 255, 255);//文本鼠标停留颜色
settextstyle(&font);
int s = 0;
loadimage(&img10, "./picture/启动界面10%.jpg");
putimage(0, 0, &img10);
Sleep(200);
loadimage(&img30, "./picture/启动界面30%.jpg");
putimage(0, 0, &img30);
Sleep(200);
loadimage(&img60, "./picture/启动界面60%.jpg");
putimage(0, 0, &img60);
Sleep(200);
loadimage(&img73, "./picture/启动界面73%.jpg");
putimage(0, 0, &img73);
Sleep(200);
loadimage(&img85, "./picture/启动界面85%.jpg");
putimage(0, 0, &img85);
Sleep(200);
loadimage(&img97, "./picture/启动界面97%.jpg");
putimage(0, 0, &img97);
Sleep(200);
while (true)
{
//每帧调用开始
GUI_stateBegin();
//添加初始页面背景
loadimage(&img100, "./picture/启动界面100%.jpg");
putimage(0, 0, &img100);
//添加进入系统按钮背景图像
loadimage(&enter, "./picture/进入系统.jpg", 150, 35);
putimage(btnx, btny, &enter);
//添加退出系统按钮背景图像
loadimage(&exitimg, "./picture/退出系统.jpg", 150, 35);
putimage(btnx, btny + 55, &exitimg);
test();
//每帧调用结束
GUI_stateEnd();
//处理消息
ExMessage msg = { 0 };
while (peekmessage(&msg))
{
GUI_stateUpdate(&msg);
}
}
return 0;
}
代码的解释都写到注释里面了,有不懂的可以私信问我,我看到就会回你。
程序插入的图片
命名的方式照着上面的代码来就好。
其实职工管理系统和学生管理系统什么的也都差不多,改一下字就行。
希望明年能开发出来明日方舟战斗界面,哈哈哈。