| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
#include <stdio.h> 
#include <string.h> 
#include "emp.h" 
int main(void)  
{  
    emp_first=emp_end=NULL; 
    gsave=gfirst=0; 
    checkfirst(); 
    login(); 
    readdata();  /*从文件中读取员工信息到链表*/  
    menu(); 
    return 0; 
}  
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("非法用户,请关机!\n");  
       getch(); 
       exit(1); 
    } 
} 
void menu()  /*菜单*/  
{ 
    char choice; 
    system("cls");    
    do{ 
        printf("\t员工管理系统\n");  
        bound('_',30);        
        printf("\t1.输入员工信息\n");  
        printf("\t2.查询员工信息\n"); 
        printf("\t3.显示员工信息\n");  
        printf("\t4.修改员工信息\n");  
        printf("\t5.删除员工信息\n");  
        printf("\t6.统计员工信息\n");  
        printf("\t7.重设系统密码\n"); 
        printf("\t0.退出系统\n");  
        bound('_',30); 
        printf("\n请选择菜单:"); 
        do{ 
            fflush(stdin); 
            choice=getchar(); 
            system("cls"); 
            switch(choice)  
            {  
                case '1':      /*输入员工信息*/  
                    addemp();  
                    break;  
                case '2':      /*查询员工信息*/  
                    if(gfirst) 
                    { 
                        printf("系统中还没有员工信息,请先添加员工信息!\n"); 
                        getch(); 
                        break; 
                    }                     
                    findemp();  
                    break;  
                case '3':       /*显示员工信息*/ 
                    if(gfirst) 
                    { 
                        printf("系统中还没有员工信息,请先添加员工信息!\n"); 
                        getch(); 
                        break; 
                    }  
                    listemp();  
                    break;                  
                case '4':      /*修改员工信息*/ 
                    if(gfirst) 
                    { 
                        printf("系统中还没有员工信息,请先添加员工信息!\n"); 
                        getch(); 
                        break; 
                    }  
                    modifyemp();  
                    break;                               
                case '5':    /*删除员工信息*/ 
                    if(gfirst) 
                    { 
                        printf("系统中还没有员工信息,请先添加员工信息!\n"); 
                        getch(); 
                        break; 
                    }  
                    delemp();  
                    break;               
                case '6':    /*统计员工信息*/ 
                    if(gfirst) 
                    { 
                        printf("系统中还没有员工信息,请先添加员工信息!\n"); 
                        getch(); 
                        break; 
                    }  
                    summaryemp();  
                    break;               
                case '7':    /*重设系统密码*/ 
                    resetpwd();  
                    break;                                                             
                case '0':    /*退出系统*/  
                    savedata(); 
                    exit(0);  
            } 
        }while(choice<'0' || choice>'7');  
        system("cls"); /*调用清屏函数*/ 
    }while(1);  
} 
void readdata(void)  /*从文件中获取员工信息,创建链表*/  
{ 
    FILE *fp; 
    EMP *emp1; 
    if((fp=fopen("emp.dat","rb"))==NULL) /*读方式打开文件*/  
    { 
        gfirst=1; 
        return;  
    } 
    while(!feof(fp))  /*读入文件中的数据,将其添加到链表中*/  
    { 
        emp1=(EMP *)malloc(sizeof(EMP)); 
        if(emp1==NULL) 
        { 
            printf("内存分败!\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 savedata()            /*将链表中的数据保存到文件*/ 
{ 
    FILE *fp; 
    EMP *emp1; 
    if(gsave==0) return ; 
    if((fp=fopen("emp.dat","wb"))==NULL) /*写方式打开文件*/  
    { 
        printf("打开文件emp.dat出错!\n"); 
        getch();  
        return;  
    } 
    emp1=emp_first; 
    while(emp1) 
    { 
        fwrite(emp1,sizeof(EMP),1,fp); 
        emp1=emp1->next; 
    } 
    gsave=0;            /*设置标志*/  
    fclose(fp);  
} 
     
void addemp()  /*输入员工信息*/  
{ 
    FILE *fp;  
    EMP *emp1; 
    int i=0;  
    char choice='y';  
    if((fp=fopen("emp.dat","ab"))==NULL)  /*追加模式打开*/  
    { 
        printf("打开文件emp.dat出错!\n"); 
        getch(); 
        return;  
    }     
    do{ 
        i++; 
        emp1=(EMP *)malloc(sizeof(EMP)); 
        if(emp1==NULL) 
        { 
            printf("内存分配失败,按任意键返回!\n"); 
            getch(); 
            return; 
        } 
        printf("输入第%d个员工的信息:\n",i);  
        bound('_',20);  
        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('_',20); 
        printf("\n是否继续输入?(y/n)");  
        fflush(stdin); 
        choice=getchar(); 
        if(toupper(choice)!='Y')  
        { 
            fclose(fp);  
            printf("\n输入完毕,任意键返回\n");  
            getch();  
            return;  
        } 
        system("cls"); 
    }while(1); 
}  
void listemp()/*显示员工信息 */ 
{ 
    EMP *emp1;  
    printf("\n员工列表:\n"); 
    bound('_',25);  
    emp1=emp_first; 
    while(emp1) 
    {  
        printf("工号:%d\n",emp1->num);  
        printf("姓名:%s\n",emp1->name); 
        printf("职务:%s\n",emp1->duty);          
        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('_',25);  
        emp1=emp1->next; 
    } 
    printf("\n按任意键返回\n");  
    getch();  
    return;  
}  
void modifyemp()  /*修改员工信息*/  
{ 
    EMP *emp1; 
    char name[10],*str;  
    int choice;  
    printf("\n输入要修改员工的姓名:");  
    scanf("%s",&name);  
    emp1=findname(name);                     
    displayemp(emp1,"姓名",name);  
    if(emp1) 
    { 
        printf("\n选择要修改的项目:\n");  
        bound('_',35);  
        printf("  1.修改职务       2.修改文化程度\n");  
        printf("  3.修改工资       4.修改办公电话\n");  
        printf("  5.修改家庭电话   6.修改移动电话\n");  
        printf("  7.修改QQ号码     8.修改住址\n");  
        printf("  0.返回\n");  
        bound('_',35);  
        printf("请选择:");  
        do{ 
            fflush(stdin); 
            choice=getchar(); 
            switch(choice)  
            {  
                case '1': 
                    str=modi_field("职务",emp1->duty,10); 
                    if(str!=NULL) 
                    { 
                        strcpy(emp1->duty,str); 
                        free(str); 
                    } 
                    break; 
                case '2':  
                    str=modi_field("文化程度",emp1->edu,10); 
                    if(str!=NULL) 
                    { 
                        strcpy(emp1->edu,str); 
                        free(str); 
                    } 
                    break;  
                case '3':  
                    emp1->salary=modi_salary(emp1->salary); 
                    break;  
                case '4':  
                    str=modi_field("办公电话", emp1->tel_office,13); 
                    if(str!=NULL) 
                    { 
                        strcpy(emp1->tel_office,str); 
                        free(str); 
                    } 
                    break;                     
                case '5':  
                    str=modi_field("家庭电话", emp1->tel_home,13); 
                    if(str!=NULL) 
                    { 
                        strcpy(emp1->tel_home,str); 
                        free(str); 
                    } 
                    break;                     
                case '6':  
                    str=modi_field("移动电话", emp1->mobile,12); 
                    if(str!=NULL) 
                    { 
                        strcpy(emp1->mobile,str); 
                        free(str); 
                    } 
                    break; 
                case '7':  
                    str=modi_field("QQ号码", emp1->qq,10); 
                    if(str!=NULL) 
                    { 
                        strcpy(emp1->qq,str); 
                        free(str); 
                    } 
                    break; 
                case '8':  
                    str=modi_field("住址", emp1->address,30); 
                    if(str!=NULL) 
                    { 
                        strcpy(emp1->address,str); 
                        free(str); 
                    } 
                    break;                                                                     
                case '0':  
                    return;  
            } 
        }while(choice<'0' || choice>'8'); 
        gsave=1; 
        savedata();  
        printf("\n修改完成,按任意键返回。\n"); 
        getch(); 
    } 
    return;  
}  
int modi_salary(int s)  /*修改工资*/  
{ 
    int salary; 
    printf("原工资:%d\n",s);  
    printf("新工资:");  
    scanf("%d",&salary);  
    return(salary);  
}  
char *modi_field(char *field,char *s,int n) /*通用文字字段修改函数*/  
{ 
    char *str; 
    str=malloc(sizeof(char)*n); 
    if(str==NULL) 
    { 
        printf("内存分配失败,按任意键返回!\n"); 
        getch(); 
        return NULL; 
    }  
    printf("原%s:%s\n",field,s);  
    printf("修改为(不超过%d个字符):",n);  
    scanf("%s",str); 
    return str;  
}  
void delemp()/*删除员工信息 */ 
{ 
    int i=0,find=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) 
        { 
            find=1; 
            system("cls"); 
            printf("员工:%s 信息如下:\n",emp1->name);  
            bound('_',25); 
            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('_',25); 
            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(!find) 
    { 
        bound('_',30); 
        printf("未找到姓名为:%s 的信息!\n",name); 
        getch(); 
    }  
    return; 
}  
void findemp()  /*查询员工信息*/  
{ 
    int choice,ret=0,num; 
    char str[13];  
    EMP *emp1; 
    system("cls"); 
    do{ 
        printf("\t查询员工信息\n");  
        bound('_',25);        
        printf("\t1.按姓名查询\n");  
        printf("\t2.按工号查询\n"); 
        printf("\t3.按电话查询\n");  
        printf("\t4.按QQ号查询\n");  
        printf("\t0.返回主菜单\n");  
        bound('_',25); 
        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':      /*按QQ号码查询*/ 
                    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);      
} 
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)  /*按QQ号码查询*/ 
{ 
    EMP *emp1; 
    emp1=emp_first; 
    while(emp1) 
    { 
        if(strcmp(name,emp1->qq)==0) return emp1; 
        emp1=emp1->next; 
    } 
    return NULL; 
} 
void displayemp(EMP *emp,char *field,char *name) /*显示查找员工信息*/  
{ 
    if(emp) 
    {                       
        printf("\n%s:%s 信息如下:\n",field,name);  
        bound('_',20); 
        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('_',20); 
    }else{ 
        bound('_',40); 
        printf("系统中没有%s为:%s 的员工信息!\n",field,name);         
    } 
    return;  
} 
 
void summaryemp()  /*员工统计*/ 
{ 
    EMP *emp1; 
    int sum=0,num=0,man=0,woman=0; 
    emp1=emp_first; 
    while(emp1) 
    { 
        num++;/*员工人数*/ 
        sum+=emp1->salary; 
        if(strcmp(emp1->sex,"男")==0) man++; 
        else woman++; 
        emp1=emp1->next; 
    } 
    printf("\n员工统计信息:\n"); 
    bound('_',20); 
    printf("总人数:%d\n",num); 
    printf("男:%d\n",man); 
    printf("女:%d\n",woman); 
    printf("工资总额:%d\n",sum); 
    bound('_',20); 
    printf("按任意键返回!\n"); 
    getch(); 
    return;  
}  
 
 |   
 
评分
- 
|  参与人数 1 | 荣誉 +4 | 
鱼币 +4 | 
收起
理由
 | 
 
  冬冬
 |  + 4 | 
 + 4 | 
好长的代码,哥佩服,希望下次别这样跌代码. | 
 
 
查看全部评分
 
 
 
 
 
 |