温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何用C语言实现简单职工信息管理系统

发布时间:2022-07-22 16:13:40 来源:亿速云 阅读:135 作者:iii 栏目:开发技术

今天小编给大家分享一下如何用C语言实现简单职工信息管理系统的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

代码实现如下:

#include <stdio.h>//输入、输出指令
#include <malloc.h>//在内存中动态的分配size字节的存储区
#include <string.h>//字符串相关操作指令
#include <stdlib.h>//五种类型、一些宏和通用工具函数
#include<conio.h>//通过键盘产生的对应操作
#include <MEMORY.H>//存储访问头文件

typedef struct node
{    
     char   name[20];
     int    age;
     int    worktime;
     char   sex[20];
     char   marrige[20];
     int    grade;
     char   tired[20];
    struct  node *next;
}STU;

void output(STU *head);
STU *Create();
STU *findByName(STU *head,char *name);
STU *findByWorktime(STU *head,int worktime);
STU *findByGrade(STU *head,int grade);
void find(STU *head);
STU *insert(STU *head);
STU *findByNumEx(STU *head,char num[],STU **ppbefore);
STU *del(STU *head);
void update(STU *head);
void sort(STU *head);
void save_info(STU *head);
void copy();
STU *load_info();
char *password();

void main()
{      
    char *p;
     int i,n=0; 
       char a[40];
        printf("        \t\t*******欢迎使用职工信息管理系统********\n\n");  
        printf("        \t\t请输入口令:\n");
        printf("        \t\t----------:\n");
    for(i=0;i<3;i++)
    {   
        n=n+1;
        printf("        \t\t第%d次输入:",i+1);
        p=password();
       if(p!=NULL)
       {
           STU *head;
            int choice;
            int n;
            head=NULL;
             printf("\n        \t\t你是合法用户!\n\n");
        for (i = 0; i < 0x7FFFFFF; i++);
        for (i = 0; i < 0x7FFFFFF; i++);
        for (i = 0; i < 0x7FFFFFF; i++);
        system("cls");
        for(;;)
            {
                system("cls");
                printf("           \t\t*******职工信息管理系统********\n");
                printf("           \t\t* 1.输入职工信息              *\n");
                printf("           \t\t* 2.显示全部职工信息          *\n");
                printf("           \t\t* 3.查询职工信息              *\n");
                printf("           \t\t* 4.新增职工信息              *\n");
                printf("           \t\t* 5.删除职工信息              *\n");
                printf("           \t\t* 6.修改职工信息              *\n");
                printf("           \t\t* 7.对职工信息排序            *\n");
                printf("           \t\t* 8.保存当前信息到文件        *\n");
                printf("           \t\t* 9.备份信息资料到文件        *\n");
                printf("           \t\t* 10.从文件加载职工信息       *\n");
                printf("           \t\t* 0.退出系统                  *\n");
                printf("           \t\t*******************************\n");
                printf("           \t\t请选择(0-9):");
                scanf("%d",&choice);
                if(choice==0) break;
                switch(choice)
                {   
                    case 1: head=Create(); break;
                    case 2: output(head);break;
                    case 3:find(head);break;
                    case 4: head=insert(head); break;
                    case 5: head=del(head); break;
                    case 6: update(head);break;
                    case 7: sort(head);break;
                    case 8: save_info(head);break;
                    case 9: copy();break;
                    case 10:head=load_info();break;
                }
            printf("按任意键继续....");
            getch();
        }
        printf("           \t\t感谢您的使用,再见!\n");
        break;
        }
       else
       { 
           printf("\n        \t\t你是非法用户!\n\n");
           if(n==3)
            printf("           \t\t你的错误口令次数超过3次,程序关闭!\n");
       }
    }
}

STU *Create()
{
    STU *head,*pnew,*pend;
    head=NULL;
    printf("输入职工信息:\n姓名\t年龄\t工龄\t性别\t婚姻\t级别\t是否在职\n");
    for(;;) 
    {
        pnew=(STU *)malloc(sizeof(STU));
        scanf("%s",pnew->name);
        if(strcmp(pnew->name,"0")==0)
        {
            printf("员工信息录入完毕\n");
            break;
        }
        scanf("%d%d%s%s%d%s",&pnew->age,&pnew->worktime,pnew->sex,pnew->marrige,&pnew->grade,pnew->tired);
        pnew->next=NULL; 
        if(head==NULL){
            head=pnew;
            pend=pnew;
        }
        else{
            pend->next=pnew;
            pend=pend->next;
        }
    }
    return head;
}

void output(STU *head)
{
    STU *p;
    printf("职工信息如下:\n姓名\t年龄\t工龄\t性别\t婚姻\t级别\t是否在职\n");
    for(p=head;p!=NULL;p=p->next)
        printf("%s\t%d\t%d\t%s\t%s\t%d\t%s\n",p->name,p->age,p->worktime,p->sex,p->marrige,p->grade,p->tired);
}

void find(STU *head)
{
    STU *p;
    char name[20];
    int worktime,grade,i;
    int choice;
    system("cls");

for(;;)
    {
        system("cls");
        printf("           \t\t*******职工信息查询系统********\n");
        printf("           \t\t* 1.按职工姓名查找            *\n");
        printf("           \t\t* 2.按职工工龄查找            *\n");
        printf("           \t\t* 3.按职工级别查找            *\n");
        printf("           \t\t* 0.退出系统                  *\n");
        printf("           \t\t*******************************\n");
        printf("           \t\t请选择(0-3):");
        scanf("%d",&choice);
        if(choice==0) break;
        switch(choice)
        {   
            case 1:
                    printf("请输入职工姓名:");
                    scanf("%s",name);
                    p=findByName(head,name);break;
            case 2: 
                    printf("请输入职工工龄:");
                    scanf("%d",&worktime);
                    p=findByWorktime(head,worktime);break;
            case 3:
                    printf("请输入职工级别:");
                    scanf("%d",&grade);
                    p=findByGrade(head,grade);break;
        }
        if(p==NULL)
    {    
        printf("无与此职工相关的信息!\a\n");
    }
    else
    {
         if(choice==1)
         {    
             printf("职工信息如下:\n姓名\t年龄\t工龄\t性别\t婚姻\t级别\t是否在职\n"); 
             for(p=head;p!=NULL;p=p->next)
             {
                if(strcmp(p->name,name)==0)
                printf("%s\t%d\t%d\t%s\t%s\t%d\t%s\n",p->name,p->age,p->worktime,p->sex,p->marrige,p->grade,p->tired);
             }
         }
         else if(choice==2)
         {     
             printf("职工信息如下:\n姓名\t年龄\t工龄\t性别\t婚姻\t级别\t是否在职\n");
                for(p=head;p!=NULL;p=p->next)
                 {
                    if(p->worktime==worktime)
                    printf("%s\t%d\t%d\t%s\t%s\t%d\t%s\n",p->name,p->age,p->worktime,p->sex,p->marrige,p->grade,p->tired);
                 }
            }
         else if(choice==3)
         {        
                   printf("职工信息如下:\n姓名\t年龄\t工龄\t性别\t婚姻\t级别\t是否在职\n");
                    for(p=head;p!=NULL;p=p->next)
                    {
                        if(p->grade==grade)
                        printf("%s\t%d\t%d\t%s\t%s\t%d\t%s\n",p->name,p->age,p->worktime,p->sex,p->marrige,p->grade,p->tired);
                    }
        }
    }
    printf("按任意键继续....");
    getch();
}
printf("           \t\t您已退出查询系统\n\n");
}

STU *findByName(STU *head,char *name)
{
    STU *p,*result=NULL;
    for(p=head;p!=NULL;p=p->next)
        if(strcmp(p->name,name)==0)
        {
            result=p;
            break;
        }
return result;
}

STU *findByWorktime(STU *head,int worktime)
{
    STU *p,*result=NULL;
    for(p=head;p!=NULL;p=p->next)
        if(p->worktime==worktime)
        {
            result=p;
            break;
        }
return result;
}

STU *findByGrade(STU *head,int grade)
{
    STU *p,*result=NULL;
    for(p=head;p!=NULL;p=p->next)
        if(p->grade==grade)
        {
            result=p;
            break;
        }
        return result;
}

STU *insert(STU *head)
{
    STU *pnew,*pcur,*pbefore,*p;
    int choice;
    printf("输入职工信息:\n");
    for(;;)
    {
        //1.申请一个新节点并赋值
        pnew=(STU *)malloc(sizeof(STU));
        printf("职工信息如下:\n姓名\t年龄\t工龄\t性别\t婚姻\t级别\t是否在职\n");
        scanf("%s%d%d%s%s%d%s",pnew->name,&pnew->age,&pnew->worktime,pnew->sex,pnew->marrige,&pnew->grade,pnew->tired);
        pnew->next=NULL;
        //2.将新节点插入到链表
        //若链表为空,则直接插入;若链表不为空,则查找位置,然后插入
    if(head==NULL)
    {
        head=pnew;  
    }
    else
    {
        pcur=NULL;
        for(p=head;p!=NULL;pbefore=p,p=p->next)
            if(strcmp(p->name,pnew->name)==1)
            {
                pcur=p;
                break;
            }
            //查找之后,判断pcur是否为空,
            //若pcur为空,则pnew所指节点应插入到链表尾部 
        if(pcur==NULL)
        {
            pbefore->next=pnew; 
        }
        //若pcur非空,则pnew所指节点应插入到pcur之前,pbefore之后,此时,又分两种情况:
        //pcur指向的是首节点,或者pcur指向后续节点;
        else
        {
            if(pcur==head)
            {
                pnew->next=pcur;
                head=pnew;
            }
            else
            {
                pnew->next=pcur;
                pbefore->next=pnew;
            }
        }
    }
    printf("请选择:按1继续添加->按0结束添加\n");
    printf("choice=");
    scanf("%d",&choice);
    if(choice==0)
        {   
            printf("信息添加完毕!\n");
            break;
        }
    }
   return head;
}

STU *del(STU *head)
{
    STU *presult,*pbefore;
    char name[20];
    printf("要删除的职工信息:");
    scanf("%s",&name);
    presult=findByNumEx(head,name,&pbefore);
    if(presult!=NULL)
    {
        printf("找到了!职工信息如下:\n姓名\t年龄\t工龄\t性别\t婚姻\t级别\t是否在职\n");  
        printf("%s\t%d\t%d\t%s\t%s\t%d\t%s\n",presult->name,presult->age,presult->worktime,presult->sex,presult->marrige,presult->grade,presult->tired);    
        if(presult==head)
        {
            head=presult->next;
        }
        else
        {
            pbefore->next=presult->next;
        }
        printf("删除成功---> ");
    }
    else
        printf("无与此职工相关的信息\a\a\n");
    return head;
}

//函数说明:
//  在head所指向链表中,查找学号为num的节点
//  找到后,返回两个值:指向当前节点的指针presult,指向当前节点的前一节点的指针pbefore,
//  presult通过函数返回值返回(即return),pbefore通过输出型参数ppBefore返回
STU *findByNumEx(STU *head,char name[],STU **ppbefore)
{
    STU *p,*presult=NULL,*pbefore=NULL;
    for(p=head;p!=NULL;pbefore=p,p=p->next)
        if(strcmp(p->name,name)==0)
        {
            presult=p;
            break;
        }
    if(p==NULL)
    pbefore=NULL;
    *ppbefore=pbefore;
    return presult;
}

void update(STU *head)
{
    STU *presult;
    char name[20];
    printf("输入要修改员工姓名:\n");
    scanf("%s",name);
    presult=findByName(head,name);
    if(presult==NULL)
        printf("查无此人!无法修改!\a\n");
    else
    {
    printf("找到了!职工信息如下:\n姓名\t年龄\t工龄\t性别\t婚姻\t级别\t是否在职\n");  
    printf("%s\t%d\t%d\t%s\t%s\t%d\t%s\n",presult->name,presult->age,presult->worktime,presult->sex,presult->marrige,presult->grade,presult->tired);    
        printf("请输入修改信息:\n");
        printf("姓名\t年龄\t工龄\t性别\t婚姻\t级别\t是否在职\n");   
            scanf("%s%d%d%s%s%d%s",presult->name,&presult->age,&presult->worktime,presult->sex,presult->marrige,&presult->grade,presult->tired);
        printf("修改完毕---> ");
    }
}

void sort(STU *head)//按级别排序
{
    STU *pi,*pj,*pindex,*p;
    STU temp;
    int len=sizeof(STU)-sizeof(STU *);
    int i,j,n=0;
    for(p=head;p!=NULL;p=p->next)
        n++;
    for(pi=head,i=0;i<n-1;i++,pi=pi->next){
        pindex=pi;
        for(pj=pi->next,j=i+1;j<n;j++,pj=pj->next)
            if(pindex->grade> pj->grade)
                pindex=pj;
            memcpy(&temp,pi,len); 
        memcpy(pi,pindex,len); 
        memcpy(pindex,&temp,len); 
    }
    printf("排序完毕---> ");
}

void save_info(STU *head)
{
    char filename[20];
    FILE *fp;
    STU *p;
    printf("保存当前信息到文件的处理\n");
    printf("请输入文件名:");
    scanf("%s",filename);
    if((fp=fopen(filename,"w"))==NULL)
    {
        printf("无法打开文件\n");
        return;
    }
    fprintf(fp,"全部职工信息如下:\n姓名\t年龄\t工龄\t性别\t婚姻\t级别\t是否在职\n");
    for(p=head;p!=NULL;p=p->next)
    fprintf(fp,"%s\t%d\t%d\t%s\t%s\t%d\t%s\n",p->name,p->age,p->worktime,p->sex,p->marrige,p->grade,p->tired);
    fprintf(fp,"%d",0);
    fclose(fp);
    printf("保存完成!--->");
}

void copy()
{
    FILE *in,*out;
    char infile[40],ch;
    char outfile[40];
    printf("现在进入学生信息文件的备份\n");
    printf("源文件名:");
    scanf("%s",infile);
    printf("备份文件名:");
    scanf("%s",outfile);
    if((in=fopen(infile,"r"))==NULL)
    {
        printf("文件无法打开\n");
        exit(0);
    }
    if((out=fopen(outfile,"w"))==NULL)
    {
        printf("文件无法打开\n");
        exit(0);
    }
    ch=fgetc(in);
    while(ch!=EOF)
    {
        fputc(ch,out);
        ch=fgetc(in);
    }   
        fclose(in);
        fclose(out);
        printf("备份成功!\n");
}

STU *load_info()
{
    STU *head=NULL,*pnew,*pend;
    char filename[20];
    char a[20];
    FILE *fp;
    int i;
    printf("           \t\t文件加载职工信息,输入文件名:");
    scanf("%s",filename);
    if((fp=fopen(filename,"r"))==NULL)
    {
        printf("           \t\t加载失败!\n");
        exit(0);
    }
    for(i=0;i<8;i++)
        fscanf(fp,"%s",a);
    for(;;)
    {
        pnew=(STU *)malloc(sizeof(STU));
        fscanf(fp,"%s",&pnew->name);
            if(strcmp(pnew->name,"0")==0)
                break;
        fscanf(fp,"%d%d%s%s%d%s",&pnew->age,&pnew->worktime,&pnew->sex,&pnew->marrige,&pnew->grade,&pnew->tired);
        pnew->next=NULL;
            if(head==NULL)
        {
            head=pnew;
            pend=pnew;
        }
        else
        {
            pend->next=pnew;
            pend=pnew;
        }
    }
    fclose(fp);
    printf("           \t\t加载成功!\n");
    return head;
}

char *password()
{
    char password[100],c;
    char *result=NULL;
    int e,count;
    e=0;count=0;
    while((c=getch())!=13&&e<100)//13为回车
              { 
                     if(c==8&&e>0)//8为退格
                     {
                            password[e--]='\0';//删除最后一个,并且计数器减1
                            printf("\b \b");//退格
                     }
                     else
                     {
                            password[e++]=c;
                            printf("*");//覆盖*号,当输出空格覆盖前一个字符后,再一次前移,以便下一次输入
                     }
              }
             password[e]='\0';
             if(strcmp(password,"123456")==0)
                result=1;     
            return  result;
}

程序运行效果

如何用C语言实现简单职工信息管理系统

以上就是“如何用C语言实现简单职工信息管理系统”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI