|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct employee
{
int num;
char duty[10];
char name[10];
char sex[3];
unsigned char age;
char edu[10];
int salary;
char tel_office[13];
char tel_home[13];
char mobile[13];
char qq[11];
char address[31];
struct employee *next;
}EMP;
char password[9];
EMP *emp_first,*emp_end;
char gsave,gfirst;
void addemp(void);
void findemp(void);
void listemp(void);
void modifyemp(void);
void summaryemp(void);
void delemp(void);
void resetpwd(void);
void readdata(void);
void savedata(void);
int modi_age(int s);
int modi_salary(int s);
char *modi_field(char *field,char *s,int n);
EMP *findname(char *name);
EMP *findnum(int num);
EMP *findtelephone(char *name);
EMP *findqq(char *name);
void displayemp(EMP *emp,char *field,char *name);
void checkfirst(void);
void bound(char ch,int n);
void login();
void menu();
void addemp()
{
FILE *fp;
EMP *emp1;
int i=0;
char choice='y';
if((fp=fopen("employee.dat","ab"))==NULL)
{
printf("打開文件employee.dat出錯!\n");
getch();
return;
}
do{
i++;
emp1=(EMP *)malloc(sizeof(EMP));
if(emp1==NULL)
{
printf("內(nèi)存分配失敗,按任意鍵退出!\n");
getch();
return;
}
printf("請輸入第%d個員工的信息,\n",i);
bound('_',30);
printf("工號:");
scanf("%d",&emp1->num);
printf("職務:");
scanf("%s",&emp1->duty);
printf("姓名:");
scanf("%s",&emp1->name);
printf("性別:");
scanf("%s",&emp1->sex);
printf("年齡:");
scanf("%d",&emp1->age);
printf("文化程度:");
scanf("%s",&emp1->edu);
printf("工資:");
scanf("%d",&emp1->salary);
printf("辦公電話:");
scanf("%s",&emp1->tel_office);
printf("家庭電話:");
scanf("%s",&emp1->tel_home);
printf("移動電話:");
scanf("%s",&emp1->mobile);
printf("QQ:");
scanf("%s",&emp1->qq);
printf("地址:");
scanf("%s",&emp1->address);
emp1->next=NULL;
if(emp_first==NULL)
{
emp_first=emp1;
emp_end=emp1;
}else {
emp_end->next=emp1;
emp_end=emp1;
}
fwrite(emp_end,sizeof(EMP),1,fp);
gfirst=0;
printf("\n");
bound('_',30);
printf("\n是否繼續(xù)輸入?(y/n)");
fflush(stdin);
choice=getch();
if(toupper(choice)!='Y')
{
fclose(fp);
printf("\n輸入完畢,按任意鍵返回\n");
getch();
return;
}
system("cls");
}while(1);
}
void bound(char ch,int n)
{
while(n--)
putch(ch);
printf("\n");
return;
}
/*首次使用,進行用戶信息初始化*/
void checkfirst()
{
FILE *fp,*fp1;
char pwd[9],pwd1[9],pwd2[9],pwd3[9],ch;
int i;
char strt='8';
if((fp=fopen("config.bat","rb"))==NULL)
{
printf("\n新系統(tǒng),請進行相應的初始化操作!\n");
bound('_',50);
getch();
do{
printf("\n設置密碼,請不要超過8位:");
for(i=0;i<8&&((pwd[i]=getch())!=13);i++)
putch('*');
printf("\n再確認一次密碼:");
for(i=0;i<8&&((pwd1[i]=getch())!=13);i++)
putch('*');
pwd[i]='\0';
pwd1[i]='\0';
if(strcmp(pwd,pwd1)!=0)
printf("\n兩次密碼輸入不一致,請重新輸入!\n\n");
else break;
}while(1);
if((fp1=fopen("config.bat","wb"))==NULL)
{
printf("\n系統(tǒng)創(chuàng)建失敗,請按任意鍵退出!");
getch();
exit(1);
}
i=0;
while(pwd[i])
{
pwd2[i]=(pwd[i]^ strt);
putw(pwd2[i],fp1);
i++;
}
fclose(fp1);
printf("\n\n系統(tǒng)初始化成功,按任意鍵退出后,再重新進入!\n");
getch();
exit(1);
}else{
i=0;
while(!feof(fp)&&i<8)
pwd[i++]=(getw(fp)^strt);
pwd[i]='\0';
if(i>=8) i--;
while(pwd[i]!=-1&&i>=0)
i--;
pwd[i]='\0';
strcpy(password,pwd);
}
}
void delemp()
{
int findok=0;
EMP *emp1,*emp2;
char name[10],choice;
system("cls");
printf("\n輸入要刪除的員工姓名:");
scanf("%s",name);
emp1=emp_first;
emp2=emp1;
while(emp1)
{
if(strcmp(emp1->name,name)==0)
{
findok=1;
system("cls");
printf("員工:%s的信息如下:",emp1->name);
bound('_',40);
printf("工號:%d\n",emp1->num);
printf("職務:%s\n",emp1->duty);
printf("姓名:%s\n",emp1->name);
printf("性別:%s\n",emp1->sex);
printf("年齡:%d\n",emp1->age);
printf("文化程度:%s\n",emp1->edu);
printf("工資:%d\n",emp1->salary);
printf("辦公電話:%s\n",emp1->tel_office);
printf("家庭電話:%s\n",emp1->tel_home);
printf("移動電話:%s\n",emp1->mobile);
printf("QQ號碼:%s\n",emp1->qq);
printf("住址:%\ns",emp1->address);
bound('_',40);
printf("您真的要刪除該員工嗎?(y/n)");
fflush(stdin);
choice=getchar();
if(choice!='y' && choice!='Y') return;
if(emp1==emp_first) emp_first=emp1->next;
else emp2->next=emp1->next;
free(emp1);
gsave=1;
savedata();
return;
} else{
emp2=emp1;
emp1=emp1->next;
}
}
if(!findok)
{
bound('_',40);
printf("\n沒有找到姓名是:%s的信息!\n",name);
getch();
}
return;
}
void displayemp(EMP *emp,char *field,char *name)
{
if(emp)
{
printf("\n%s:%s信息如下:\n",field,name);
bound('_',30);
printf("工號:%d\n",emp->num);
printf("職務:%s\n",emp->duty);
printf("姓名:%s\n",emp->name);
printf("性別:%s\n",emp->sex);
printf("年齡:%d\n",emp->age);
printf("文化程度:%s\n",emp->edu);
printf("工資:%d\n",emp->salary);
printf("辦公電話:%s\n",emp->tel_office);
printf("家庭電話:%s\n",emp->tel_home);
printf("移動電話:%s\n",emp->mobile);
printf("QQ號碼:%s\n",emp->qq);
printf("住址:%s\n",emp->address);
bound('_',30);
}else {
bound('_',40);
printf("資料庫中沒有%s為:%s的員工!請重新確認!",field,name);
}
return;
}
EMP *findname(char *name)
{
EMP *emp1;
emp1=emp_first;
while(emp1)
{
if(strcmp(name,emp1->name)==0) return emp1;
emp1=emp1->next;
}
return NULL;
}
EMP *findnum(int num)
{
EMP *emp1;
emp1=emp_first;
while(emp1)
{
if(num==emp1->num) return emp1;
emp1=emp1->next;
}
return NULL;
}
EMP *findtelephone(char *name)
{
EMP *emp1;
emp1=emp_first;
while(emp1)
{
if((strcmp(name,emp1->tel_office)==0)||
(strcmp(name,emp1->tel_home)==0)||
(strcmp(name,emp1->mobile)==0))
return emp1;
emp1=emp1->next;
}
return NULL;
}
EMP *findqq(char *name)
{
EMP *emp1;
emp1=emp_first;
while(emp1)
{
if(strcmp(name,emp1->qq)==0) return emp1;
emp1=emp1->next;
}
return NULL;
}
void findemp()
{
int choice,ret=0,num;
char str[13];
EMP *emp1;
system("cls");
do{
printf("\t查詢員工信息\n");
bound('_',30);
printf("\t1.按姓名查詢\n");
printf("\t2.按工號查詢\n");
printf("\t3.按電話查詢\n");
printf("\t4.按QQ號查詢\n");
printf("\t0.返回主菜單\n");
bound('_',30);
printf("\n請選擇菜單:");
do{
fflush(stdin);
choice=getchar();
system("cls");
switch(choice)
{
case '1':
printf("\n輸入要查詢的員工姓名:");
scanf("%s",str);
emp1=findname(str);
displayemp(emp1,"姓名",str);
getch();
break;
case '2':
printf("\n請輸入要查詢的員工的工號");
scanf("%d",&num);
emp1=findnum(num);
itoa(num,str,10);
displayemp(emp1,"工號",str);
getch();
break;
case '3':
printf("\n輸入要查詢員工的電話:");
scanf("%s",str);
emp1=findtelephone(str);
displayemp(emp1,"電話",str);
getch();
break;
case '4':
printf("\n輸入要查詢的員工的QQ號:");
scanf("%s",str);
emp1=findqq(str);
displayemp(emp1,"QQ號碼",str);
getch();
break;
case '0':
ret=1;
break;
}
}while(choice<'0'||choice>'4');
system("cls");
if(ret) break;
}while(1);
}
void listemp()
{
EMP *emp1;
printf("\n資料庫中的員工信息列表\n");
bound('_',40);
emp1=emp_first;
while(emp1)
{
printf("工號:%d\n",emp1->num);
printf("職務:%s\n",emp1->duty);
printf("姓名:%s\n",emp1->name);
printf("性別:%s\n",emp1->sex);
printf("年齡:%d\n",emp1->age);
printf("文化程度:%s\n",emp1->edu);
printf("工資:%d\n",emp1->salary);
printf("辦公電話:%s\n",emp1->tel_office);
printf("家庭電話:%s\n",emp1->tel_home);
printf("移動電話:%s\n",emp1->mobile);
printf("QQ號碼:%s\n",emp1->qq);
printf("住址:%s\n",emp1->address);
bound('_',40);
emp1=emp1->next;
}
printf("\n顯示完畢,按任意鍵退出!\n");
getch();
return;
}
/*檢測登錄密碼*/
void login()
{
int i,n=3;
char pwd[9];
do{
printf("請輸入密碼:");
for(i=0;i<8 && ((pwd[i]=getch())!=13);i++)
putch('*');
pwd[i]='\0';
if(strcmp(pwd,password))
{
printf("\n密碼錯誤,請重新輸入!\n");
system("cls");
n--;
}else break;
} while(n>0);
if(!n)
{
printf("請退出,你的三次輸入密碼錯誤!");
getch();
exit(1);
}
}
void menu()
{
char choice;
system("cls");
do{
printf("\t 企業(yè)員工管理系統(tǒng)\n");
bound('_',40);
printf("\t1.輸入員工信息\n");
printf("\t2.查詢員工信息\n");
printf("\t3.顯示員工信息\n");
printf("\t4.修改員工信息\n");
printf("\t5.刪除員工信息\n");
printf("\t6.統(tǒng)計員工信息\n");
printf("\t7.重置系統(tǒng)密碼\n");
printf("\t0.退出系統(tǒng)\n");
bound('_',40);
printf("\n請選擇您需要的操作!");
do{
fflush(stdin);
choice=getchar();
system("cls");
switch(choice)
{
case '1':
addemp();
break;
case '2':
if(gfirst)
{
printf("系統(tǒng)信息中無員工信息,請先添加員工信息!\n");
getch();
break;
}
findemp();
break;
case '3':
if(gfirst)
{
printf("系統(tǒng)信息中無員工信息,請先添加員工信息!\n");
getch();
break;
}
listemp();
break;
case '4':
if(gfirst)
{
printf("系統(tǒng)信息中無員工信息,請先添加員工信息!\n");
getch();
break;
}
modifyemp();
break;
case '5':
if(gfirst)
{
printf("系統(tǒng)信息中無員工信息,請先添加員工信息!\n");
getch();
break;
}
delemp();
break;
case '6':
if(gfirst)
{
printf("系統(tǒng)信息中無員工信息,請先添加員工信息!\n");
getch();
break;
}
summaryemp();
break;
case '7':
resetpwd();
break;
case '0':
savedata();
exit(0);
}
} while(choice<'0'||choice>'7');
system("cls");
}while(1);
}
int modi_salary(int salary){
int newsalary;
printf("原來的工資數(shù)為:%d",salary);
printf("新的工資數(shù):");
scanf("%d",&newsalary);
return(newsalary);
}
int modi_age(int age){
int newage;
printf("原來的年齡為:%d",age);
printf("新的年齡:");
scanf("%d",&newage);
return(newage);
}
char *modi_field(char *field,char *content,int len)
{
char *str;
str=malloc(sizeof(char)*len);
if(str==NULL)
{
printf("內(nèi)存分配失敗,按任意鍵退出!");
getch();
return NULL;
}
printf("原來%s為:%s\n",field,content);
printf("修改為(內(nèi)容不要超過%d個字符!):",len);
scanf("%s",str);
return str;
}
void modifyemp()
{
EMP *emp1;
char name[10],*newcontent;
int choice;
printf("\n請輸入您要修改的員工的信息:");
scanf("%s",&name);
emp1=findname(name);
displayemp(emp1,"姓名",name);
if(emp1)
{
printf("\n 請輸入你要修改的內(nèi)容選項!\n");
bound('_',40);
printf("1.修改職務 2.修改年齡\n");
printf("3.修改文化程度 4.修改工資\n");
printf("5.修改辦公室電話 6.修改家庭電話\n");
printf("7.修改移動電話 8.修改QQ號碼 \n");
printf("9.修改住址 0.返回\n ");
bound('_',40);
do{
fflush(stdin);
choice=getchar();
switch(choice)
{
case '1':
newcontent=modi_field("職務",emp1->duty,10);
if(newcontent!=NULL)
{
strcpy(emp1->duty,newcontent);
free(newcontent);
}
break;
case '2':
emp1->age=modi_age(emp1->age);
break;
case '3':
newcontent=modi_field("文化程度",emp1->edu,10);
if(newcontent!=NULL)
{
strcpy(emp1->edu,newcontent);
free(newcontent);
}
break;
case '4':
emp1->salary=modi_salary(emp1->salary);
break;
case '5':
newcontent=modi_field("辦公室電話",emp1->tel_office,13);
if(newcontent!=NULL)
{
strcpy(emp1->tel_office,newcontent);
free(newcontent);
}
break;
case '6':
newcontent=modi_field("家庭電話",emp1->tel_home,13);
if(newcontent!=NULL)
{
strcpy(emp1->tel_home,newcontent);
free(newcontent);
}
break;
case '7':
newcontent=modi_field("移動電話",emp1->mobile,12);
if(newcontent!=NULL)
{
strcpy(emp1->mobile,newcontent);
free(newcontent);
}
break;
case '8':
newcontent=modi_field("QQ號碼",emp1->qq,10);
if(newcontent==NULL)
{
strcpy(emp1->qq,newcontent);
free(newcontent);
}
break;
case '9':
newcontent=modi_field("住址",emp1->address,30);
if(newcontent!=NULL)
{
strcpy(emp1->address,newcontent);
free(newcontent);
}
break;
case '0':
return;
}
}while(choice<'0' || choice>'9');
gsave=1;
savedata();
printf("\n修改完畢,按任意鍵退出!\n");
getch();
}
return;
}
/*讀取數(shù)據(jù)*/
void readdata(void)
{
FILE *fp;
EMP *emp1;
if((fp=fopen("employee.dat","rb"))==NULL)
{
gfirst=1;
return;
}
while(!feof(fp))
{
emp1=(EMP *)malloc(sizeof(EMP));
if(emp1==NULL)
{
printf("內(nèi)存分配失敗!\n");
getch();
return;
}
fread(emp1,sizeof(EMP),1,fp);
if(feof(fp)) break;
if(emp_first==NULL)
{
emp_first=emp1;
emp_end=emp1;
}else{
emp_end->next=emp1;
emp_end=emp1;
}
emp_end->next=NULL;
}
gfirst=0;
fclose(fp);
}
void resetpwd()
{
char pwd[9],pwd1[9],ch;
int i;
FILE *fp1;
system("cls");
printf("\n請輸入舊密碼:\n");
for(i=0;i<8 && ((pwd[i]=getch())!=13);i++)
putch('*');
pwd[i]='\0';
if(strcmp(password,pwd)!=0)
{
printf("\n密碼錯誤,請按任意鍵退出!\n");
getch();
return;
}
do{
printf("\n設置新密碼,請不要超過8位:");
for(i=0;i<8&&((pwd[i]=getch())!=13);i++)
putch('*');
printf("\n再確認一次密碼:");
for(i=0;i<8&&((pwd1[i]=getch())!=13);i++)
putch('*');
pwd[i]='\0';
pwd1[i]='\0';
if(strcmp(pwd,pwd1)!=0)
printf("\n兩次密碼輸入不一致,請重新輸入!\n\n");
else break;
}while(1);
if((fp1=fopen("config.bat","wb"))==NULL)
{
printf("\n系統(tǒng)創(chuàng)建失敗,請按任意鍵退出!");
getch();
exit(1);
}
i=0;
while(pwd[i])
{
putw(pwd[i],fp1);
i++;
}
fclose(fp1);
printf("\n密碼修改成功,按任意鍵退出!\n");
getch();
return;
}
void savedata()
{
FILE *fp;
EMP *emp1;
if(gsave==0) return;
if((fp=fopen("employee.dat","wb"))==NULL)
{
printf("打開文件employee.dat出錯!\n");
getch();
return;
}
emp1=emp_first;
while(emp1)
{
fwrite(emp1,sizeof(EMP),1,fp);
emp1=emp1->next;
}
gsave=0;
fclose(fp);
}
void summaryemp()
{
EMP *emp1;
int sum=0,num=0,man=0,woman=0;
emp1=emp_first;
while(emp1)
{
num++;
sum+=emp1->salary;
char strw[2];
strncpy(strw,emp1->sex,2);
if((strcmp(strw,"ma")==0)||(strcmp(emp1->sex,"男")==0)) man++;
else woman++;
emp1=emp1->next;
}
printf("\n下面是相關員工的統(tǒng)計信息!\n");
bound('_',40);
printf("員工總數(shù)是:%d\n",num);
printf("員工的工資總數(shù)是:%d\n",sum);
printf("男員工數(shù)為:%d\n",man);
printf("女員工數(shù)為:%d\n",woman);
bound('_',40);
printf("按任意鍵退出!\n");
getch();
return;
}
int main(void)
{
emp_first=emp_end=NULL;
gsave=gfirst=0;
checkfirst();
login();
readdata();
menu();
system("PAUSE");
return 0;
}
|
評分
-
查看全部評分
|