Geek.. 2021-06-12 12:48 采纳率: 100%
浏览 67
已结题

为什么不能进行下一步呢?

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
struct teach{
    int xh;
    char gh[20];
    char xm[20];
    int gl;
    char zc[20];
    double gz;
    char dh[13];
    struct teach *next;
};
void menu1();
void menu2();
void menuM();
int load();
struct teach* creatList();
void brow(struct teach *head);
void find(struct teach *head);
void findNo(struct teach *head,char no[]);
struct teach *del(struct teach *head,char gh[]);
int main(){
    int dl=load();
    int choice;
    char gh[20];
    struct teach *head,*p;
    if(dl==1){
        head=creatList();
        while(1){
            menuM();
            printf("请选择操作(0-5):\n");
            scanf("%d",&choice);
            switch(choice){
                case 1:
                    brow(head);
                    break;
                case 2:
                    find(head);
                    break;
                case 3:
                    printf("请输入工号:\n");
                    scanf("%s",gh);
                    p=del(head,gh);
                    if(p!=NULL){
                        printf("序号 工号\t姓名\t工龄\t职称\t\t工资\t\t联系方式\n");
                        printf("%d %s\t%s\t%d\t%s\t\t%-.2f\t\t-s\n",p->xh,p->xm,p->gl,p->zc,p->gz,p->dh);
                    }
                    else
                        printf("删除失败\n");
                    break;
                case 4:
                    break;
                case 5:
                    break;
                default:
                    return 0;        
            }
            system("pause");
            system("cls");
        }
    }
    return 0;
}
struct teach *del(struct teach *head,char gh[]){
    struct teach *pre,*p;
    pre=head;
    p=head->next;
    while(head!=NULL){
        if(strcmp(head->gh,gh)==0){
            pre->next=p->next;
            return p;
        }
        else{
            pre=p;
            p=p->next;
        }
    }
    if(head==NULL)
    return NULL;
}
void findNo(struct teach *head,char no[]){
    head=head->next;
    printf("序号 工号\t姓名\t工龄\t职称\t\t工资\t\t联系方式\n");
    while(head!=NULL){
        if(strcmp(head->gh,no)==0){
            printf("%d %s\t%s\t%d\t%s\t\t%-.2f\t\t-s\n",head->xh,head->xm,head->gl,head->zc,head->gz,head->dh);
            break;
            }
    head=head->next;
    }
    if(head==NULL)
        printf("查无此人!\n");
}
void find(struct teach *head){
    int c,tage;
    char no[20];
    while(1){
        printf("**********************************************\n");
        printf("*        1-----------按教工工号信息查询      *\n");
        printf("*        2-----------按教工工龄信息查询      *\n");
        printf("*        0-----------退出                    *\n");
        printf("**********************************************\n");
        printf("请选择(0-2):\n");
        scanf("%d",&c);
        if(c==1){
            printf("gh:");
            scanf("%s",no);
            findNo(head,no);
        }
        else if(c==2){
            printf("gl:");
            scanf("%d",&tage);
            //findTage(head,tage);
        }
        else
            return ;
    }
}
void menuM(){
    printf("**********************************************\n");
    printf("*        1-----------教工信息浏览            *\n");
    printf("*        2-----------教工信息查询            *\n");
    printf("*        3-----------教工信息删除            *\n");
    printf("*        4-----------教工信息插入            *\n");
    printf("*        5-----------教工信息修改            *\n");
    printf("*        0-----------退出                    *\n");
    printf("**********************************************\n");
}
void brow(struct teach *head){
    printf("序号 工号\t姓名\t工龄\t职称\t\t工资\t\t联系方式\n");
    head=head->next;
    while(head){
        printf("%d %s\t%s\t%d\t%s\t\t%-.2f\t\t-s\n",head->xh,head->xm,head->gl,head->zc,head->gz,head->dh);
        head=head->next;
    }
}
struct teach* creatList(){
    FILE *fp;
    fp=fopen("tea.txt","r");
    if(fp==NULL){
        
    }
    struct teach t,*head,*temp,*tail;
    head=(struct teach*)malloc(sizeof(struct teach));
    head->next=NULL;
    tail=head;
    fscanf(fp,"%d %s %s %d %s %lf %s",&t.xh,t.gh,t.xm,&t.gl,t.zc,&t.gz,t.dh);
    while(!feof(fp)){
        temp=(struct teach*)malloc(sizeof(struct teach));
        temp->xh=t.xh;
        strcpy(temp->gh,t.gh);
        strcpy(temp->xm,t.xm);
        temp->gl=t.gl;
        strcpy(temp->zc,t.zc);
        temp->gz=t.gz;
        strcpy(temp->dh,t.dh);
        temp->next=NULL;
        tail->next=temp;
        tail=temp;
        fscanf(fp,"%d %s %s %d %lf %lf %s",&t.xh,t.gh,t.xm,&t.gl,t.zc,&t.gz,t.dh);
    }
    return head;
}
void menu1(){
        printf("*************************************\n");
        printf("*********1------------登陆***********\n");
        printf("*********0------------退出***********\n");
        printf("*************************************\n");
}
int load(){
    int c=3,dl;
    char name[20],pass[10];
    while(c){
        system("cls");
        menu1();
        printf("请选择(0/1):\n");
        scanf("%d",&dl);
        getchar();
        if(dl==1){
            printf("请输入用户名:\n");
            scanf("%s",name);
            printf("请输入密码:\n");
            scanf("%s",pass);
            c--;
            if(strcmp("aaa",name)==0&&strcmp("123456",pass)==0){//登陆成功创建链表
                    return 1;
            }else if(c!=0){
                    printf("用户名或密码错误,还有%d次机会\n",c);
                    system("pause");
            }
            else
                return 0;    
            }
        else
        return 0;
    }
}

输完用户名和密码就不能往下进行,怎么回事呢?

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2021-06-12 15:20
    关注

    tea.txt文件最好先建立起来,因为代码里除了文件读入数据,没有增加数据的地方,修改完善如下,供参考。:

    #include<stdio.h>
    #include <string.h>
    #include <stdlib.h>
    struct teach{
        int    xh;
        char   gh[20];
        char   xm[20];
        int    gl;
        char   zc[20];
        double gz;
        char   dh[13];
        struct teach *next;
    };
    
    void menu1();
    void menu2();
    void menuM();
    int  load();
    void creatList(struct teach *&head);
    void brow(struct teach *head);
    void find(struct teach *head);
    void findNo(struct teach *head,char no[]);
    void del(struct teach *head,char gh[]);
    
    int main(){
        int  dl=load();
        int  choice;
        char gh[20];
        struct teach *head,*p;
        if(dl==1){
            creatList(head);
            while(1){
                menuM();
                printf("请选择操作(0-5):\n");
                scanf("%d",&choice);
                switch(choice){
                    case 1:
                        brow(head);
                        break;
                    case 2:
                        find(head);
                        break;
                    case 3:
                        printf("请输入工号:\n");
                        scanf("%s",gh);
                        del(head,gh);
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    default:
                        return 0;        
                }
                system("pause");
                system("cls");
            }
        }
        return 0;
    }
    
    void del(struct teach *head,char gh[]){
        if(head==NULL){
            printf("表为空!\n");
            return;
        }
        struct teach *pre,*p;
        pre=head;
        p=head->next;
        while(p!=NULL){
            if(strcmp(p->gh,gh)==0){
                pre->next=p->next;
                printf("删除成功!\n");
                printf("序号 工号\t姓名\t工龄\t职称\t工资\t联系方式\n");
                printf("%d   %s\t%s\t%d\t%s\t\t%-6.2f\t\t%-s\n",
                                       p->xh,p->gh,p->xm,p->gl,p->zc,p->gz,p->dh);
                free(p);
                break;
            }
            pre=p;
            p=p->next;
        }
        if(p == NULL) printf("查无此人!\n");
    }
    
    void findNo(struct teach *head,char no[]){
        if(head==NULL){
            printf("表为空!\n");
            return;
        }
        head=head->next;
        printf("序号 工号\t姓名\t工龄\t职称\t工资\t联系方式\n");
        while(head){
            if(strcmp(head->gh,no)==0){
                printf("%d   %s\t%s\t%d\t%s\t\t%-6.2f\t\t%-s\n",
                  head->xh,head->gh,head->xm,head->gl,head->zc,head->gz,head->dh);
                break;
            }
            head=head->next;
        }
        if(head == NULL) printf("查无此人!\n");
    }
    
    void find(struct teach *head){
        int c,tage;
        char no[20];
        while(1){
            printf("**********************************************\n");
            printf("*        1-----------按教工工号信息查询      *\n");
            printf("*        2-----------按教工工龄信息查询      *\n");
            printf("*        0-----------退出                    *\n");
            printf("**********************************************\n");
            printf("请选择(0-2):\n");
            scanf("%d",&c);
            if(c==1){
                printf("gh:");
                scanf("%s",no);
                findNo(head,no);
            }
            else if(c==2){
                printf("gl:");
                scanf("%d",&tage);
                //findTage(head,tage);
            }
            else
                return ;
        }
    }
    
    void menuM(){
        printf("**********************************************\n");
        printf("*        1-----------教工信息浏览            *\n");
        printf("*        2-----------教工信息查询            *\n");
        printf("*        3-----------教工信息删除            *\n");
        printf("*        4-----------教工信息插入            *\n");
        printf("*        5-----------教工信息修改            *\n");
        printf("*        0-----------退出                    *\n");
        printf("**********************************************\n");
    }
    void brow(struct teach *head){
        if(head==NULL){
            printf("表为空!\n");
            return;
        }
        printf("序号 工号\t姓名\t工龄\t职称\t工资\t联系方式\n");
        head=head->next;
        while(head){
            printf("%d   %s\t%s\t%d\t%s\t\t%-6.2f\t\t%-s\n",
                   head->xh,head->gh,head->xm,head->gl,head->zc,head->gz,head->dh);
            head=head->next;
        }
    }
    
    void  creatList(struct teach *&head){
        FILE *fp;
        fp=fopen("tea.txt","r");
        if(fp==NULL){
           printf("Open file fail!\n");
           head=NULL;
           return;
        }
        struct teach *tmp,*tail;
        head =(struct teach*)malloc(sizeof(struct teach));
        head->next=NULL;
        tail= head;
        while(1){
            tmp=(struct teach*)malloc(sizeof(struct teach));
            tmp->next=NULL;
            if(fscanf(fp,"%d %s %s %d %s %lf %s",
              &tmp->xh,tmp->gh,tmp->xm,&tmp->gl,tmp->zc,&tmp->gz,tmp->dh)!=7) break;
            if(head->next==NULL){
                head->next = tmp;
                tail       = tmp;
            }else{
                tail->next=tmp;
                tail = tmp;
            }
        }
        fclose(fp);
        free(tmp);
    }
    void menu1(){
            printf("*************************************\n");
            printf("*********1------------登陆***********\n");
            printf("*********0------------退出***********\n");
            printf("*************************************\n");
    }
    int load(){
        int c=3,dl;
        char name[20],pass[10];
        while(c){
            system("cls");
            menu1();
            printf("请选择(0/1):\n");
            scanf("%d",&dl);
            getchar();
            if(dl==1){
                printf("请输入用户名:\n");
                scanf("%s",name);
                printf("请输入密码:\n");
                scanf("%s",pass);
                c--;
                if(strcmp("aaa",name)==0&&strcmp("123456",pass)==0){//登陆成功创建链表
                        return 1;
                }else if(c!=0){
                        printf("用户名或密码错误,还有%d次机会\n",c);
                        system("pause");
                }
                else
                    return 0;    
            }
            else
             return 0;
        }
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月31日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。