标题:不知道哪里的错,请大侠帮忙
取消只看楼主
someday
Rank: 3Rank: 3
来 自:保定
等 级:论坛游侠
帖 子:83
专家分:116
注 册:2010-10-8
结帖率:70.59%
已结贴  问题点数:20 回复次数:0 
不知道哪里的错,请大侠帮忙
这是我做的一个公司员工信息管理系统,在执行系统前,请在当前文件夹下建立名为data.txt的文本文档。
代码是:
/*公司员工信息管理系统设计与实现*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct CSM
{
    int snum;//工号
    char name[20];//姓名
    char sex[10];//性别
    char level[30];//级别
    int wdnum;//出勤天数
    int mmoney;//月薪
    CSM *next;
}CSM;

CSM *Ppoint;//指向单链表的尾结点

void Rewrite(CSM *l)//文件重写
{
    int judge;
    FILE *fp;
    CSM *r;
    r=l;
    judge=remove("data.txt");
    if(judge==EOF)
        printf("删除旧文件失败,重写文件失败\n");
    else
    {

        if((fp=fopen("data.txt","w+"))==NULL)
        {
            printf("File open error! \n");
            exit(0);
        }
        else
        {
            do
            {
               r=r->next;
               fprintf(fp,"%s %s %s ",r->name,r->sex,r->level);
               fprintf(fp,"%d %d %d ",(*r).snum,(*r).wdnum,(*r).mmoney);
            }while(r!=Ppoint);
        }
    }
}

void Read(CSM *l)//录入以前的记录
{
    CSM *node;
    FILE *fp;
    node=(CSM *)malloc(sizeof(CSM));
    if((fp=fopen("data.txt","r"))==NULL)
    {
        printf("File open error!请在当前文件夹下建立一个名为data的文本文档 \n");
        exit(0);
    }
    else
    {
        while(!feof(fp))
        {
            fscanf(fp,"%s %s %s ",node->name,node->sex,node->level);
            fscanf(fp,"%d %d %d ",&((*node).snum),&((*node).wdnum),&((*node).mmoney));
            node->next=NULL;
            Ppoint->next=node;
            Ppoint=node;
            node=(CSM *)malloc(sizeof(CSM));
        }
    }
    if(fclose(fp))
    {
        printf("Can not close the file! \n");
        exit(0);
    }
}

void Enroll(CSM *l,CSM *p)//信息录入
{
    FILE *fp;
    printf("\n");
    printf("工号(4位数字):");
    scanf("%d",&((*p).snum));
    printf("姓名:");
    scanf("%s",p->name);
    getchar();
    printf("性别:");
    scanf("%s",p->sex);
    getchar();
    printf("级别:");
    scanf("%s",p->level);
    getchar();
    printf("出勤天数(满勤22(天)):");
    scanf("%d",&((*p).wdnum));
    printf("月薪:");
    scanf("%d",&((*p).mmoney));
    p->next=NULL;
    if((fp=fopen("data.txt","a +"))==NULL)
    {
        printf("File open error! \n");
        exit(0);
    }
    fprintf(fp,"%s %s %s ",p->name,p->sex,p->level);
    fprintf(fp,"%d %d %d ",(*p).snum,(*p).wdnum,(*p).mmoney);
    if(fclose(fp))
    {
        printf("Can not close the file! \n");
        exit(0);
    }
    Ppoint->next=p;
    Ppoint=p;
}

void Use_sn_to_check(CSM *l,int key)//按工号进行查找
{
    CSM *mark;
    int sign=0;
    mark=l;
    do
    {
        mark=mark->next;
        if((*mark).snum==key)
        {
            printf("工号是: %d\n",(*mark).snum);
            printf("姓名是: %s\n",(*mark).name);
            printf("性别是: %s\n",(*mark).sex);
            printf("级别是: %s\n",(*mark).level);
            printf("出勤天数是: %d\n",(*mark).wdnum);
            printf("此月薪金是: %d\n",((*mark).mmoney)-(22-((*mark).wdnum))*50);
            sign=1;
        }
        

    }while(mark->next!=NULL);
    if(sign==0)
        printf("查找失败\n");
}

void Use_name_to_check(CSM *l,char key[20])//按姓名进行查找
{
    CSM *mark;
    int sign=0;
    mark=l;
    do
    {
        mark=mark->next;
        if(strcmp(mark->name,key)==0)
        {
            printf("工号是: %d\n",(*mark).snum);
            printf("姓名是: %s\n",(*mark).name);
            printf("性别是: %s\n",(*mark).sex);
            printf("级别是: %s\n",(*mark).level);
            printf("出勤天数是: %d\n",(*mark).wdnum);
            printf("此月薪金是: %d\n",((*mark).mmoney)-(22-((*mark).wdnum))*50);
            sign=1;
        }
    }while(mark->next!=NULL);
    if(sign==0)
        printf("查找失败\n");
}

void Revise(CSM *l,int key)//更改信息
{
    CSM *mark;
    int sign=0,n;
    char judge;
    mark=l;
    do
    {
        mark=mark->next;
        if((*mark).snum==key)
        {
            printf("原信息为:\n");
            printf("工号是: %d\n",(*mark).snum);
            printf("姓名是: %s\n",(*mark).name);
            printf("性别是: %s\n",(*mark).sex);
            printf("级别是: %s\n",(*mark).level);
            printf("出勤天数是: %d\n",(*mark).wdnum);
            printf("月薪是: %d\n",(*mark).mmoney);
            printf("\n");
            printf("1 工号; 2 姓名; 3 性别; 4 级别; 5 出勤天数; 6 月薪; 0 退出修改;\n");
            printf("请选择: \n");
            scanf("%d",&n);
            while(n>=0)
            {
                switch(n)
                {
                case 1:
                    {
                        printf("请更改工号(4位数字): \n");
                        scanf("%d",&((*mark).snum));
                        break;
                    }
                case 2:
                    {
                        printf("请更改姓名: \n");
                        scanf("%s",mark->name);
                        break;
                    }
                case 3:
                    {
                        printf("请更改性别: \n");
                        scanf("%s",mark->sex);
                        break;
                    }
                case 4:
                    {
                        printf("请更改级别: \n");
                        scanf("%s",mark->level);
                        break;
                    }
                case 5:
                    {
                        printf("请更改出勤天数: \n");
                        scanf("%d",&((*mark).wdnum));
                        break;
                    }
                case 6:
                    {
                        printf("请更改月薪: \n");
                        scanf("%d",&((*mark).mmoney));
                        break;
                    }
                case 0:
                    {
                        printf("你确定要退出吗? y/n : ");
                        getchar();
                        judge=getchar();
                        break;
                    }
                }
                if(n==0&&judge=='y')
                    n=-1;
                else
                {
                     printf("1 工号; 2 姓名; 3 性别; 4 级别; 5 出勤天数; 6 月薪; 0 退出;\n");
                     printf("请选择: \n");
                     scanf("%d",&n);
                }
            }
            printf("新信息为:\n");
            printf("工号是: %d\n",(*mark).snum);
            printf("姓名是: %s\n",(*mark).name);
            printf("性别是: %s\n",(*mark).sex);
            printf("级别是: %s\n",(*mark).level);
            printf("出勤天数是: %d\n",(*mark).wdnum);
            printf("月薪是: %d\n",(*mark).mmoney);
            printf("\n");
            sign=1;
        }
    }while(mark->next!=NULL);
    if(sign==0)
        printf("没有找到该信息,信息更改失败\n");
    else
        Rewrite(l);
}
         
void Delete(CSM *l,int key)//删除信息
{
    CSM *mark,*before;
    int sign=0;
    mark=l;
    do
    {
        before=mark;
        mark=mark->next;
        if((*mark).snum==key)
        {
            sign=1;
            printf("该员工信息为: \n");
            printf("工号是: %d\n",(*mark).snum);
            printf("姓名是: %s\n",(*mark).name);
            printf("性别是: %s\n",(*mark).sex);
            printf("级别是: %s\n",(*mark).level);
            printf("出勤天数是: %d\n",(*mark).wdnum);
            printf("月薪是: %d\n",(*mark).mmoney);
            before->next=mark->next;
            free(mark);
            break;
        }
    }while(mark->next!=NULL);
    if(sign==0)
        printf("没有找到该员工的信息,删除失败\n");
    else
        Rewrite(l);
}

void Goover(CSM  *l)//遍历所有
{
    CSM *p;
    p=l;
    if(p==Ppoint)
        printf("当前信息为空\n");
    do
    {
        p=p->next;
        printf("工号 %d,姓名 %s,性别 %s,级别 %s,出勤 %d,月薪 %d\n",(*p).snum,p->name,p->sex,p->level,(*p).wdnum,(*p).mmoney);
        printf("\n");
    }while(p!=Ppoint);
}





int main(void)
{
    int choose,snkey;
    char namekey[20],judge;
    CSM *l,*p;
    l=(CSM *)malloc(sizeof(CSM));
    l->next=NULL;
    Ppoint=l;
    Read(l);//读入以前的记录
    printf("欢迎进入公司员工信息管理系统\n");
    printf("您要进行哪项操作:\n");
    printf("1 录入\n");
    printf("2 按 工号 进行查询\n");
    printf("3 按 姓名 进行查询\n");
    printf("4 进行信息修改\n");
    printf("5 进行员工信息删除\n");
    printf("6 浏览\n");
    printf("0 退出系统\n");
    printf("请选择: ");
    scanf("%d",&choose);
    while(choose>=0)
    {
        system("cls");//清屏
        switch(choose){
          case 1:
              {
                  p=(CSM *)malloc(sizeof(CSM));
                  Enroll(l,p);//录入新信息
                  break;
              }
          case 2:
              {
                  printf("输入工号(4位数字): ");
                  scanf("%d",&snkey);
                  Use_sn_to_check(l,snkey);//按工号进行查询
                  break;
              }
          case 3:
              {
                  printf("输入姓名: ");
                  scanf("%s",namekey);
                  getchar();
                  Use_name_to_check(l,namekey);//按姓名进行查询
                  break;
              }
          case 4:
              {
                  printf("请输入要修改的员工的工号: \n");
                  scanf("%d",&snkey);
                  Revise(l,snkey);//更改信息
                  break;
              }
          case 5:
              {
                  printf("请输入要删除的员工的工号: \n");
                  scanf("%d",&snkey);
                  Delete(l,snkey);//删除员工信息
                  break;
              }
          case 6:
              {
                  printf("浏览结果为:\n");
                  Goover(l);
                  break;
              }
          case 0:
              {
                  printf("你确定要退出吗? y/n : ");
                  getchar();
                  judge=getchar();
                  break;
              }
        }
        if(choose==0&&judge=='y')
            choose=-1;
        else
        {
            printf("1 录入\n");
            printf("2 按 工号 进行查询\n");
            printf("3 按 姓名 进行查询\n");
            printf("4 进行信息修改\n");
            printf("5 进行员工信息删除\n");
            printf("6 浏览\n");
            printf("0 退出系统\n");
            printf("请选择: ");
            scanf("%d",&choose);
        }
    }
    return 0;
}
如果打开程序后,不进行录入,直接选择6,浏览。因为文本文档里此时没有存入数据,本应显示“当前信息为空”。但是竟然有输出,是乱码。应该是在执行Read();数时出的错。有时候进行信息删除功能,即选择5时运行会出错。恳请各位大侠帮忙修改程序,小弟感激不尽。顺便告诉我有没有文件覆盖的语句
搜索更多相关主题的帖子: 公司员工 文件夹 姓名 
2011-01-11 12:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-330932-1-1.html




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

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