C语言程序设计 之 职工信息管理系统

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xiangchengshishuiguo/article/details/71723772

要做程序设计,好烦哦……




#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define N sizeof(struct works)
struct works
{
    int   W_number;
    char  name[20];
    char  sex[5];
    char   W_date_birth[11];
    char  xueli[20];
    char  position[15];
    char  gongzi[20];
    char  address[20];
    char  telephone[11];
    works()
    {
        next=NULL;
    }
    struct works *next;
};
struct works *creat()                               //                建立链表
{
    struct works *head,*p1,*p2;
    int n=1;
    char a;
    printf("输入职工信息:\n");
    do
    {
        p1=(struct works *)malloc(N);/*为p1开辟一个新单元*/
        printf("\n职工号:");
        scanf("%d",&p1->W_number);
        printf("姓名:");
        scanf("%s",p1->name);
        printf("性别:");
        scanf("%s",p1->sex);
        printf("出生年月:");
        scanf("%s",p1->W_date_birth);
        printf("学历:");
        scanf("%s",p1->xueli);
        printf("职务:");
        scanf("%s",p1->position);
        printf("工资:");
        scanf("%s",p1->gongzi);
        printf("住址:");
        scanf("%s",p1->address);
        printf("电话号码:");
        scanf("%s",p1->telephone);
        p1->next=NULL;
        if(n==1)/*n=1时接入表头*/
            head=p1;
        else
            p2->next=p1;/*n!=1时接入表尾*/
        p2=p1;
        n++;
        printf("\n是否输入下一个员工信息?(y/n)\n");
        getchar();
        a=getchar();
    }
    while(a=='y'); /*执行是否输入下一个数据*/
    return head;
}

struct works *cut(struct works *head)                              //              删除
{
    struct works *p1,*p2;
    int   W_number;
    printf("输入要删除职工的职工号:\n");
    scanf("%d",&W_number);
    p1=head;
    while (p1->W_number!=W_number&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(p1->W_number==W_number)/*找到了*/
    {
        if(head==p1)
        {
            head=head->next;
        }
        else
        {
            p2->next=p1->next;
        }
        free(p1);
        printf("\n删除%d职工成功!\n",W_number);
    }
    else/*没找到*/
    {
        printf("\n未找到职工号为%d职工!\n",W_number);
    }
    return head;
}

struct works *insert(struct works *head)               //             插入
{
    struct works *p1,*p3;
    works *p2=new works();
    int    W_number;
    printf("\n插入某个职工之后:");
    scanf("%d", &W_number);
    printf("\n输入将要插入职工的数据:\n");
    p3=(struct works *)malloc(N);
    p1=head;
    while(p1->W_number!=W_number&&p1->next!=NULL)
    {
        p1=p1->next;
    }
    if(p1->W_number!=W_number)
    {
        printf("\n未找到%d职工!\n",W_number);
    }
    else
    {
        printf("\n职工号:");
        scanf("%d",&p3->W_number);
        printf("姓名:");
        scanf("%s",p3->name);
        printf("性别:");
        scanf("%s",p3->sex);
        printf("出生年月:");
        scanf("%s",p3->W_date_birth);
        printf("学历:");
        scanf("%s",p3->xueli);
        printf("职务:");
        scanf("%s",p3->position);
        printf("工资:");
        scanf("%s",p3->gongzi);
        printf("住址:");
        scanf("%s",p3->address);
        printf("电话号码:");
        scanf("%s",p3->telephone);
        p2=p1->next;
        p1->next=p3;
        p3->next=p2;
    }
    return head;
}

struct works *seek1(struct works *head)             //                   查找
{
    struct works *p;
    int W_number;
    p=head;
    printf("\n输入查找职工的职工号:\n");
    scanf("%d", &W_number);
    while(p->W_number!=W_number&&p->next!=NULL)
    {
        p=p->next;
    }
    if(p->W_number==W_number)/*找到了*/
    {
        printf("\n职工号\t姓名\t性别\t出生年月\t学历\t职务\t工资\t住址\t电话号码\n");
        printf("%d\t%s\t%s\t%s\t\t%s\t%s\t%s\t%s\t%s\n",p->W_number,p->name,p->sex,p->W_date_birth,p->xueli,p->position,p->gongzi,p->address,p->telephone);

    }
    else/*没有找到*/
    {
        printf("\n未找到该职工!\n");
    }
    return p;
}
struct works *seek2(struct works *head)
{
    struct works *p;
    char w_name[20];
    p=head;
    printf("请输入要查找的职工姓名:\n");
    scanf("%s",w_name);
    while(strcmp(p->name,w_name)!=0&&p->next!=NULL)
    {
        p=p->next;
    }
    if(strcmp(p->name,w_name)!=0)
    {
        printf("\n未找到该职工!\n");
    }
    else
    {
        printf("\n职工号\t姓名\t性别\t出生年月\t学历\t职务\t工资\t住址\t电话号码\n");
        printf("%d\t%s\t%s\t%s\t\t%s\t%s\t%s\t%s\t%s\n",p->W_number,p->name,p->sex,p->W_date_birth,p->xueli,p->position,p->gongzi,p->address,p->telephone);
    }
    return p;
}

struct works *xiugai(struct works *head)             //                   修改
{
    struct works *p;
    int W_number;
    p=head;
    printf("\n输入要修改职工的职工号:");
    scanf("%d",&W_number);
    while(p->W_number!=W_number&&p->next!=NULL)
    {
        p=p->next;/*后一一个结点*/
    }
    if(p->W_number==W_number)/*找到了*/
    {
        printf("\n职工号:");
        scanf("%d",&p->W_number);
        printf("姓名:");
        scanf("%s",p->name);
        printf("性别:");
        scanf("%s",p->sex);
        printf("出生年月:");
        scanf("%s",p->W_date_birth);
        printf("学历:");
        scanf("%s",p->xueli);
        printf("职务:");
        scanf("%s",p->position);
        printf("工资:");
        scanf("%s",p->gongzi);
        printf("住址:");
        scanf("%s",p->address);
        printf("电话号码:");
        scanf("%s",p->telephone);
        printf("\n修改成功!\n");
    }
    else/*未找到*/
    {
        printf("\n未找到该职工 !\n");
    }
    return head;
}
struct works *sort(struct works *head)
{
    struct works *p1,*p2,*p3;

    char a[20];
    int w_number;
    p3=(struct works *)malloc(N);
    for(p1=head; p1!=NULL; p1=p1->next)
    {
        for(p2=p1->next; p2!=NULL; p2=p2->next)
        {
            if(p1->W_number>p2->W_number)
            {
                p3->W_number=p2->W_number;
                p2->W_number=p1->W_number;
                p1->W_number=p3->W_number;
                strcpy(a,p1->name);
                strcpy(p1->name,p2->name);
                strcpy(p2->name,a);
                strcpy(a,p1->sex);
                strcpy(p1->sex,p2->sex);
                strcpy(p2->sex,a);
                strcpy(a,p1->W_date_birth);
                strcpy(p1->W_date_birth,p2->W_date_birth);
                strcpy(p2->W_date_birth,a);
                strcpy(a,p1->xueli);
                strcpy(p1->xueli,p2->xueli);
                strcpy(p2->xueli,a);
                strcpy(a,p1->position);
                strcpy(p1->position,p2->position);
                strcpy(p2->position,a);
                strcpy(a,p1->gongzi);
                strcpy(p1->gongzi,p2->gongzi);
                strcpy(p2->gongzi,a);
                strcpy(a,p1->address);
                strcpy(p1->address,p2->address);
                strcpy(p2->address,a);
                strcpy(a,p1->telephone);
                strcpy(p1->telephone,p2->telephone);
                strcpy(p2->telephone,a);
            }
        }

    }
    return head;
}
void save(struct works *head)                              //                       保存
{
    FILE *fp;
    struct works *p;
    p=head;
    if((fp=fopen("D:\\file.txt","w"))==NULL)
    {
        printf("打开失败\n");
        exit(0);
    }
    while(p!=NULL)
    {
        fprintf(fp,"%d %s %s %s %s %s %s %s %s\n",p->W_number,p->name,p->sex,p->W_date_birth,p->xueli,p->position,p->gongzi,p->address,p->telephone);
        p=p->next;
    }
    printf("保存成功\n");
    fclose(fp);
    printf("\n");

}
void print(struct works *head)
{
    struct works *p;
    p=head;
    printf("\n职工号\t姓名\t性别\t出生年月\t学历\t职务\t工资\t住址\t电话号码\n");
    while(p!=NULL)
    {

        printf("%d\t%s\t%s\t%s\t\t%s\t%s\t%s\t%s\t%s\n",p->W_number,p->name,p->sex,p->W_date_birth,p->xueli,p->position,p->gongzi,p->address,p->telephone);
        p=p->next;
    }

}
void menu1()
{

    printf("*********欢迎进入查询系统***********\n");
    printf("\t\t》》》1.按职工号查找\n");
    printf("\t\t》》》2.按姓名查找\n");
    printf("\t\t》》》3.退出\n");
    printf("************谢谢使用*******************\n");


}
void seek(struct works *head)
{
    int i;
    char ch;
    printf("请选择查找方式\n");
    scanf("%d",&i);
    do
    {
        switch(i)
        {
        case 1:
            seek1(head);
            break;
        case 2:
            seek2(head);
            break;
        case 3:
            printf("成功退出\n");
            break;
        default:
            printf("请输入正确指令!\n");
            break;

        }
        printf("是否继续查询是(Y/y)否(N/n)\n");
        getchar();
        ch=getchar();
    }
    while(ch=='Y'||ch=='y');

}

void menu()                                                  //              菜单
{
    printf("********************************************************************************\n");
    printf("\t\t\t  欢迎使用职工管理系统\n");
    printf("\t**************************************************************\n");
    printf("\t\t\t     1.录入职工信息。\n");
    printf("\t\t\t     2.查询职工信息。\n");
    printf("\t\t\t     3.删除职工信息。\n");
    printf("\t\t\t     4.插入职工信息。\n");
    printf("\t\t\t     5.修改职工信息。\n");
    printf("\t\t\t     6.打印职工信息。\n");
    printf("\t\t\t     7.按职工号排序。\n");
    printf("\t\t\t     8.保存职工信息。\n");
    printf("\t\t\t     9.退出职工信息系统。\n");
    printf("********************************************************************************\n");
    printf("\t\t\t     选择对应编号:");
}
int main()
{
    struct works *head;

    char ch;
    int a;
    while(1)
    {
        menu();
        scanf("%d",&a);
        switch(a)
        {
        case 1:
            head=creat();
            break;
        case 2:
            menu1();
            seek(head);
            break;
        case 3:
            head=cut(head);
            break;
        case 4:
            head=insert(head);
            break;
        case 5:
            head=xiugai(head);
            break;
        case 6:
            print(head);
            break;
        case 7:
            head=sort(head);
            print(head);
            break;
        case 8:
            save(head);
            break;
        case 9:
            exit(0);
            break;
            printf("请输入正确指令!\n");
            break;
        }
    }

    printf("************************************谢谢使用************************************\n");
    return 0;
}


展开阅读全文

没有更多推荐了,返回首页