职工工资管理系统
暑假学校实习,要求做个工资管理系统。写这篇博客来记录下过程,以及在做的过程中的一些总结!提前说下这是基于C语言写的管理系统!
话不多说,先上图介绍下有哪些功能!
1.首先来开下数据结构吧
#define N 100
struct Worker
{char workernum[10];//员工编号char workername[20];//员工姓名int basicwage;//员工基本工资int allowance;//津贴int Bonus;//员工工资int totalwage;//员工总工资
}Worker_Infor[N];
struct Worker temp;
int m;//定义全局变量控制人数
结构体数组这块没啥太多东西,一些员工的基本属性就OK了,其实在这里本来想用动态数组的但是C语言这学期学完后没往深了看,就没使用动态赋值!?
2. ? 看看第一个功能吧,信息录入。这个功能比较好弄,而且方法也比较多。
那就不多说了,上代码!
void Input_Infor()
{char c,nums[20];int i,j,flag=1;if(m==0)i=0;else i=m;printf(" ------------------------| 员工信息录入 |------------------------------\n");printf(" ======================================================================\n");while(1){printf(" || -第%d位职工的编号:",i+1);TO:if(m==0){gets(Worker_Infor[m].workernum);}else{gets(nums);for(j=0;j<m;j++){if(strcmp(nums,Worker_Infor[j].workernum)==0){printf(" || -已存在!请重输:");goto TO;}else{strcpy(Worker_Infor[i].workernum,nums);}}}printf("\n || -工号为%s职工的姓名:",Worker_Infor[i].workernum);gets(Worker_Infor[i].workername);printf("\n || -%s职工基本工资:",Worker_Infor[i].workername);while(1){scanf("%d",&Worker_Infor[i].basicwage);if(Worker_Infor[i].basicwage>=0) break;else printf("\n || 输入错误,请重试:");}printf("\n || -%s职工的津贴:",Worker_Infor[i].workername);while(1){scanf("%d",&Worker_Infor[i].allowance);if(Worker_Infor[i].allowance>=0) break;else printf("\t输入错误,请重试:");}printf("\n || -%s职工的奖金:",Worker_Infor[i].workername);while(1){scanf("%d",&Worker_Infor[i].Bonus);if(Worker_Infor[i].Bonus>=0) break;else printf("\t输入错误,请重试:");}i++;m=i;getchar();printf("\n || -是否继续输入:Y or N 或者 y or n:");scanf("%c",&c);getchar();if(c=='N'||c=='n'){system("cls");printf(" =========================================================================================\n");printf(" || ||\n");printf(" || 感谢您的使用! ||\n");printf(" || ||\n");printf(" || 信息更新完毕请保存! ||\n");printf(" || ||\n");printf(" || - @author by---Zdde丶 - ||\n");printf(" =========================================================================================\n");break;}}
}
这部分有一个点,就是在录入编号的时候,要检查数据文件里面是否有重复的。开始弄这个时觉得这个功能很简单,想着用个while(1)循环里面套for循环,遍历结构体数组。也就是把现在代码里面的goto换成了while(1)来做的,结果出现了一个很不可思议的问题,就是if else语句居然if和else里面的东西同时满足并且执行了。后来也发现了问题所在。但是这个问题真的很隐蔽。
3. 再就是保存数据到硬盘的功能了
保存文件就用到了C语言的文件管理。课堂上老师也就一笔带过,再弄这块也是疯狂的百度资料。
主要是查询fread fwrite ,fscanf以及fprintf这四个函数的用法。想来想去还是用了fscanf和fprintf这两个。毕竟是刚自学不久,我就不讲二者用法了。上代码看看这个功能吧
void Save_Infor()
{int i,n=0;FILE *pk;FILE *pf;Cal_Wages();pk=fopen("Worker.txt","w");pf=fopen("sumpeo.txt","w");n += m;//这里其实是多余的直接使用m即可fprintf(pf,"%d",n);if(pk==NULL||pf==NULL);{printf("文件打开失败\n");exit(0);}for(i=0;i<m;i++){fprintf(pk,"\t%10s\t%8s\t%10d\t%8d%8d%6d\t\n",Worker_Infor[i].workernum,Worker_Infor[i].workername,Worker_Infor[i].basicwage,Worker_Infor[i].allowance,Worker_Infor[i].Bonus,Worker_Infor[i].totalwage);}fclose(pf);fclose(pk);printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 文件保存成功 ||\n");printf(" || ||\n");printf(" =======================================================================================\n");}
保存文件这块,也就这点代码,但是真的让我查了很久的资料!
4.数据载入
功能快捷键
int Up_infor()
{FILE *fp;FILE *pk;int i;fp = fopen("Worker.txt","r");pk = fopen("sumpeo.txt","rb");if(pk==NULL){printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 文件打开失败 ||\n");printf(" || ||\n");printf(" =======================================================================================\n");}else{fscanf(pk,"%d",&m);//printf("%d",m);}for(i=0;i<m;i++){fscanf(fp,"\t%10s\t%8s",Worker_Infor[i].workernum,Worker_Infor[i].workername);fscanf(fp,"\t%10d\t%8d%8d%6d",&Worker_Infor[i].basicwage,&Worker_Infor[i].allowance,&Worker_Infor[i].Bonus,&Worker_Infor[i].totalwage);}return m;fclose(fp);fclose(pk);}
这里使用了两个txt文件,一个是保存人数,一个是保存每个人的信息。这里函数返回了一个整型值m,为的就是开始定义的全局变量m能够起作用。我想的是,将人数保存在单独的文件中,每次从文件中读取,就可以在全局使用这个人数了,因为人数就可以控制for循环的范围,便于遍历数组。
5.这里也就不一个一个去说了。直接上全部代码和调试截图
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define N 100
void Input_Infor();
void Cal_Wages();
void sort_Wage();
void Save_Infor();
void Menu();
int Up_infor();
int Search_Name();
int Search_Num();
void Modify_Worker();
void Print_Worker(int i);
int Search_Worker();
int Worker_del();
struct Worker
{char workernum[10];//员工编号char workername[20];//员工姓名int basicwage;//员工基本工资int allowance;//津贴int Bonus;//员工工资int totalwage;//员工总工资
}Worker_Infor[N];
struct Worker temp;
int m;
int main()
{m = Up_infor();Menu();return 0;
}/**********************Menu菜单**********************/
void Menu()
{int c,t=1;int i=0;while(t){printf("\n ------------------------------------| 职工管理系统 |---------------------------------\n");printf(" =======================================================================================\n");printf(" || ||\n");printf(" || - 1 - 录入员工信息 - 2 - 文件保存到硬盘上 ||\n");printf(" || ||\n");printf(" || - 3 - 职工总工资进行排序 - 4 - 查询员工信息 ||\n");printf(" || ||\n");printf(" || - 5 - 修改员工信息 - 6 - 查看所有员工信息 ||\n");printf(" || ||\n");printf(" || - 7 - 删除员工 - 8 - 退出系统 ||\n");printf(" || ||\n");printf(" || - @author by---Zdde丶 - ||\n");printf(" =======================================================================================\n");printf(" --请输入您的选择:");scanf("%d",&c);getchar();if(c<1||c>8) {printf("\t输入错误!请重新输入:\n");continue;}switch(c){case 1:Input_Infor();Cal_Wages();Sleep(2000);break;case 2:system("cls");Save_Infor();Sleep(2000);break;case 3:system("cls");sort_Wage();Sleep(2000);break;case 4:system("cls");Search_Worker();Sleep(2000);break;case 5:system("cls");Modify_Worker();break;case 6:system("cls");printf(" -----------------------------| 所有员工信息 |------------------------------\n");printf(" ============================================================================\n");printf("\t员工编号\t员工姓名\t基本工资\t津贴\t奖金\t总工资\t\n");for(i=0;i<m;i++){Print_Worker(i);}printf(" ============================================================================\n");Sleep(2000);break;case 7:system("cls");Worker_del();Sleep(2000);break;case 8:system("cls");t=0;printf("\n --------------------------| 职工管理系统 |--------------------------\n");printf(" ======================================================================\n");printf(" || ||\n");printf(" || 感谢您的使用! ||\n");printf(" || ||\n");printf(" || ( ^_^ ) ||\n");printf(" || ||\n");printf(" || - @author by---Zdde丶 - ||\n");printf(" =====================================================================\n");break;}}
}
/****************员工信息录入函数****************/
void Input_Infor()
{char c,nums[20];int i,j,flag=1;if(m==0)i=0;else i=m;printf(" ------------------------| 员工信息录入 |------------------------------\n");printf(" ======================================================================\n");while(1){printf(" || -第%d位职工的编号:",i+1);TO:if(m==0){gets(Worker_Infor[m].workernum);}else{gets(nums);for(j=0;j<m;j++){if(strcmp(nums,Worker_Infor[j].workernum)==0){printf(" || -已存在!请重输:");goto TO;}else{strcpy(Worker_Infor[i].workernum,nums);}}}printf("\n || -工号为%s职工的姓名:",Worker_Infor[i].workernum);gets(Worker_Infor[i].workername);printf("\n || -%s职工基本工资:",Worker_Infor[i].workername);while(1){scanf("%d",&Worker_Infor[i].basicwage);if(Worker_Infor[i].basicwage>=0) break;else printf("\n || 输入错误,请重试:");}printf("\n || -%s职工的津贴:",Worker_Infor[i].workername);while(1){scanf("%d",&Worker_Infor[i].allowance);if(Worker_Infor[i].allowance>=0) break;else printf("\t输入错误,请重试:");}printf("\n || -%s职工的奖金:",Worker_Infor[i].workername);while(1){scanf("%d",&Worker_Infor[i].Bonus);if(Worker_Infor[i].Bonus>=0) break;else printf("\t输入错误,请重试:");}i++;m=i;getchar();printf("\n || -是否继续输入:Y or N 或者 y or n:");scanf("%c",&c);getchar();if(c=='N'||c=='n'){system("cls");printf(" =========================================================================================\n");printf(" || ||\n");printf(" || 感谢您的使用! ||\n");printf(" || ||\n");printf(" || 信息更新完毕请保存! ||\n");printf(" || ||\n");printf(" || - @author by---Zdde丶 - ||\n");printf(" =========================================================================================\n");break;}}
}/*******************计算总工资******************************/
void Cal_Wages()
{int i;for(i=0;i<m;i++){Worker_Infor[i].totalwage = Worker_Infor[i].basicwage + Worker_Infor[i].allowance + Worker_Infor[i].Bonus;}}
/*******************员工总工资排序******************************/
void sort_Wage()
{Cal_Wages();int i,j;//printf("%d",m);for(i=0;i<m-1;i++){for(j=i+1;j<m;j++){if(Worker_Infor[j].totalwage>Worker_Infor[i].totalwage){temp = Worker_Infor[j];Worker_Infor[j]=Worker_Infor[i];Worker_Infor[i]=temp;}}}printf(" ------------------------------| 员工总工资排序 |---------------------------------\n");printf(" =================================================================================\n");printf("\t员工编号\t员工姓名\t基本工资\t津贴\t奖金\t总工资\t\n");for(i=0;i<m;i++){printf("\t%-10s\t%-8s\t%-10d\t%-8d%-8d%-6d\t\n",Worker_Infor[i].workernum,Worker_Infor[i].workername,Worker_Infor[i].basicwage,Worker_Infor[i].allowance,Worker_Infor[i].Bonus,Worker_Infor[i].totalwage);}printf(" =================================================================================\n");
}
/*******************保存文件**********************/
void Save_Infor()
{int i,n=0;FILE *pk;FILE *pf;Cal_Wages();pk=fopen("Worker.txt","w");pf=fopen("sumpeo.txt","w");n += m;fprintf(pf,"%d",n);/*if((pk=fopen("C:\\Users\\Administrator\\Desktop\\Worker.txt","w"))==NULL);{printf("文件打开失败\n");exit(0);}*/for(i=0;i<m;i++){fprintf(pk,"\t%10s\t%8s\t%10d\t%8d%8d%6d\t\n",Worker_Infor[i].workernum,Worker_Infor[i].workername,Worker_Infor[i].basicwage,Worker_Infor[i].allowance,Worker_Infor[i].Bonus,Worker_Infor[i].totalwage);}fclose(pf);fclose(pk);printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 文件保存成功 ||\n");printf(" || ||\n");printf(" =======================================================================================\n");}
/***************************文件载入******************/
int Up_infor()
{FILE *fp;FILE *pk;int i;fp = fopen("Worker.txt","r");pk = fopen("sumpeo.txt","rb");if(pk==NULL){printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 文件保存失败 ||\n");printf(" || ||\n");printf(" =======================================================================================\n");}else{fscanf(pk,"%d",&m);//printf("%d",m);}for(i=0;i<m;i++){fscanf(fp,"\t%10s\t%8s",Worker_Infor[i].workernum,Worker_Infor[i].workername);fscanf(fp,"\t%10d\t%8d%8d%6d",&Worker_Infor[i].basicwage,&Worker_Infor[i].allowance,&Worker_Infor[i].Bonus,&Worker_Infor[i].totalwage);/*printf("\t%10s\t%8s",Worker_Infor[i].workernum,Worker_Infor[i].workername);printf("\t%10d\t%8d%8d%6d\n",Worker_Infor[i].basicwage,Worker_Infor[i].allowance,Worker_Infor[i].Bonus,Worker_Infor[i].totalwage);*/}return m;fclose(fp);fclose(pk);}
/***************************查询方式******************/
void Print_Worker(int i)
{printf("\t%-10s\t%-8s\t%-10d\t%-8d%-8d%-6d\t\n",Worker_Infor[i].workernum,Worker_Infor[i].workername,Worker_Infor[i].basicwage,Worker_Infor[i].allowance,Worker_Infor[i].Bonus,Worker_Infor[i].totalwage);}
int Search_Worker()
{int i,p;int Search_Num();int Search_Name();printf(" ------------------------------------| 员工查询功能 |----------------------------------\n");printf(" ======================================================================================\n");printf(" || ||\n");printf(" || - 请选择查询方式: ||\n");printf(" || - 1 - 按姓名查询 - 2 - 按员工工号查询 ||\n");printf(" || - 3 - 退出 - 欢迎使用 -( ^_^ )- ||\n");printf(" || ||\n");printf(" ======================================================================================\n");scanf("%d",&p);switch(p){case 1:i=Search_Name();return i;break;case 2:i=Search_Num();return i;break;case 3:return -1;}return 0;
}
/***************************按名字查找******************/
int Search_Name()
{int i;char name[20];printf(" || - 请输入要查询的员工姓名: ");scanf("%s",name);for(i=0;i<m;i++){if(strcmp(name,Worker_Infor[i].workername)==0){printf(" ===============================================================================\n");printf("\t员工编号\t员工姓名\t基本工资\t津贴\t奖金\t总工资\t\n");Print_Worker(i);printf(" ===============================================================================\n");break;}}if(i==m){printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 没有找到员工 ||\n");printf(" || ||\n");printf(" =======================================================================================\n");return -1;}else return i;
}
/***************************按员工号查找******************/
int Search_Num()
{int i;char number[10];printf(" || - 请输入要查询的员工编号: ");scanf("%s",number);for(i=0;i<m;i++){if(strcmp(number,Worker_Infor[i].workernum)==0){printf(" =================================================================================\n");printf("\t员工编号\t员工姓名\t基本工资\t津贴\t奖金\t总工资\t\n");Print_Worker(i);printf(" =================================================================================\n");break;}}if(i==m){printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 没有找到员工 ||\n");printf(" || ||\n");printf(" =======================================================================================\n");return -1;}else return i;
}
/***************************修改员工信息**********************/
void Modify_Worker()
{int i,m;printf(" -------------------| 修改员工信息 |-------------------------\n");i = Search_Worker();if(i == -1) return;getchar();printf(" ========================================================================================\n");printf(" || ||\n");printf(" || - 请选择你要修改的内容: ||\n");printf(" || - 1 - 员工号 - 2 - 员工姓名 ||\n");printf(" || - 3 - 员工基本工资 - 4 - 员工的津贴 ||\n");printf(" || - 5 - 员工的奖金 - 6 - 退出 ||\n");printf(" || ||\n");printf(" ========================================================================================\n");scanf("%d",&m);switch(m){case 1:printf(" || -请输入新的职工编号:");scanf("%s",Worker_Infor[i].workernum);system("cls");printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 感谢您的使用! ||\n");printf(" || ||\n");printf(" || 信息修改完毕请保存! ||\n");printf(" || ||\n");printf(" || 请按2保存! ||\n");printf(" || ||\n");printf(" || - @author by---Zdde丶 - ||\n");printf(" =======================================================================================\n");Sleep(2000);break;case 2:printf(" || -请输入新的员工名:");scanf("%s",Worker_Infor[i].workername);system("cls");printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 感谢您的使用! ||\n");printf(" || ||\n");printf(" || 信息修改完毕请保存! ||\n");printf(" || ||\n");printf(" || 请按2保存! ||\n");printf(" || ||\n");printf(" || - @author by---Zdde丶 - ||\n");printf(" =======================================================================================\n");Sleep(1000);break;case 3:printf(" || -请输入新的员工基本工资:");scanf("%d",&Worker_Infor[i].basicwage);system("cls");printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 感谢您的使用! ||\n");printf(" || ||\n");printf(" || 信息修改完毕请保存! ||\n");printf(" || ||\n");printf(" || 请按2保存! ||\n");printf(" || ||\n");printf(" || - @author by---Zdde丶 - ||\n");printf(" =======================================================================================\n");Sleep(1000);break;case 4:printf(" || -请输入新的员工津贴:");scanf("%d",&Worker_Infor[i].allowance);system("cls");printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 感谢您的使用! ||\n");printf(" || ||\n");printf(" || 信息修改完毕请保存! ||\n");printf(" || ||\n");printf(" || 请按2保存! ||\n");printf(" || ||\n");printf(" || - @author by---Zdde丶 - ||\n");printf(" =======================================================================================\n");Sleep(1000);break;case 5:printf(" || -请输入新的员工奖金:");scanf("%d",&Worker_Infor[i].Bonus);system("cls");printf(" =======================================================================================\n");printf(" || ||\n");printf(" || 感谢您的使用! ||\n");printf(" || ||\n");printf(" || 信息修改完毕请保存! ||\n");printf(" || ||\n");printf(" || 请按2保存! ||\n");printf(" || ||\n");printf(" || - @author by---Zdde丶 - ||\n");printf(" =======================================================================================\n");Sleep(1000);break;case 6:return ;break;}printf("修改后的员工信息为:\n");printf(" =================================================================================\n");printf("\t员工编号\t员工姓名\t基本工资\t津贴\t奖金\t总工资\t\n");Print_Worker(i);printf(" =================================================================================\n");
}
/***************************删除员工信息**********************/
int Worker_del()
{int p,i,index;char name[20],worknum[20];Up_infor();printf(" =======================================================================================\n");printf(" || ||\n");printf(" || - 请选择删除方式: ||\n");printf(" || - 1 - 按姓名删除 - 2 - 按员工工号删除 ||\n");printf(" || - 3 - 退出 ||\n");printf(" || ||\n");printf(" =======================================================================================\n");scanf("%d",&p);switch(p){case 1:printf(" || -请输入要删除的员工姓名:");scanf("%s",name);for(i=0;i<m;i++){if(strcmp(name,Worker_Infor[i].workername)==0){index = i;//printf("%d",i);}}for(i = index;i<m-1;i++){Worker_Infor[i] = Worker_Infor[i+1];}m -= 1;Save_Infor();break;case 2:printf(" || -请输入要删除的员工编号:");scanf("%s",worknum);for(i=0;i<m;i++){if(strcmp(worknum,Worker_Infor[i].workernum)==0){index = i;//printf("%d",i);}}for(i = index;i<m-1;i++){Worker_Infor[i] = Worker_Infor[i+1];}m -= 1;Save_Infor();break;case 3:return -1;break;}return 0;
}
信息录入的截图
希望各位前辈提提建议,谢谢了!