智能人员考勤系统(C语言)
/*智能人员考评系统*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include<string.h>
void SaveStaff();
void ReadStaff();
//typedef struct sNode Node;
typedef struct staff{
char name[20];
char id[10];
int sex;
int grade;
char function[20];
void (*func)(void *g_pHead );
}Staffes;
typedef struct sNode
{
Staffes stu; //员工信息
struct sNode* pNext; //指向下一个员工
}Node;
Node *g_pHead =NULL; //定义第一个员工
void menu(){
printf(" 智能人员考评系统 \n");
printf("----------------------------\n");
printf("|1、添加员工信息 |\n");
printf("|2、保存所有员工信息 |\n");
printf("|3、查询员工信息 |\n");
printf("|4、删除员工信息 |\n");
printf("|5、修改员工信息 |\n");
printf("|6、职能施展 |\n");
printf("|7、退出 |\n");
printf("----------------------------\n");
printf("请输入相应的序号选择! \n");
}
//添加员工
void addStaff(){
system("cls");
ReadStaff();
//SaveStaff();
printf("请输入员工 工号 姓名 性别 评分 职能\n");
Node* p;
p = g_pHead;
while (g_pHead !=NULL &&p->pNext != NULL)
{
p = p->pNext;
}
Node* pNewNode = (Node*)malloc(sizeof(Node));//为新添加的员工分配空间
pNewNode->pNext = NULL;
if (g_pHead == NULL)
{
g_pHead = pNewNode;
p = g_pHead;
}
else
{
p->pNext = pNewNode;//p的下一个节点为pNewNode
}
//scanf("%s %s %d %d %s",pNewNode->stu.id,pNewNode->stu.name,&pNewNode->stu.sex,&pNewNode->stu.grade,pNewNode->stu.function);
printf("工号:");
scanf("%s",pNewNode->stu.id);
printf("姓名:");
scanf("%s",pNewNode->stu.name);
printf("性别:");
scanf("%d",&pNewNode->stu.sex);
printf("评分:%d\n",pNewNode->stu.grade=0);
printf("职能:");
scanf("%s",pNewNode->stu.function);
printf(" \n数据添加成功....\n\n\n");
system("pause");
system("cls");
}
//保存
void SaveStaff()
{
system("cls");
FILE *pFile = fopen("staff.txt", "w");
if (pFile ==0)
{
printf("打开文件失败");
return;
}
//写入数据
Node *p;
p = g_pHead;
while (p != NULL)
{
fprintf(pFile,"%s %s %d %d %s\n",p->stu.id,p->stu.name,p->stu.sex,p->stu.grade,p->stu.function);
p =p->pNext;
}
printf(" \n\n数据保存成功\n\n\n");
fclose(pFile);
system("pause");
system("cls");
}
//读员工信息
void ReadStaff()
{
system("cls");
Node *p,*p2;
p = p2 = g_pHead;
while(p2 != NULL)//先吧链表中的数据删掉
{
p=p->pNext;
free(p2);
p2=p;
}
g_pHead = NULL;
FILE *pFile = fopen("staff.txt","r+");
//printf("打开文件");
if(pFile ==0)
{
printf("打开文件失败");
return;
}
//读取数据
while (!feof(pFile))
{
Node *pTemp = (Node*)malloc(sizeof(Node));
fscanf(pFile, "%s %s %d %d %s\n", &pTemp->stu.id, &pTemp->stu.name, &pTemp->stu.sex, &pTemp->stu.grade,&pTemp->stu.function);
// printf("duqu");
//创建链表
if(g_pHead ==NULL)
{
g_pHead = pTemp;
p = g_pHead;
}
else
{
p->pNext =pTemp; //p的下一个节点为temp
p = p->pNext;
p->pNext = NULL;
}
}
fclose(pFile);
}
//查找员工
Node* FindStaff()
{
system("cls");
char StaNum[20];
printf("请输入您要查找的员工号: ");
scanf("%s",StaNum);
ReadStaff(); //把文件的东西都取到链表中
Node* p;
p = g_pHead;
while (p != NULL)
{
if (strcmp(p->stu.id,StaNum)==0)
{
return p;
}
p = p->pNext;
}
//链表完没有找到信息
if (p == NULL)
{
printf("没有员工的信息\n");
return NULL;
}
return NULL;
}
//删除
void DeleteStaff()
{
system("cls");
char StaNum[20];
printf("请输入要删除员工工号:\n");
scanf("%s",StaNum);
ReadStaff(); //把文件内容读入链表
Node *p,*p2;
p= g_pHead;
if(strcmp(g_pHead->stu.id,StaNum)==0)//判断是不是头结点
{
p2 = g_pHead;
g_pHead=g_pHead->pNext;
free(p2);
return;
}
while(p->pNext != NULL)//不是头节点时
{
if(strcmp(p->pNext->stu.id,StaNum)==0)
{
p2=p->pNext;
p->pNext =p->pNext->pNext;//直接把要删除的跳过
free(p2);
return;
}
p=p->pNext;
if(p->pNext == NULL)//看看是不是链表尾
{
break;
}
}
if (p->pNext == NULL)
{
printf("没有该员工信息\n");
}
}
//修改员工信息
void modifystaff(){
system("cls");
char StaNum[20];
printf("请输入您要修改的员工工号");
scanf("%s",StaNum);
ReadStaff(); //把文件内容读入链表
Node* p;
p = g_pHead;
while(p != NULL){
if(strcmp(p->stu.id,StaNum)==0){
printf("工号:%s\t姓名:%s\t性别:%d\t评分%d\t职能:%s\n\n",p->stu.id, p->stu.name, p->stu.sex, p->stu.grade,p->stu.function);
printf("请输入要修改的信息:工号 姓名 性别 分数 职能\n\n");
printf("工号:%s\n",p->stu.id);
printf("姓名:");
scanf("%s",p->stu.name);
printf("性别:%d\n评分:%d\n职能:",p->stu.sex,p->stu.grade);
scanf("%s",p->stu.function);
printf("\n修改成功\n");
break;
}
p=p->pNext;
}
if(p== NULL)
{
printf("没有该员工信息!\n");
}
system("pause");
system("cls");
}
//考核打分
void DaFen(void *g_pHead){
system("cls");
ReadStaff(); //把文件内容读入链表
Node* p;
p = (Node*)g_pHead;
while (p != NULL){
printf("工号:%s\t姓名:%s\t性别:%d\t职能:%s\t评分:",p->stu.id,p->stu.name,p->stu.sex,p->stu.function);
scanf("%d",&p->stu.grade);
p = p->pNext;
}
SaveStaff();
// return 0;
}
//清楚打分
void Clear(void *g_pHead){
system("cls");
ReadStaff(); //把文件内容读入链表
Node* p;
p = (Node*)g_pHead;
while (p != NULL)
{
printf("工号:%s\t姓名:%s\t性别:%d\t职能:%s\t评分:",p->stu.id,p->stu.name,p->stu.sex,p->stu.function);
p->stu.grade=0;
printf("%d\n",p->stu.grade);
p = p->pNext;
}
system("pause");
SaveStaff();
// return 0;
}
//排序
void Sort(void *g_pHead){
system("cls");
ReadStaff(); //把文件内容读入链表
int i,j,cout=0;
Node* p;
Staffes t;
p =(Node*) g_pHead;
while (p != NULL)
{
cout++;
// printf("工号:%s\t姓名:%s\t性别:%d\t评分%d\t职能:%s\n",p->stu.id, p->stu.name, p->stu.sex, p->stu.grade,p->stu.function);
p = p->pNext;
}
Node* b,* d;
for(b=(Node*) g_pHead;b!=NULL;b = b->pNext){
for(d=b;d!=NULL;d = d->pNext){
if(b->stu.grade>d->stu.grade){
t = d->stu;
d->stu=b->stu;
b->stu=t;
}
}
}
p =(Node*) g_pHead;
while (p != NULL)
{
cout++;
printf("工号:%s\t姓名:%s\t性别:%d\t评分%d\t职能:%s\n",p->stu.id, p->stu.name, p->stu.sex, p->stu.grade,p->stu.function);
p = p->pNext;
}
system("pause");
SaveStaff();
// return 0;
}
//计算分数
void Average(void *g_pHead){
// system("cls");
int sum=0;
int cout=0;
ReadStaff(); //把文件内容读入链表
Node* p;
p =(Node*) g_pHead;
while (p != NULL)
{
sum=sum+p->stu.grade;
cout++;
p = p->pNext;
}
printf("所有员工总分数为: %d\n",sum);
printf("所有员工总人数为: %d\n",cout);
printf("所有员工平均分数为:%d\n\n",sum/cout);
system("pause");
system("cls");
//SaveStaff();
//return 0;
}
//职能施展
void operation(){
system("cls");
char StaNum[20];
printf("请输入您的工号:");
scanf("%s",StaNum);
char dafen[]="dafen";
char sclear[]="clear";
char sort[]="sort";
char average[]="average";
ReadStaff(); //把文件内容读入链表
Node* p;
p = g_pHead;
while(p!= NULL){
if(strcmp(p->stu.id,StaNum)==0){
if(strcmp(p->stu.function,dafen)==0)
{
p->stu.func=DaFen;
p->stu.func(g_pHead);
break;
}
else if(strcmp(p->stu.function,sclear)==0)
{
p->stu.func=Clear;
p->stu.func(g_pHead);
break;
}
else if(strcmp(p->stu.function,sort)==0)
{
p->stu.func=Sort;
p->stu.func(g_pHead);
break;
}
else if(strcmp(p->stu.function,average)==0){
p->stu.func=Average;
p->stu.func(g_pHead);
break;
}
else
{
printf("抱歉您没有特殊职能\n");
break;
}
}
p=p->pNext;
}
if(p == NULL)
{
printf("工号输入错误\n");
}
}
int main()
{
while(1){
// system("cls");
menu();
char ch;
ch = _getch();
switch (ch)
{
case '1': //1、添加员工信息
addStaff();
break;
case '2': //2、保存员工信息
SaveStaff();
break;
case '3': //3、查询员工信息
{
Node *pNode = FindStaff();
if(pNode !=NULL)
{
printf("工号:%s\t姓名:%s\t性别:%d\t评分%d\t职能:%s\n\n\n",pNode->stu.id, pNode->stu.name, pNode->stu.sex, pNode->stu.grade,pNode->stu.function);
}
system("pause");
system("cls");
break;
}
case '4': //4、删除员工信息
DeleteStaff();
break;
case '5': //5、修改员工信息
modifystaff();
break;
case '6': //6、职能施展
operation();
break;
case '7': //7、退出
printf("\n\n 欢迎再次使用! \n\n\n");
exit(0);
break;
default:
printf("您的输入有误,请重新输入!\n");
break;
}
}
return 0;
}