CHANsM 2020-07-01 17:31 采纳率: 100%
浏览 91
已采纳

大佬们,为啥这个程序用一次查询功能再用浏览功能就会多打印一遍啊

大佬们,为啥这个程序用一次查询功能再用浏览功能就会多打印一遍啊
图片说明图片说明图片说明

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <malloc.h>
#include<conio.h>
#include<Windows.h>
struct worker
{
    char ID[20];
    char name[20];
    char sex[20];
    char birth[20];
    char education[20];
    char post[20];
    char wages[20];
    char address[30];
    char phone[20];

}; 
struct Node 
{
    struct worker data;
    struct Node* next;
};
struct Node* createList()//创建链表 
{
    struct Node* listHeadNode = (struct Node*)malloc(sizeof(struct Node));
    listHeadNode->next = NULL;
    return listHeadNode;
}
struct Node* createNode(struct worker data)//创建结点 
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;   //信息初始化 
    newNode->next = NULL;
    return newNode;
}
void insertNodeByHead(struct Node* listHeadNode, struct worker data)//插入结点 
{
    struct Node* newNode = createNode(data);
    newNode->next = listHeadNode->next;
    listHeadNode->next = newNode;
}
void readInfoFromFile(char *fileName, struct Node* listHeadNode)//读取文件 
{
    FILE *fp = fopen(fileName, "r");
    if (fp == NULL)
    {
        fp = fopen(fileName, "w");
    }
    struct worker tempData;
    while (fscanf(fp, "%s %s %s %s %s %s %s %s %s\n", tempData.ID, tempData.name, 
        tempData.sex, tempData.birth, tempData.education, tempData.post, tempData.wages, tempData.address, tempData.phone) != EOF)
    {
        insertNodeByHead(listHeadNode, tempData);
        memset(&tempData, 0, sizeof(tempData));
    }
    fclose(fp);
}
void saveInfoToFile(char *fileName, struct Node* listHeadNode)//保存到文件 
{
    FILE *fp = fopen(fileName, "w");
    struct Node* pMove = listHeadNode->next;
    while (pMove)
    {
        fprintf(fp,"%s %s %s %s %s %s %s %s %s\n", pMove->data.ID, pMove->data.name,
            pMove->data.sex, pMove->data.birth,pMove->data.education,pMove->data.post, pMove->data.wages,pMove->data.address,pMove->data.phone);
        pMove = pMove->next;
    }
    fclose(fp);
}

void printList(struct Node* listHeadNode)//打印:浏览信息
{
    struct Node* pMove = listHeadNode->next;
    while (pMove)
    {
        printf("%s %s %s %s %s %s %s %s %s\n", pMove->data.ID, pMove->data.name, pMove->data.sex,pMove->data.birth,pMove->data.education, pMove->data.post, pMove->data.wages, pMove->data.address, pMove->data.phone);
        pMove = pMove->next;
    }
    printf("\n");
}
void printNode(struct Node* curNode)
{
    printf("\t");
    printf("%s %s %s %s %s %s %s %s %s\n", curNode->data.ID, curNode->data.name, curNode->data.sex,curNode->data.birth,curNode->data.education, curNode->data.post, curNode->data.wages,curNode->data.address,curNode->data.phone);
}
void deleteNodeByAppoinNum(struct Node* listHeadNode,char *ID)//按工号删除 
{
    struct Node* posFrontNode = listHeadNode;
    struct Node* posNode = listHeadNode->next;
    if (posNode == NULL)
    {
        printf("无相关内容,无法删除!\n");
        return;
    }
    else
    {
        while (strcmp(posNode->data.ID,ID))
        {
            posFrontNode = posNode;
            posNode = posFrontNode->next;
            if (posNode == NULL)
            {
                printf("无相关内容,无法删除!\n");
                return;
            }
        }
        posFrontNode->next = posNode->next;
        free(posNode);
    }
}
struct Node* searchNodeByAppoinNum(struct Node* listHeadNode, char *ID)//查找 
{
    struct Node* pMove = listHeadNode->next;
    if (pMove == NULL)
        return pMove;
    else
    {
        while (strcmp(pMove->data.ID, ID))
        {
            pMove = pMove->next;
            if (pMove == NULL)
                break;
        }
        return pMove;
    }
}

struct Node* list = NULL;
void menu()
{
    printf("--------------[职工管理系统]--------------\n");
    printf("\t\t0.退出系统\n");
    printf("\t\t1.创建信息\n");
    printf("\t\t2.浏览信息\n");
    printf("\t\t3.删除信息\n");
    printf("\t\t4.修改信息\n");
    printf("\t\t5.查询信息\n");

    printf("------------------------------------------\n");
    printf("请输入0-5来执行操作"); 
} 
void keydown()
{
    int userkey;
    struct worker tempData;
    scanf("%d",&userkey);
    switch(userkey)
    {
        case 0:
            printf("\t\t0.退出系统\n");
            system("pause");
            exit(0);
            break;
        case 1:
            printf("\t\t1.创建信息\n");
            printf("请输入工号,姓名,性别(M&F),出生年月,学历,职务,工资,住址,电话:\n");
            printf("\t\t(每个数据以一个空格分开)\n");
            scanf("%s %s %s %s %s %s %s %s %s\n",tempData.ID, tempData.name, 
        tempData.sex, tempData.birth, tempData.education, tempData.post, tempData.wages, tempData.address, tempData.phone);
            insertNodeByHead(list, tempData);
            saveInfoToFile("worker.txt", list);
            break;
        case 2:
            printf("\t\t2.浏览信息\n");
            printList(list);
            break;
        case 3:
            printf("\t\t3.删除信息\n");
            printf("请输入需要删除的职工的工号:");
            scanf("%s", tempData.ID);
            deleteNodeByAppoinNum(list, tempData.ID);
            saveInfoToFile("worker.txt", list);
            break;

        case 4:
            printf("\t\t4.修改信息\n");
            printf("请输入要修改的职工的工号:");
            scanf("%s", tempData.ID);
        if (searchNodeByAppoinNum(list, tempData.ID) == NULL)
        {
            printf("未找到相关信息!\n");
        }
        else
        {
            struct Node* curNode = searchNodeByAppoinNum(list, tempData.ID);
            printf("请重新输入工号,姓名,性别(M&F),出生年月,学历,职务,工资,住址,电话:\n");
            printf("\t\t(每个数据以一个空格分开)\n"); 

                scanf("%s %s %s %s %s %s %s %s %s",curNode->data.ID, curNode->data.name, 
        curNode->data.sex, curNode->data.birth, curNode->data.education, curNode->data.post, curNode->data.wages, curNode->data.address, curNode->data.phone);
            saveInfoToFile("worker.txt", list);
        }
            break;
        case 5:
            readInfoFromFile("worker.txt", list);
            printf("\t\t5.查询信息\n");
            printf("请输入查找的工号:");
            scanf("%s", tempData.ID);
            if (searchNodeByAppoinNum(list, tempData.ID) == NULL)
        {
            printf("未找到相关信息!\n");
        }
        else
        {
            printNode(searchNodeByAppoinNum(list, tempData.ID));

        }
            break;

        default:
            printf("重新输入\n");
            break;
    }
}
 int main()
{
    list = createList();
    readInfoFromFile("worker.txt", list);
    while(1)
    {
     menu();
     keydown();
     system("pause");
     system("cls");
    }
    return 0;
} 
  • 写回答

2条回答 默认 最新

  • 喜欢喝茶的猫 2020-07-01 21:26
    关注

    在第五个功能前加如下两行代码:

            case 5:
                free(list);
                list = createList();
                readInfoFromFile("worker.txt", list);
                printf("\t\t5.查询信息\n");
                printf("请输入查找的工号:");
                scanf("%s", tempData.ID);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)