include<stdio.h>
#include<string.h>
#include<stdlib.h>
//员工结构体
struct Employee{
char gonghao[20];
char name[15];
char gender[8];//性别
char tele[11];//电话
double gongzi;//工资
int age;//年龄
char bumen[20];//部门
char mima[20];//密码
};
//员工结构体数组
struct Employee employeeList[100];
//记录当前员工的个数
int employeeCount = 0;
void menu();
//根据员工姓名获取员工下标
int getOneEmployeeByName(char *name);
//添加
void addEmployee();
//查询员工
void findEmployee (int all);
//写入员工信息
void writeEmployee();
//读取员工信息
void readEmployee();
//修改员工信息
void modEmployee();
//删除员工
void delEmployee();
int delEmployeeByIndex(int index);
//排序
void sortEmployee();
int entry(char *user,char *pass);
//主函数
int main() {
//读取员工信息
readEmployee();
char pass[20],user[20];
printf("输入账号密码>>>");
scanf("%s %s",&user,&pass);
int flag=entry(user,pass);
if(flag==-1){
while(1){
int choice;
menu();
scanf("%d",&choice);
switch(choice){
case 1:
addEmployee();
break;
case 2:
findEmployee (1);// 直接显示所有的员工
delEmployee();
break;
case 3:
findEmployee (1);// 直接显示所有的员工
modEmployee();
break;
case 4:
//查询员工
findEmployee (0);
break;
case 5:
findEmployee (1);
sortEmployee();//排序
break;
case 6:
writeEmployee();
printf("保存员工成功\n");
break;
case 0:
printf("\n\n感谢您的使用\n\n");
exit(0);
default:
printf("您的输入有误,请从新输入\n");
system("pause");
break;
}
}
}
else if(flag>=0)
{
printf("这是你的信息\n");
printf("\n%-5s%-20s%-15s%-10s%-16s%-12s%-10s%-20s\n\n","序号" ,"工号","姓名","性别","电话","工资","年龄","部门" );
printf("%-5d%-20S%-15s%-10s%-16s%-12lf%-10d%-20s \n\n",flag
,employeeList [flag] . gonghao
,employeeList [flag] . name
,employeeList [flag] . gender
,employeeList [flag] . tele
,employeeList [flag] . gongzi
,employeeList [flag] . age
,employeeList [flag] . bumen
);
}
else{
printf("warnings");
printf("%d",flag);
}
}
//排序员工
void sortEmployee(){
int select,i,j;
printf("1:按 姓名 排序\n");
printf("2:按 性别 排序\n");
printf("3:按 电话 排序\n");
printf("4:按 工资 排序\n");
printf("5:按 年龄 排序\n");
printf("请输入选择:");
scanf("%d",&select);
// 1 2 3 5 6 8
for(i = employeeCount-1 ; i > 0;i--){//确定排序的次数, n - 1,,, 5 4 3 2 1
for(j = 0;j<i;j++){ //每轮排序的次数
if(
1 == select && strcmp( employeeList[j].name ,employeeList[j+1].name ) > 0 //正向排序
||
2 == select && strcmp( employeeList[j].gender ,employeeList[j+1].gender ) > 0 //正向排序
||
3 == select && strcmp( employeeList[j].tele ,employeeList[j+1].tele ) > 0 //正向排序
||
4 == select && employeeList[j].gongzi > employeeList[j+1].gongzi //正向排序
||
5 == select && employeeList[j].age > employeeList[j+1].age //正向排序
){
struct Employee employee = employeeList[j];
employeeList[j] = employeeList[j+1];
employeeList[j+1] = employee;
}
}
}
findEmployee(1);
}
//删除员工
void delEmployee(){
printf("请输入需要删除的员工序号:");
int index;
scanf("%d",&index);
int res = delEmployeeByIndex(index);
if(res){
printf("删除成功!\n");
}else{
printf("删除失败!\n");
}
}
//根据下标删除员工
int delEmployeeByIndex(int index){
if(index<0 || index >= employeeCount){
return 0;//删除失败!
}
//开始删除
//0 1 2 _ 4 5 employeeCount--;
int i;
for(i = index;i < employeeCount-1 ;i++){
employeeList[i] = employeeList[i+1];
}
employeeCount --;
return 1;//删除成功!
}
//修改
void modEmployee(){
printf("请输入要修改的员工的序号:");
int indexOfMod = -1;
scanf("%d",&indexOfMod);
if(indexOfMod >= 0 && indexOfMod < employeeCount){
printf("请输入员工工号:");
char GONGHAO[20];
scanf("%s",GONGHAO);
while( strcmp(employeeList[indexOfMod].gonghao, GONGHAO)!=0 && getOneEmployeeByName(GONGHAO)>-1 ){
printf("当前员工姓名已经存在啦!请重新输入:\n");
scanf("%s",GONGHAO);
}
//跳出循环说明姓名已经不会重复了
printf("请输入姓名 :");
scanf("%s",employeeList [ indexOfMod ] . name);
printf("请输入性别 :");
scanf("%s",employeeList [ indexOfMod ] . gender);
printf("请输入电话 :");
scanf("%s",employeeList [ indexOfMod ] . tele);
printf("请输入工资 :");
scanf("%lf",&employeeList [ indexOfMod ] . gongzi);
printf("请输入年龄 :");
scanf("%d",&employeeList [ indexOfMod ] . age);
printf("请输入部门 :");
scanf("%s",&employeeList [ indexOfMod ] . bumen);
printf("请输入密码 :");
scanf("%d",&employeeList [ indexOfMod ] . mima);
printf("修改成功!\n");
}else{
printf("请输入正确的序号!\n");
}
}
//写入员工信息
void writeEmployee(){
FILE *fp=NULL;
fp = fopen("C:\\Users\\ASUS\\Desktop\\4.txt","w+");//覆盖写入文件
int i;
for(i = 0 ;i<employeeCount;i++){
fprintf(fp,"%s %s %s %s %lf %d %s %s\n",employeeList[i].gonghao ,employeeList[i].name ,employeeList[i].gender ,employeeList[i].tele ,employeeList[i].gongzi ,employeeList[i].age,employeeList[i].bumen,employeeList[i].mima );
}
fclose(fp);
}
//读取已经保存的员工信息
void readEmployee(){
FILE *fp=NULL;
if( NULL == (fp = fopen("C:\\Users\\ASUS\\Desktop\\4.txt","r")) ){
//没有 Employee.txt
//fclose(fp);
return ;
}
int i=0;
while(//& andou
fscanf(fp,"%s%s%s%s%lf%d%s%s\n",employeeList[i].gonghao ,employeeList[i].name ,employeeList[i].gender ,employeeList[i].tele ,&employeeList[i].gongzi ,&employeeList[i].age,&employeeList[i].bumen,&employeeList[i].mima)!=EOF
){
i++;
}
employeeCount = i;
fclose(fp);
}
//员工查询
void findEmployee (int all) {
int select = 0;
char conCharArr[40];
double conNum1,conNum2;//condintion
if(all == 0){
printf("0:全部\n");
printf("1:按 姓名 查询\n");
printf("2:按 性别 查询\n");
printf("3:按 工资 查询\n");
printf("请输入选择:");
scanf("%d",&select);
if(select==1 ||select==2 ) {
printf("请输入筛选条件:");
scanf("%s",conCharArr);
} else if(select==3 ) {
printf("请输入筛选范围m,n,用空格隔开(m ≤工资 ≤n):");
scanf("%lf",&conNum1);
scanf("%lf",&conNum2);
}
}
int i;
int count = 0;
double gongziSum = 0.0 ;
int ageSum = 0;
printf("\n%-5s%-20s%-15s%-10s%-16s%-12s%-10s%-20s\n\n","序号" ,"工号","姓名","性别","电话","工资","年龄","部门" );
for(i = 0;i < employeeCount;i++){
//select conCharArr
if(
select == 0
||
select == 1 && strstr(employeeList[i].name,conCharArr)
||
select == 2 && strstr(employeeList[i].gender,conCharArr)
||
select == 3 && employeeList[i].gongzi >= conNum1 && employeeList[i].gongzi <= conNum2
){
printf("%-5d%-20S%-15s%-10s%-16s%-12lf%-10d%-20s \n\n",i
,employeeList [i] . gonghao
,employeeList [i] . name
,employeeList [i] . gender
,employeeList [i] . tele
,employeeList [i] . gongzi
,employeeList [i] . age
,employeeList [i] . bumen
);
count++;//总人数
gongziSum += employeeList [i] . gongzi;
ageSum += employeeList [i] . age;
}
}
printf("\n总人数:%d\n",count);
printf("\n平均工资:%.2f\n", count==0? 0 : gongziSum/count);
printf("\n平均年龄:%.2f\n", count==0? 0 : ageSum * 1.0/count);
}
//传入一个姓名,然后循环判断当前姓名在员工中是否已经存在。
int getOneEmployeeByName(char *name){
int i;
int res = -1;
for(i = 0;i < employeeCount;i++){
if( strcmp(name,employeeList[i].gonghao) == 0 ){
//表示相等
res = i;
break;
}
}
return res;
}
//添加员工
void addEmployee(){
char selectYNFlag = 'y';
while(selectYNFlag == 'Y' || selectYNFlag=='y'){
//输入员工信息
printf("\n请输入工号:");
scanf("%s",employeeList[employeeCount].gonghao);
int index = getOneEmployeeByName(employeeList[employeeCount].gonghao);
if(index > -1){
printf("人已经存在!\n");
break;
}
printf("请输入姓名:");
scanf("%s",employeeList[employeeCount].name);
printf("请输入性别:");
scanf("%s",employeeList[employeeCount].gender);
printf("请输入电话:");
scanf("%s",employeeList[employeeCount].tele);
printf("请输入工资:");
scanf("%lf",&employeeList[employeeCount].gongzi);
printf("请输入年龄:");
scanf("%d",&employeeList[employeeCount].age);
printf("请输入部门:");
scanf("%s",&employeeList[employeeCount].bumen);
printf("请输入密码:");
scanf("%s",&employeeList[employeeCount].mima);
employeeCount = employeeCount+1;
printf("是否继续录入:");
getchar();
scanf("%c",&selectYNFlag);//回车
}
printf("\n录入成功!员工数量:%d\n",employeeCount);
}
void menu() {
//菜单
printf("==============主菜单===========\n");
printf("1.添加员工\n");
printf("2.删除员工\n");
printf("3.修改员工\n");
printf("4.查询员工\n");
printf("5.排序员工\n");
printf("6.保存\n");
printf("0.退出\n");
printf("===============================\n");
}
int entry(char *user,char *pass)
{
if(strcmp(user,"users")==0&&strcmp(pass,"114514")==0)
{
return -1;
}else{
for(int i=0;i<employeeCount;i++){
if(strcmp(user,employeeList[i].name) == 0 && strcmp(pass,employeeList[i].mima) == 0 )
{
return i;
}
}
}
return -2;
}
员工管理系统
最新推荐文章于 2024-07-12 21:20:02 发布