| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 439 人关注过本帖
标题:求程序每一步详细解析》》》》》》》》》》》》》
只看楼主 加入收藏
笛c
Rank: 2
等 级:论坛游民
帖 子:34
专家分:24
注 册:2012-11-17
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:3 
求程序每一步详细解析》》》》》》》》》》》》》
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h> //用于支持getch()函数

typedef struct student
{
    char no[20];
    char name[10];
    char sex[7];
    char date[9];
    char edu[20];
    char work[20];
    char salary[7];
    char addr[40];
    char phone[20];
    struct student *next;
}STU;

int detect(int aim,int norm)
{
    if (aim > norm-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int detrep(STU *head,char str[])
{
    STU *tm = head;
    while (tm != NULL)
    {
        if (strcmp(tm->no,str) == 0)
        {
            return 1;
        }
        else
        {
            tm = tm->next;
        }
    }
    if (tm == NULL)
    {
        return 0;
    }
}

int detreps(STU *head,char str[])
{
    STU *tm = head;
    while (tm != NULL)
    {
        if (strcmp(tm->name,str) == 0)
        {
            return 1;
        }
        else
        {
            tm = tm->next;
        }
    }
    if (tm == NULL)
    {
        return 0;
    }
}

void split (char str[],STU *tm)
{
    char *p = strtok(str, "|");
    if (p) strcpy(tm->no,p);
    p = strtok(NULL, "|");
    if (p) strcpy(tm->name,p);
    p = strtok(NULL, "|");
    if (p) strcpy(tm->sex,p);
    p = strtok(NULL, "|");
    if (p) strcpy(tm->date,p);
    p = strtok(NULL, "|");
    if (p) strcpy(tm->edu,p);
    p = strtok(NULL, "|");
    if (p) strcpy(tm->work,p);
    p = strtok(NULL, "|");
    if (p) strcpy(tm->salary,p);
    p = strtok(NULL, "|");
    if (p) strcpy(tm->addr,p);
    p = strtok(NULL, "|");
    if (p) strcpy(tm->phone,p);
}

int load(char str[][155],STU *tm,STU *p,STU *q)
{
    FILE *fp;
    int i=0;
    fp = fopen("Employee.txt","r");
        if (fp == NULL)
        {
            puts("读取本地文件错误,请插入新信息!");
            getch();
            exit(1);
        }
    while(fgets(str[i],155,fp) != NULL)
    {
        if (i == 0)
        {
            split(str[i],tm);
            p = tm;
            p->next = NULL;
        }
        else
        {
            q = (STU *)malloc(sizeof(STU));
            split(str[i],q);
            q->next = NULL;
            p->next = q;
            p = q;
            p->next = NULL;
        }
        i++;
    }
    fclose(fp);
    return i;
}

void menu()
{
    printf("******************************************************************\n");
    printf("**                         1.读取职工信息                       **\n");
    printf("**                         2.插入职工信息                       **\n");
    printf("**                         3.修改职工信息                       **\n");
    printf("**                         4.查询职工信息                       **\n");
    printf("**                         5.删除职工信息                       **\n");
    printf("**                         6.退出管理系统                       **\n");
    printf("******************************************************************\n");
}

void selme()
{
    printf("******************************************************************\n");
    printf("**                       1.依据职工编号查询                     **\n");
    printf("**                       2.依据职工姓名查询                     **\n");
    printf("******************************************************************\n");
}

void line()
{
    printf("------------------------------------------------------------------\n");
    printf("------------------------------------------------------------------\n");
}

void insert(STU *head)
{
    char str[9][40];
    FILE *fp;
    fp = fopen("Employee.txt","a");
    if (fp == NULL)
    {
        puts("插入职工信息失败!");
        getch();
        exit(1);
    }
    getchar();                                            //刷新缓冲
    puts("请输入职工号:");
    gets(str[0]);
    while(detect(strlen(str[0]),20))
    {
        puts("您输入的内容过多,请重新输入:");
        gets(str[0]);
    }
    while(detrep(head,str[0]))
    {
        puts("职工编号不允许重复,请重新输入:");
        gets(str[0]);
    }
    puts("请输入职工姓名:");
    gets(str[1]);
    while(detect(strlen(str[1]),10))
    {
        puts("您输入的内容过多,请重新输入:");
        gets(str[1]);
    }
    puts("请输入职工性别:");
    gets(str[2]);
    while(detect(strlen(str[2]),7))
    {
        puts("您输入的内容过多,请重新输入:");
        gets(str[2]);
    }
    puts("请输入职工出生年月日:");
    gets(str[3]);
    while(detect(strlen(str[3]),9))
    {
        puts("您输入的内容过多,请重新输入:");
        gets(str[3]);
    }
    puts("请输入职工学历:");
    gets(str[4]);
    while(detect(strlen(str[4]),20))
    {
        puts("您输入的内容过多,请重新输入:");
        gets(str[4]);
    }
    puts("请输入职工职务:");
    gets(str[5]);
    while(detect(strlen(str[5]),20))
    {
        puts("您输入的内容过多,请重新输入:");
        gets(str[5]);
    }
    puts("请输入职工工资:");
    gets(str[6]);
    while(detect(strlen(str[6]),7))
    {
        puts("您输入的内容过多,请重新输入:");
        gets(str[6]);
    }
    puts("请输入职工住址:");
    gets(str[7]);
    while(detect(strlen(str[7]),40))
    {
        puts("您输入的内容过多,请重新输入:");
        gets(str[7]);
    }
    puts("请输入联系电话:");gets(str[8]);
    while(detect(strlen(str[8]),20))
    {
        puts("您输入的内容过多,请重新输入:");
        gets(str[8]);
    }
    fputs(str[0],fp);fputs("|",fp);
    fputs(str[1],fp);fputs("|",fp);
    fputs(str[2],fp);fputs("|",fp);
    fputs(str[3],fp);fputs("|",fp);
    fputs(str[4],fp);fputs("|",fp);
    fputs(str[5],fp);fputs("|",fp);
    fputs(str[6],fp);fputs("|",fp);
    fputs(str[7],fp);fputs("|",fp);
    fputs(str[8],fp);fputs("\n",fp);
    fclose(fp);
}

STU * amend(STU *head)
{
    STU *tm = head;
    char str[20],strs[9][40];
    puts("请输入要修改的职工序号:");
    gets(str);
    while(detect(strlen(str),20))
    {
        puts("您输入的内容过多,请重新输入:");
        gets(str);
    }
    if (detrep(head,str)==0)
    {
        puts("该职工序号不存在!");
        return head;
    }
    while (tm != NULL)
    {
        if (strcmp(tm->no,str) == 0)
        {
            puts("请输入职工姓名:");
            gets(strs[1]);
            while(detect(strlen(strs[1]),10))
            {
                puts("您输入的内容过多,请重新输入:");
                gets(strs[1]);
            }
            strcpy(tm->name,strs[1]);
            puts("请输入职工性别:");
            gets(strs[2]);
            while(detect(strlen(strs[2]),7))
            {
                puts("您输入的内容过多,请重新输入:");
                gets(strs[2]);
            }
            strcpy(tm->sex,strs[2]);
            puts("请输入职工出生年月日:");
            gets(strs[3]);
            while(detect(strlen(strs[3]),9))
            {
                puts("您输入的内容过多,请重新输入:");
                gets(strs[3]);
            }
            strcpy(tm->date,strs[3]);
            puts("请输入职工学历:");
            gets(strs[4]);
            while(detect(strlen(strs[4]),20))
            {
                puts("您输入的内容过多,请重新输入:");
                gets(strs[4]);
            }
            strcpy(tm->edu,strs[4]);
            puts("请输入职工职务:");
            gets(strs[5]);
            while(detect(strlen(strs[5]),20))
            {
                puts("您输入的内容过多,请重新输入:");
                gets(strs[5]);
            }
            strcpy(tm->work,strs[5]);
            puts("请输入职工工资:");
            gets(strs[6]);
            while(detect(strlen(strs[6]),7))
            {
                puts("您输入的内容过多,请重新输入:");
                gets(strs[6]);
            }
            strcpy(tm->salary,strs[6]);
            puts("请输入职工住址:");
            gets(strs[7]);
            while(detect(strlen(strs[7]),40))
            {
                puts("您输入的内容过多,请重新输入:");
                gets(strs[7]);
            }
            strcpy(tm->addr,strs[7]);
            puts("请输入联系电话:");gets(strs[8]);
            while(detect(strlen(strs[8]),20))
            {
                puts("您输入的内容过多,请重新输入:");
                gets(strs[8]);
            }
            strcat(strs[8],"\n");
            strcpy(tm->phone,strs[8]);
            puts("修改职工信息成功!");
            return head;
        }
        else
        {
            tm = tm->next;
        }
    }
    return head;
}

STU * del(STU *head)
{
    char str[3][20];
    STU *tm = head,*p=head,*q;
    char no[20];
    int dele=0;
    getchar();
    puts("请输入要删除的职工号:");gets(no);
    while (tm != NULL)
    {
        if (strcmp(head->no,no)==0)                        //二者相等
        {
            dele = 1;
            tm = head->next;
            head = tm;
            p = tm;
        }
        else
        {
            if (strcmp(tm->no,no)==0)
            {
                dele = 1;
                p->next = tm->next;;
            }
            else
            {
                p=tm;
            }
            tm = tm->next;
        }
    }
    if (dele == 0)
    {
        puts("该职工不存在!");
        line();
    }
    return head;
}

void search(STU *head)
{
    STU *tm = head;
    char str[20],strs[9][40];
    int sel;
    while(1)
    {
        puts("请选择查询的方式:");
        selme();
        printf("[ ]\b\b");
        scanf("%d",&sel);
        getchar();
        switch (sel)
        {
        case 1:
            puts("请输入要查找的职工序号:");
            gets(str);
            while(detect(strlen(str),20))
            {
                puts("您输入的内容过多,请重新输入:");
                gets(str);
            }
            if (detrep(head,str)==0)
            {
                puts("该职工序号不存在!");
                return;
            }
            while (tm != NULL)
            {
                if (strcmp(tm->no,str) == 0)
                {
                    printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n","职工号","姓 名","性 别","生 日","学 历","职 务","工资","住 址","联系电话");
                    printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",tm->no,tm->name,tm->sex,tm->date,tm->edu,tm->work,tm->salary,tm->addr,tm->phone);
                }
                tm = tm->next;
            }
            return ;
            break;
        case 2:
            puts("请输入要查找的职工姓名:");
            gets(str);
            while(detect(strlen(str),10))
            {
                puts("您输入的内容过多,请重新输入:");
                gets(str);
            }
            if (detreps(head,str)==0)
            {
                puts("该职工姓名不存在!");
                return;
            }
            while (tm != NULL)
            {
                if (strcmp(tm->name,str) == 0)
                {
                    printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n","职工号","姓 名","性 别","生 日","学 历","职 务","工资","住 址","联系电话");
                    printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",tm->no,tm->name,tm->sex,tm->date,tm->edu,tm->work,tm->salary,tm->addr,tm->phone);
                }
                tm = tm->next;
            }
            return ;
            break;
        default :
            puts("您的选择有误,请重新选择:");
            break;
        }
    }
}

void save (STU *head)
{
    FILE *fp;
    STU *tm = head;
    fp = fopen("Employee.txt","w");
    while (tm != NULL)
    {
        fputs(tm->no,fp);fputs("|",fp);
        fputs(tm->name,fp);fputs("|",fp);
        fputs(tm->sex,fp);fputs("|",fp);
        fputs(tm->date,fp);fputs("|",fp);
        fputs(tm->edu,fp);fputs("|",fp);
        fputs(tm->work,fp);fputs("|",fp);
        fputs(tm->salary,fp);fputs("|",fp);
        fputs(tm->addr,fp);fputs("|",fp);
        fputs(tm->phone,fp);
        tm = tm->next;
    }
    fclose(fp);
}

void show(STU *head)
{
    STU *tm = head;
    printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n","职工号","姓 名","性 别","生 日","学 历","职 务","工资","住 址","联系电话");
    while(tm != NULL)
    {
        printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",tm->no,tm->name,tm->sex,tm->date,tm->edu,tm->work,tm->salary,tm->addr,tm->phone);
        tm = tm->next;
    }
}

int main()
{
    int sel;
    char str[100][155];
    STU *head,*p,*q=NULL;
    p = (STU *)malloc(sizeof(STU));
    head = (STU *)malloc(sizeof(STU));
    head->next = NULL;
    puts("              --------------------------------------");
    puts("              |     欢迎使用该职工信息管理系统,请 |\n              | 输入菜单中相应的序号并按回车键完成 |\n              | 功能的选择。               |");
    puts("              --------------------------------------");
    menu();line();
    while(1)
    {
        printf("[ ]\b\b");
        scanf("%d",&sel);line();
        switch (sel)
        {
        case 1:
            if(load(str,head,p,q)==0)
            {
                puts("请先插入职工信息!");
                menu();line();
                break;
            }
            show(head);
            menu();line();
            break;
        case 2:
            insert(head);
            puts("插入职工信息成功!");
            line();menu();line();
            break;
        case 3:
            getchar();
            head = amend(head);
            show(head);
            save(head);
            menu();line();
            break;
        case 4:
            search(head);
            menu();line();
            break;
        case 5:
            if(load(str,head,p,q)==0)
            {
                puts("请先插入职工信息!");
                menu();line();
                break;
            }
            head = del(head);
            save(head);
            show(head);
            menu();line();
            break;
        case 6:
            exit(0);
        default :
            printf("非法的菜单选择!\n");
            menu();line();
            break;
        }   
    }
    return 0;
 }

搜索更多相关主题的帖子: phone include return 
2012-12-31 15:33
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:5 
从哪抄的代码 啊   

还要每一步都详细注释  

既如此  给你转到程序供求 看看是否有人助你

DO IT YOURSELF !
2012-12-31 15:37
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:5 
祝楼主新年愉快
2012-12-31 18:44
青春无限
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江苏
等 级:贵宾
威 望:24
帖 子:3452
专家分:19340
注 册:2012-3-31
收藏
得分:5 
新年愉快

学 会看代码…学习写程序…学会搞开发…我的目标!呵呵是不是说大话啊!!一切皆可能
2012-12-31 21:49
快速回复:求程序每一步详细解析》》》》》》》》》》》》》
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031806 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved