⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmain.c

📁 纯用C写的图形化的职工管理系统 虽然只实现了基本功能 但很有代表性
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <math.h>
#include <ctype.h>
#include "hz.c"
#include "welcome.c"
#include "bye.c"
#include "sortfunc.c"


/********************define the button********************/
#define SCR_WIDTH           640
#define SCR_HEIGHT          480
#define MENU_TOP            70
#define MENU_LEFT           50
#define BUTTON_WIDTH        55
#define BUTTON_HEIGHT       30
#define BUTTON_PLOT         15
#define MAX_BUTTON_NUM      7
#define MIN_BUTTON_NUM      0
#define NOT_CUR_COLOR       LIGHTGRAY
#define CUR_COLOR           LIGHTCYAN
#define TEXT_COLOR          RED
#define BK_COLOR            LIGHTBLUE
#define PI                  3.14159265

/***********************define key***********************/
#define KB_DOWN  80
#define KB_UP    72
#define KB_LEFT  75
#define KB_RIGHT 77
#define KB_ENTER 28
#define KB_ESC   1
#define KB_TAB   15

/*********************有关职工的定义********************/
#define MAX_WORKERS 20
WORKERTYPE* pworker[MAX_WORKERS]={NULL};
int workersum=0;/*当前职工数*/

/*********************有关按钮的定义********************/
/*按钮与功能函数挂接数组*/
void (*button_fuc[MAX_BUTTON_NUM+1])(void);

/*按钮文本数组*/
char *button_text[MAX_BUTTON_NUM+1]=
			{
				"输入",
				"排序",
				"查找",
				"删除",
				"图表",
				"读入",
				"保存",
				"退出"
			};
/*按钮位置数组*/
int button_pos[MAX_BUTTON_NUM+1][2]=
			{
				MENU_LEFT,MENU_TOP,
				MENU_LEFT+(BUTTON_PLOT+BUTTON_WIDTH),MENU_TOP,
				MENU_LEFT+(BUTTON_PLOT+BUTTON_WIDTH)*2,MENU_TOP,
				MENU_LEFT+(BUTTON_PLOT+BUTTON_WIDTH)*3,MENU_TOP,
				MENU_LEFT+(BUTTON_PLOT+BUTTON_WIDTH)*4,MENU_TOP,
				MENU_LEFT+(BUTTON_PLOT+BUTTON_WIDTH)*5,MENU_TOP,
				MENU_LEFT+(BUTTON_PLOT+BUTTON_WIDTH)*6,MENU_TOP,
				MENU_LEFT+(BUTTON_PLOT+BUTTON_WIDTH)*7,MENU_TOP
			};
/*当前按钮标志*/
int cur_button=0;

/*资料是否已存标志*/
int savedflag=1;

/*************************函数部分****************************/

/*初始化图形模式*/
void initgr(void)
{
	int gd=VGA,gm=VGAHI;
	registerbgidriver(EGAVGA_driver);
	initgraph(&gd,&gm,"");
}

/*识别按键*/
int keyreg(void)
{
   union REGS rg;
   rg.h.ah=0;
   int86(0x16,&rg,&rg);
   return rg.h.ah;
}

/*图形模式下的输入函数,韩海版权所有*/
void ginput(int x,int y,int lenstrict/*长度*/,int mode,
			int backcolor,int charcolor,char* s)
{				/*mode为方式:0,数字;1,字母;2,数字与字母*/
	char temp,*end=s;
	int len=0;
	int cwidth,cur_x;
	cwidth=textwidth("A");
	cur_x=x;
	*s='\0';
	moveto(x,y);
	setcolor(charcolor);
	while(1)
	{
		int k;
		while(1)
		{
			while(!(k=bioskey(1)));/*等待按键*/

			if(k&0x00ff)/*是普通键么*/
			{
				temp=getch();/*是则读进来*/
				break;
			}
			else
				getch();/*不是则丢弃*/
		}

		switch (temp)/*得键处理*/
		{
		case 8:/*是退格么*/
			if (end>s)
			{
				moveto(cur_x-=cwidth,y);/*后退一个字宽*/
				setcolor(backcolor);
				outtext("\xdb");/*用背景色打一个“■”*/
				moveto(cur_x,y);
				setcolor(charcolor);
				*(--end)='\0';/*从串里删除*/
				len--;/*长度减一*/
			}
			break;
		case 13:/*是回车么*/
			return;

		default:
			switch (mode)
			{
			case 0:
					if (isdigit(temp))	/*是数字么*/
						goto pro;		/*goto 不得已而用之*/
					else break;
			case 1:
					if (isalpha(temp))	/*是字母么*/
						goto pro;
					else break;
			case 2:
					if (isalnum(temp))	/*是数字或字母么*/
						goto pro;
					else break;
			pro:
					if(len<lenstrict)/*长度小于限制么*/
					{
					*(end++)=temp;	/*添到串里*/
					len++;
					cur_x+=cwidth;/*当前位置自加一个字宽*/
					*end='\0';
					}
					else
					{
						*(end+1)='\0';
					}
			}
		}
	outtextxy(x,y,s);
	}
}

/*******************有关界面的函数**********************/
/*画凸起按钮*/
void button(int i,int color)
{
	int x1=button_pos[i][0],
		y1=button_pos[i][1],
		x2=button_pos[i][0]+BUTTON_WIDTH,
		y2=button_pos[i][1]+BUTTON_HEIGHT;

	setfillstyle(SOLID_FILL,color);
	bar(x1,y1,x2,y2);

	setcolor(WHITE);
	line(x1,y1,x1,y2);
	line(x1,y1,x2,y1);
	line(x1+1,y1+1,x1+1,y2-1);
	line(x1+1,y1+1,x2-1,y1+1);

	setcolor(DARKGRAY);
	line(x2,y2,x2,y1);
	line(x2,y2,x1,y2);
	line(x2-1,y2-1,x2-1,y1+1);
	line(x2-1,y2-1,x1+1,y2-1);

	setcolor(TEXT_COLOR);
	hz(x1+(BUTTON_WIDTH-30)/2,
		y1+(BUTTON_HEIGHT-15)/2,
		15,1,TEXT_COLOR,button_text[i]);
	setcolor(BK_COLOR);
}


/*画凹陷按钮*/
void buttonflat(int i,int color)
{
	int x1=button_pos[i][0],
		y1=button_pos[i][1],
		x2=button_pos[i][0]+BUTTON_WIDTH,
		y2=button_pos[i][1]+BUTTON_HEIGHT;

	setfillstyle(SOLID_FILL,color);
	bar(x1,y1,x2,y2);

	setcolor(DARKGRAY);
	line(x1,y1,x1,y2);
	line(x1,y1,x2,y1);
	line(x1+1,y1+1,x1+1,y2-1);
	line(x1+1,y1+1,x2-1,y1+1);

	setcolor(WHITE);
	line(x2,y2,x2,y1);
	line(x2,y2,x1,y2);
	line(x2-1,y2-1,x2-1,y1+1);
	line(x2-1,y2-1,x1+1,y2-1);

	setcolor(TEXT_COLOR);
	hz(x1+(BUTTON_WIDTH-30)/2,
		y1+(BUTTON_HEIGHT-15)/2,
		15,1,TEXT_COLOR,button_text[i]);
	setcolor(BK_COLOR);
}

/*当前按钮左移*/
void cur_button_l(void)
{
	button(cur_button,NOT_CUR_COLOR);
	cur_button--;
	if(cur_button<MIN_BUTTON_NUM)
		cur_button=MAX_BUTTON_NUM;
	button(cur_button,CUR_COLOR);
}

/*当前按钮右移*/
void cur_button_r(void)
{
	button(cur_button,NOT_CUR_COLOR);
	cur_button++;
	if(cur_button>MAX_BUTTON_NUM)
		cur_button=MIN_BUTTON_NUM;
	button(cur_button,CUR_COLOR);
}

/*画工作显示区*/
void workarea_draw()
{
	int x1=MENU_LEFT-12,
		y1=MENU_TOP+BUTTON_HEIGHT+18,
		x2=SCR_WIDTH+12-MENU_LEFT,
		y2=SCR_HEIGHT-43;

	setcolor(DARKGRAY);
	line(x1,y1,x1,y2);
	line(x1,y1,x2,y1);
	line(x1+1,y1+1,x1+1,y2-1);
	line(x1+1,y1+1,x2-1,y1+1);

	setcolor(WHITE);
	line(x2,y2,x2,y1);
	line(x2,y2,x1,y2);
	line(x2-1,y2-1,x2-1,y1+1);
	line(x2-1,y2-1,x1+1,y2-1);

	setcolor(BK_COLOR);
}

/*画整个界面*/
void interface_draw()
{
	int i;
	cleardevice();
	hz(SCR_WIDTH/2-8*15,25,30,2,YELLOW,"职工信息管理系统");
	button(cur_button,CUR_COLOR);
	for(i=0;i<=MAX_BUTTON_NUM;i++)
		button(i,NOT_CUR_COLOR);
	button(cur_button,CUR_COLOR);
	workarea_draw();
}


/*开启视口*/
void open_child_viewport()
{
	setviewport(MENU_LEFT-10,MENU_TOP+BUTTON_HEIGHT+20,
		SCR_WIDTH+10-MENU_LEFT,SCR_HEIGHT-45,1);
}

/*关闭视口*/
void close_child_viewport()
{
	clearviewport();
	setviewport(0,0,SCR_WIDTH-1,SCR_HEIGHT-1,0);
	interface_draw();
}

/*********************按钮功能相关函数*********************/

/*输入函数*/
void myinput(void)
#define BAR_LEFT    150
#define BAR_TOP     100
#define BAR_RIGHT   400
#define BAR_BOTTOM  175
#define HINT_LEFT   170
#define HINT_TOP    110
#define INPUT_MID   275
#define INPUT_LEFT	180
#define INPUT_TOP   150
{
	/* input here */
	char temp[30],yn;
	setfillstyle(SOLID_FILL,YELLOW);
	bar(BAR_LEFT,BAR_TOP,BAR_RIGHT,BAR_BOTTOM);
	setcolor(RED);
	do
	{
		if(workersum>=MAX_WORKERS)
		{
			hz(HINT_LEFT,HINT_TOP,15,1,RED,"     职工已满");
			keyreg();
			return;
		}
		pworker[workersum]=(WORKERTYPE*)malloc(sizeof(WORKERTYPE));

		do
		{
			hz(HINT_LEFT,HINT_TOP,15,1,RED," 请输入职工号(最长6位):");
			rectangle(INPUT_MID-6*4-2,INPUT_TOP-2,INPUT_MID+6*4+1,INPUT_TOP+9);
			ginput(INPUT_MID-6*4,INPUT_TOP,6,0,YELLOW,TEXT_COLOR,temp);
			bar(BAR_LEFT,BAR_TOP,BAR_RIGHT,BAR_BOTTOM);
		}while(!strlen(temp));
		pworker[workersum]->num=atol(temp);


		do
		{
			hz(HINT_LEFT,HINT_TOP,15,1,RED,"   请输入职工姓名:");
			rectangle(INPUT_MID-19*4-2,INPUT_TOP-2,INPUT_MID+19*4+1,INPUT_TOP+9);
			ginput(INPUT_MID-19*4,INPUT_TOP,19,1,YELLOW,TEXT_COLOR,temp);
			bar(BAR_LEFT,BAR_TOP,BAR_RIGHT,BAR_BOTTOM);
		}while(!strlen(temp));
		strcpy(pworker[workersum]->name,temp);


		do
		{
			hz(HINT_LEFT,HINT_TOP,15,1,RED," 请输入职工性别(m/f):");
			rectangle(INPUT_MID-4-2,INPUT_TOP-2,INPUT_MID+4+1,INPUT_TOP+9);
			ginput(INPUT_MID-4,INPUT_TOP,1,1,YELLOW,TEXT_COLOR,temp);
			bar(BAR_LEFT,BAR_TOP,BAR_RIGHT,BAR_BOTTOM);
		}while(*temp!='m'&&*temp!='M'&&*temp!='f'&&*temp!='F');
		pworker[workersum]->sex=toupper(*temp);

		do
		{
			hz(HINT_LEFT,HINT_TOP,15,1,RED,"   请输入职工年龄:");
			rectangle(INPUT_MID-2*4-2,INPUT_TOP-2,INPUT_MID+2*4+1,INPUT_TOP+9);
			ginput(INPUT_MID-2*4,INPUT_TOP,2,0,YELLOW,TEXT_COLOR,temp);
			bar(BAR_LEFT,BAR_TOP,BAR_RIGHT,BAR_BOTTOM);
		}while(atoi(temp)>55||atoi(temp)<18);
		pworker[workersum]->age=atoi(temp);

		do
		{
			hz(HINT_LEFT,HINT_TOP,15,1,RED,"   请输入职工月薪:");
			rectangle(INPUT_MID-5*4-2,INPUT_TOP-2,INPUT_MID+5*4+1,INPUT_TOP+9);
			ginput(INPUT_MID-5*4,INPUT_TOP,5,0,YELLOW,TEXT_COLOR,temp);
			bar(BAR_LEFT,BAR_TOP,BAR_RIGHT,BAR_BOTTOM);
		}while(atol(temp)<200);
		pworker[workersum]->salary=atol(temp);

		workersum++;

		hz(HINT_LEFT,HINT_TOP,15,1,RED,"   继续吗?(y/n)");
		yn=getch();
		bar(BAR_LEFT,BAR_TOP,BAR_RIGHT,BAR_BOTTOM);

	}while(yn!='n'&&yn!='N');
	savedflag=0;/*由于要输入资料,置为“未存”*/
}

/*列出信息函数,非按钮功能*/
void mylist(int total,WORKERTYPE* p[])
{
	int i,tempy;
	char temp[55]={'\0'};
	clearviewport();
	hz(75,30,15,1,YELLOW,
				"职工号   姓名          性别 年龄  月薪 ");
	setcolor(LIGHTRED);
	for(i=0;i<total;i++)
	{	
		tempy=(i*9)%243+52;
		sprintf(temp,"%-6lu     %-19s     %c    %2u     %-5lu",
						p[i]-> num,
						p[i]-> name,
						p[i]-> sex,
						p[i]-> age,
						p[i]-> salary
				);
		outtextxy(75,tempy,temp);
		if((i+1)%27==0) 
		{
			outtextxy(260,300,"Press any key to continue...");
			keyreg();
			setfillstyle(SOLID_FILL,BK_COLOR);
			bar(74,50,500,320);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -