#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dir.h>
#include <dos.h>
#define CURYEAR 2003
struct date1
{
int year;
int month;
int day;
};
struct employee
{
char name[20]; /* 职工姓名 */
int number; /* 职工序号 */
char sex; /* 职工性别 */
char rank; /* 职工职称(p->教授,l->讲师,a->副教授 */
char department[20]; /* 职工所在部门 */
int money; /* 职工工资 */
struct date1 birthday; /* 职工生日 */
struct date1 workdate; /* 职工工作时间 */
struct employee *next; /* 结点指针 */
};
char *sFileName = "e://personal.txt";
FILE *pFile;
char cover( void );
int input(void);
int output(void);
int find(void);
int erase(void);
int add(void);
int modify(void);
int statistic(void);
void help( void );
int quit(void);
int main(void)
{
int iRCover;
do
{
iRCover = cover();
clrscr();
switch (iRCover)
{
case 5:/* 输入 */
input();
break;
case 6:/* 输出 */
output();
break;
case 10:/* 查找(按姓名 || 按学号) */
find();
break;
case 7:/* 修改 */
modify();
break;
case 9:/* 删除 */
erase();
break ;
case 8:/* 添加 */
add();
break;
case 11:/* 统计 */
statistic();
break;
case 12:/* 帮助 */
help();
break;
case 13:/* 退出 */
system("cls");
exit(1);
break;
}/* end of switch ... case */
}while ( 1 );/* end of do ... while */
}
int GetKey( void ) /*获得键盘扫描码*/
{
union REGS rg;
rg.h.ah = 0;
int86(0x16, &rg, &rg);
return rg.h.ah;
}
void drawDia( void ) /*输出红色对话框*/
{
window(25, 8, 55, 16);
textbackground(RED);
textcolor(WHITE);
clrscr();
}
void revert( void )/* 还原屏幕,消除对话框带来的负面影响 */
{
window(1, 1, 80, 25);
textbackground(BLACK);
textcolor(WHITE);
clrscr();
}
char cover( void )/*功能选择面板*/
{
int key = 0;
int down = 5;
revert();
gotoxy(17, 5);
printf(" I -----------> input the information ");
gotoxy(17, 6);
printf(" O -----------> output all information ");
gotoxy(17, 7);
printf(" M -----------> modify the information ");
gotoxy(17, 8);
printf(" A -----------> add the information ");
gotoxy(17, 9);
printf(" E -----------> delete the information ");
gotoxy(17, 10);
printf(" F -----------> find the information ");
gotoxy(17, 11);
printf(" S -----------> statistic of the information");
gotoxy(17, 12);
printf(" H -----------> help of the system ");
gotoxy(17, 13);
printf(" Q -----------> quit the system ");
gotoxy(19, 5);
do{
key = GetKey();
if (key == 80)/* 向下 */
{
down = down == 13 ? 5 : down + 1;
gotoxy(19, down);
}
else if (key == 72)/* 向上 */
{
down = down == 5 ? 13 : down - 1;
gotoxy(19, down);
}
else if (key == 28)/* 回车 */
{
return down;
}
}while ( 1);
}/*end of cover(...)*/
/*******************************************************************************
*输入函数
*完成功能:让用户在提示下输入系统所需信息(所有信息都暂存于链表中)
完成对职工信息的录入,并将其存放到指定文件中。
*******************************************************************************/
int input(void)
{
void ReadTab(void);
void ReadData(struct employee *pLNode);
struct employee *pLNode = NULL;
int iTNumber = 0, i;
clrscr();
if (searchpath(sFileName) != NULL)/* 已存在文件时,input()函数不可用 */
{
drawDia();
gotoxy(1, 2);
printf("There is almost a exist file!");
gotoxy(1, 4);
printf("You can't use this function!");
gotoxy(1, 6);
printf("Press any key to return!");
getch();
return (0);
}
/* 要求输入信息总数 */
drawDia(); /* 一个对话框 */
gotoxy(1, 2);/* 注意gotoxy(x, y)中坐标为相对坐标(相对最近一个window()) */
printf("Please input the total number");
gotoxy(1, 4);
printf("of the workers' information:");
gotoxy(1, 6);
scanf("%d", &iTNumber);
revert();/*还原屏幕*/
pLNode = (struct employee *)malloc(sizeof(struct employee));
/* 读取数据并存盘 */
pFile = fopen(sFileName, "wb");
for (i = 0; i < iTNumber; i++)
{
clrscr();
gotoxy(4, 3);
printf("The number of information you are inputting is %d !", i + 1);
ReadTab();
ReadData(pLNode);
fwrite(pLNode, sizeof(struct employee), 1, pFile);/* 写文件 */
}
fclose(pFile);
free(pLNode);
pLNode = NULL;/* 防止野指针 */
return (1);
}
/**************************
*打印输入提示符
*************************/
void ReadTab(void)
{
gotoxy(8, 5);
puts("Name :");/* string */
gotoxy(8, 7);
puts("Number :"); /* int */
gotoxy(8, 9);
puts("Sex(F&M) :"); /* char */
gotoxy(8, 11);
puts("Rank(P&L&A) :"); /* char */
gotoxy(8, 13);
puts("Department :"); /* string */
gotoxy(8, 15);
puts("Wage :"); /* int */
gotoxy(8, 17);
puts("Birthday");
gotoxy(8, 18);
puts("(yyyy mm dd) :"); /* struct date */
gotoxy(8, 20);
puts("WorkDate");
gotoxy(8, 21);
puts("(yyyy mm dd) :"); /* struct date */
}
/*******************
*得到输入数据
*******************/
void ReadData(struct employee *pLNode)
{
gotoxy(14, 5);
scanf("%s", pLNode->name);
gotoxy(16, 7);
scanf("%d", &pLNode->number);
gotoxy(18, 9);
getchar(); /* 取消回车符的录入 */
pLNode->sex = getchar();
gotoxy(21, 11);
getchar();
pLNode->rank = getchar();
gotoxy(20, 13);
scanf("%s", pLNode->department);
gotoxy(14, 15);
scanf("%d", &pLNode->money);
gotoxy(17, 17);
scanf("%d%d%d", &pLNode->birthday.year, &pLNode->birthday.month, &pLNode->birthday.day);
gotoxy(17, 20);
scanf("%d%d%d", &pLNode->workdate.year, &pLNode->workdate.month, &pLNode->workdate.day);
}
/************************
*格式化输出数据
************************/
int output()
{
int display(struct employee *pLNode, int line);
int line = 3;
struct employee *pLNode = NULL;
clrscr();
pFile = fopen(sFileName, "rb");
if (pFile == NULL)
{
printf("can't open the file!");
puts("press any key to return!");
return (0);
}
gotoxy(1, 2);
puts("Name");
gotoxy(15, 2);
puts("Number");
gotoxy(25, 2);
puts("Sex");
gotoxy(30, 2);
puts("Rank");
gotoxy(37, 2);
puts("Department");
gotoxy(50, 2);
puts("Wage");
gotoxy(58, 2);
puts("Birthday");
gotoxy(70, 2);
puts("WorkDate");
/*开辟缓冲空间 */
pLNode = (struct employee *)malloc(sizeof(struct employee));
while (!feof(pFile))
{
/* 读文件 */
if (fread(pLNode, sizeof(struct employee), 1, pFile) != 1)/* 1是所读数据快的个数 */
break;
/* 显示 */
display(pLNode, line);
++line;
}
fclose(pFile);
gotoxy(2, 25);
printf("Press any key to return!");
getch();
free(pLNode);
pLNode = NULL;/* 防止野指针 */
return (1);
}
int display(struct employee *pLNode, int line)
{
gotoxy(1, line);
printf("%s", pLNode->name);
gotoxy(15, line);
printf("%d", pLNode->number);
gotoxy(25, line);
printf("%c", pLNode->sex);
gotoxy(30, line);
printf("%c", pLNode->rank);
gotoxy(37, line);
printf("%s", pLNode->department);
gotoxy(50, line);
printf("%d", pLNode->money);
gotoxy(58, line);
printf("%d/%d/%d", pLNode->birthday.year, pLNode->birthday.month, pLNode->birthday.day);
gotoxy(70, line);
printf("%d/%d/%d", pLNode->workdate.year, pLNode->workdate.month, pLNode->workdate.day);
}
/*******************************************************************************
*查找函数
*调用方式:find();
*函数功能:按职工的序号进行查找,若找到,则输出该同志的相关信息;
否则,输出提示信息(如:not found!)。
*******************************************************************************/
int find( void )
{
void show(struct employee *pLNode);
void findDia(int *flag, char *sTmpName, int *iTmpNum);
struct employee *pLNode;
int iTmpNum = 0, flag = 3;
char *sTmpName = 0;
findDia(&flag, sTmpName, &iTmpNum);
pFile = fopen(sFileName, "rb");
pLNode = (struct employee *)malloc(sizeof(struct employee));
while (!feof(pFile))
{
/* 读文件 */
if (fread(pLNode, sizeof(struct employee), 1, pFile) != 1)
break;
/* 按姓名 */
if ((flag == 0) && (strcmp(sTmpName, pLNode->name) == 0))
{
flag = 2;
show(pLNode);
}
/* 按学号 */
if ((flag == 1) && (iTmpNum == pLNode->number))
{
flag = 2;
show(pLNode);
}
}/*end of while*/
fclose(pFile);
if (flag != 2)/* 没找到 */
{
drawDia();
gotoxy(1, 2);
printf("No that person!");
gotoxy(1, 4);
printf("The file don't include that");
gotoxy(1, 6);
printf(" information!");
gotoxy(1, 8);
printf("Press any key to return!");
getch();
pLNode = NULL;
}
free(pLNode);
pLNode = NULL;/* 防止野指针 */
return (1);
}
/*****************************************
*要求用户输入查找时信息的对话框
*因为本函数中用到了输入输出参数,所以不用返回值
*******************************************/
void findDia(int *flag, char *sTmpName, int *pTmpNum)
{
char *sFindFiled = 0;
drawDia();
gotoxy(1, 2);
printf("Sort for name or number?");
scanf("%s", sFindFiled);
if (strcmp(sFindFiled, "name") == 0)
(*flag) = 0;
else if (strcmp(sFindFiled, "number") == 0)
(*flag) = 1;
else
return;
if ((*flag) == 0)/* 按名字 */
{
gotoxy(1, 4);
printf("Please input name:");
scanf("%s", sTmpName);
}
else/* 按学号 */
{
gotoxy(1, 4);
printf("Please input number:");
scanf("%d", pTmpNum);
}
}
/*************************
*显示pLNode中的内容
*************************/
void show(struct employee *pLNode)
{
revert();
/* 输出表单 */
gotoxy(3, 2);
puts("The information of your wonder is : ");
gotoxy(4, 4);
puts("Name :");
gotoxy(4, 6);
puts("Number :");
gotoxy(4, 8);
puts("Sex :");
gotoxy(4, 10);
puts("Rank :");
gotoxy(4, 12);
puts("Department :");
gotoxy(4, 14);
puts("Wage :");
gotoxy(4, 16);
puts("Birthday :");
gotoxy(4, 18);
puts("Workdate :");
/* 输出数据 */
gotoxy(10, 4);
printf("%s", pLNode->name);
gotoxy(12, 6);
printf("%d", pLNode->number);
gotoxy(9, 8);
printf("%c", pLNode->sex);
gotoxy(10, 10);
printf("%c", pLNode->rank);
gotoxy(16, 12);
printf("%s", pLNode->department);
gotoxy(10, 14);
printf("%d", pLNode->money);
gotoxy(14, 16);
printf("%d/%d/%d", pLNode->birthday.year, pLNode->birthday.month, pLNode->birthday.day);
gotoxy(14, 18);
printf("%d/%d/%d", pLNode->workdate.year, pLNode->workdate.month, pLNode->workdate.day);
gotoxy(4, 25);
puts("Press any key to continue!");
getch();
return;
}
/*******************************************************************************
*修改函数
*调用方式:modify();
*函数功能:对某职工的某条信息进行修改。要确定在数据文件中有该同志的信息资料,
若查找不到,则输出提示信息(如:not found!)。
*******************************************************************************/
int modify( void )
{
void show(struct employee *pLNode);
struct employee *pLNode = NULL, *pLHead = NULL, *pLModify = NULL;
char *sTmpFiled = 0;/* 存放将要修改的域 */
char *sTmpName = 0;/* 存放将要修改的姓名 */
int flag = 0;/* 是否存在的标志 */
clrscr();
drawDia();
gotoxy(1, 1);
printf("Who do you want to modify?");
gotoxy(1, 2);
scanf("%s", sTmpName);
pFile = fopen(sFileName, "rb");
/* 建立首节点 */
pLHead = (struct employee *)malloc(sizeof(struct employee));
pLHead->next = NULL;
while (!feof(pFile))
{
pLNode = (struct employee *)malloc(sizeof(struct employee));
/* 读文件 */
if (fread(pLNode, sizeof(struct employee), 1, pFile) != 1)
break;
/* 建立链表 */
pLNode->next = pLHead->next;
pLHead->next = pLNode;
if (strcmp(sTmpName, pLNode->name) == 0)
{
flag = 1;/* 找到的话,置标志 */
pLModify = pLNode;/* 并赋值指针 */
}
}/*end of while*/
fclose(pFile);
if (flag == 1)/* 可以修改 */
{
char *sFiled[8] = {"name", "number", "sex", "rank", "department", "wage"
"birthday", "workdate"};
int i = 0;
gotoxy(1, 3); /* 输出对话框 */
printf("And which filed ?");
gotoxy(1, 4);
printf("(name number sex rank workdate");
gotoxy(1, 5);
printf("department wage birthday)");
gotoxy(18, 3);
scanf("%s", sTmpFiled);
gotoxy(1, 6);
printf("The current data is : ");
gotoxy(1, 7);
while (strcmp(sTmpFiled, sFiled[i]) != 0)/* 检验是哪个域 */
++i;
switch (i)
{
case 0: printf("%s", pLModify->name);
gotoxy(1, 8);
printf("Please input the new data :");
gotoxy(1, 9);
scanf("%s", pLModify->name);
break;
case 1: printf("%d", pLModify->number);
gotoxy(1, 8);
printf("Please input the new data :");
gotoxy(1, 9);
scanf("%d", &pLModify->number);
break;
case 2: printf("%c", pLModify->sex);
gotoxy(1, 8);
printf("Please input the new data :");
gotoxy(1, 9);
getchar();
pLModify->sex = getchar();
break;
case 3: printf("%c", pLModify->rank);
gotoxy(1, 8);
printf("Please input the new data :");
gotoxy(1, 9);
getchar();
pLModify->rank = getchar();
break;
case 4: printf("%s", pLModify->department);
gotoxy(1, 8);
printf("Please input the new data :");
gotoxy(1, 9);
scanf("%s", pLModify->department);
break;
case 5: printf("%d", pLModify->money);
gotoxy(1, 8);
printf("Please input the new data :");
gotoxy(1, 9);
scanf("%d", &pLModify->money);
break;
case 6: printf("%d/%d/%d", pLModify->workdate.year,
pLModify->workdate.month,
pLModify->workdate.day);
gotoxy(1, 8);
printf("Input new data:(yyyy mm dd)");
gotoxy(1, 9);
scanf("%d%d%d", &pLModify->workdate.year,
&pLModify->workdate.month,
&pLModify->workdate.day);
break;
case 7: printf("%d/%d/%d", pLModify->birthday.year,
pLModify->birthday.month,
pLModify->birthday.day);
gotoxy(1, 8);
printf("Input new data:(yyyy mm dd)");
gotoxy(1, 9);
scanf("%d%d%d", &pLModify->birthday.year,
&pLModify->birthday.month,
&pLModify->birthday.day);
break;
default: printf("No that filed!");
gotoxy(1, 8);
printf("Press any key to return!");
return (0);
}
}
else
{
gotoxy(1, 4);
printf("No this person!");
gotoxy(1, 6);
printf("Press any key to return!");
getch();
return (0);
}
revert();
/* 显示新信息 */
show(pLModify);
/* 存盘 */
pFile = fopen(sFileName, "wb");
pLNode = pLHead->next;
while (pLNode != NULL)
{
if (fwrite(pLNode, sizeof(struct employee), 1, pFile) != 1)
break;
pLNode = pLNode->next;
}
fclose(pFile);
/* 释放链表 */
free(pLHead);
pLHead = NULL;
return (1);
}
/*******************************************************************************
*添加函数
*调用方法:add();
*函数功能:在原数据文件中插入一条新职工信息。
*******************************************************************************/
int add( void )
{
void ReadData(struct employee *pLNode);
void ReadTab(void);
struct employee *pLNode = NULL, *pLHead = NULL;
pFile = fopen(sFileName, "rb");
/* 建立链表首节点 */
pLHead = (struct employee *)malloc(sizeof(struct employee));
pLHead->next = NULL;
while (!feof(pFile))
{
pLNode = (struct employee *)malloc(sizeof(struct employee));
if (fread(pLNode, sizeof(struct employee), 1, pFile) != 1)
break;
pLNode->next = pLHead->next;
pLHead->next = pLNode;
}
fclose(pFile);
/* 为新增节点开辟空间 */
pLNode = (struct employee *)malloc(sizeof(struct employee));
pLNode->next = pLHead->next;
pLHead->next = pLNode;
clrscr();
gotoxy(3, 3);
printf("Please input the information you want to add : ");
pLNode = pLHead->next;
ReadTab();
ReadData(pLNode);
pFile = fopen(sFileName, "wb");
pLNode = pLHead->next;
while (pLNode != NULL)
{
if (fwrite(pLNode, sizeof(struct employee), 1, pFile) != 1)
break;
pLNode = pLNode->next;
}
fclose(pFile);
free(pLHead);
pLHead = pLNode = NULL;
return (1);
}
/*******************************************************************************
*删除模块
*调用方式:erase();
*函数功能:输入要删除职工姓名,在数据文件中查找该职工信息,找到后删除;
若查找不到,则输出提示信息(如:not found!)。
*******************************************************************************/
int erase( void )
{
void show(struct employee *pLNode);
struct employee *pLNode = NULL, *pLHead = NULL;
char *sTmpName = 0;
int flag = 0;/* 是否存在的标志 */
clrscr();/* 使清屏 */
drawDia();
/* 输入要删除的职工姓名 */
gotoxy(1, 2);
printf("Who do you want to delete?");
gotoxy(1, 4);
scanf("%s", sTmpName);
if ((pFile = fopen(sFileName, "rb")) == NULL)
{
gotoxy(1, 5);
puts("Can't open the file!");
gotoxy(1, 6);
puts("Press any key to return!");
getch();
return (0);
}
pLHead = (struct employee *)malloc(sizeof(struct employee));
pLHead->next = NULL;
while (!feof(pFile))
{
int isFind = 0;
pLNode = (struct employee *)malloc(sizeof(struct employee));
if (fread(pLNode, sizeof(struct employee), 1, pFile) != 1)
break;
if (strcmp(sTmpName, pLNode->name) == 0)/* 存在此记录 */
{
flag = 1;/* 置标志1 */
isFind = 1;/*是否找到标志2*/
show(pLNode);/* 显示将要删除的信息 */
free(pLNode);
pLNode = NULL;
}
if (isFind != 1)/*若果找到,则直接删除节点*/
{
pLNode->next = pLHead->next;
pLHead->next = pLNode;
}
}
fclose(pFile);
if (flag != 1)/* 不存在 */
{
drawDia();
gotoxy(1, 2);
printf("No that person!");
gotoxy(1, 4);
printf("The file don't include that");
gotoxy(1, 6);
printf(" information!");
gotoxy(1, 8);
printf("Press any key to return!");
getch();
return (0);
}
revert();
gotoxy(3, 3);
printf("The information you want to delete is :");
/* 存盘 */
pFile = fopen(sFileName, "wb");
pLNode = pLHead->next;
while (pLNode != NULL)
{
if (fwrite(pLNode, sizeof(struct employee), 1, pFile) != 1)/* 存盘 */
break;
free(pLNode);
pLNode = pLNode->next;
}
fclose(pFile);
clrscr();
drawDia();
gotoxy(1, 2);
printf("The information has been ");
gotoxy(1, 4);
printf("deleted successfully!");
gotoxy(1, 8);
printf("Press any key to return!");
getch();
free(pLHead);
pLHead = pLNode = NULL;
return (1);
}
/*******************************************************************************
*统计函数
*调用方式:statistic();
*函数功能:做职工卡片。
统计教授(P)、讲师(L)、副教授的人数(A)
计算并打印今年的退休人员(男60 ,女55)及相关信息
*******************************************************************************/
int statistic(void)
{
void show(struct employee *pLNode);
struct employee *pLNode, *pLHead;
int nProfessor = 0, nLecture = 0, nAdjunct = 0;
int nOldMen = 0, nOldWomen = 0;
pFile = fopen(sFileName, "rb");/*读文件并建立链表*/
pLHead = (struct employee *)malloc(sizeof(struct employee));
pLHead->next = NULL;
while (!feof(pFile))
{
pLNode = (struct employee *)malloc(sizeof(struct employee));
if (fread(pLNode, sizeof(struct employee), 1, pFile) != 1)
break;
pLNode->next = pLHead->next;
pLHead->next = pLNode;
}
fclose(pFile);
pLNode = pLHead->next; /*遍历链表对数据进行统计*/
while (pLNode != NULL)
{
/* 功能一 */
if (pLNode->rank == 'p' || pLNode->rank == 'P')
{
++nProfessor;
}
else if (pLNode->rank == 'l' || pLNode->rank == 'L')
{
++nLecture;
}
else if (pLNode->rank == 'a' || pLNode->rank == 'A')
{
++nAdjunct;
}
/* 功能二 */
if ((pLNode->sex == 'm' || pLNode->sex == 'M') &&
(CURYEAR - pLNode->birthday.year > 60))
{
++nOldMen;
}
if ((pLNode->sex == 'f' || pLNode->sex == 'F') &&
(CURYEAR - pLNode->birthday.year > 55))
{
++nOldWomen;
}
pLNode = pLNode->next;
}
clrscr();
/* 显示 */
drawDia();
gotoxy(1, 1);
printf("There are the statistic: ");
gotoxy(1, 2);
printf("Professor : %d", nProfessor);
gotoxy(1, 3);
printf("Lecture : %d", nLecture);
gotoxy(1, 4);
printf("Adjunct : %d", nAdjunct);
gotoxy(1, 5);
printf("This year there will be ");
gotoxy(1, 6);
printf("%d men retire", nOldMen);
gotoxy(1, 7);
printf("And %d women retire!", nOldWomen);
gotoxy(1, 8);
printf("Press any key to continue");
getch();
revert();
/* 职工卡片 */
pLNode = pLHead->next;
while (pLNode != NULL)
{
clrscr();
gotoxy(4, 1);
puts("There are the employees' kard!");
show(pLNode);
pLNode = pLNode->next;
}
/* 释放链表 */
pLNode = pLHead->next;
while (pLNode != NULL)
{
free(pLNode);
pLNode = pLNode->next;
}
pLNode = pLHead = NULL;
return (1);
}
/*******************************************************************************
*帮助函数
*调用方式:help();
*函数功能:为用户提供必要的帮助说明,如:功能键的说明,
关于系统的说明,
系统的使用方式等等
*******************************************************************************/
void help( void )
{
FILE *pHelp;
int line = 2, col = 2;
char cTemp;
clrscr();
pHelp = fopen("e://endtemp//help.txt", "rb");
if(pHelp == NULL)
{
printf("this file cannot open/n");
getch();
return;
}
while (((cTemp = fgetc(pHelp)) != EOF) && (col < 80) && (line < 23))
{
if (cTemp == '/n')
{
line++;
col = 2;
}
gotoxy(col++, line);
putchar(cTemp);
}
gotoxy(2, 25);
printf("Press any key to return!");
getch();
fclose(pHelp);
return;
}