c语言图书管理系统设计,卡在查找时如果是找不到则程序出错,大神能帮我解决一下吗?

输入是成功,但是如果查找的不是已输入,则程序出错。

#include
#include
#include
//定义一本书
struct book
{
char name[10];
char writer[10];
char publisher[21];
char price[5];
};
struct Node
{
//int data;
struct book data;
struct Node* next;
};
//创建表
struct Node* createList()
{
//结构体变量表示表头
//指针--->变量 动态内存申请
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next = NULL;
return headNode;
}
//创建结点
struct Node* createNode(struct book data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//插入结点
void insertNodeByHead(struct Node* headNode, struct book data)
{
struct Node* newNode = createNode(data);
//表头法
newNode->next = headNode->next;
headNode->next = newNode;
}
//指定位置删除
void deleteAppoinNode(struct Node* headNode, char* name)
{ //struct book
struct Node* posNode = headNode->next;
struct Node* posFrontNode = headNode;
if (posNode == NULL)
{
printf("数据为空,无法删除!\n");
return;
}
//书名是字符串,字符串:strcmp
while (strcmp(posNode->data.name,name))
{
posFrontNode = posNode;
posNode = posFrontNode->next;
if (posNode == NULL)
{
printf("未找到指定位置无法删除!\n");
return;
}
}
//找到了
posFrontNode->next = posNode->next;
free(posNode);
}
//查找功能
struct Node* searchInfoByData(struct Node* headNode, char name)
{
struct Node
pMove = headNode->next;
if (pMove == NULL)
return NULL;
while (strcmp(pMove->data.name, name))
{
pMove = pMove->next;
}
return pMove;
}

//打印
void printList(struct Node* headNode)
{
struct Node* pMove = headNode->next;
//设计到数据的处理
printf("书名\t作者\t出版社\t价格\n");
while (pMove)
{
printf("%s\t%s\t%s\t%s\n", pMove->data.name, pMove->data.writer, pMove->data.publisher, pMove->data.price);
pMove = pMove->next;
}
printf("\n");
}

void menu()
{
//所有操作都同步到文件
printf("-------------【图书信息管理系统】-----------\n");
printf("\t\t1.录入图书信息\n");
printf("\t\t2.浏览图书信息\n");
printf("\t\t3.修改图书信息\n");
printf("\t\t4.删除图书信息\n");
printf("\t\t5.查找图书信息\n");
printf("\t\t0.退出系统\n");
printf("-------------------------------------------\n");
}
struct Node* list = createList();

//接收指令
void keyDown()
{
int choice = 0;
struct book data;
struct Node* pMove = NULL;
scanf("%d", &choice);
switch (choice)
{
case 0:
printf("正常退出!\n");
system("pause");
exit(0);
break;
case 1:
printf("--------------【录入图书信息】-----------\n");
//插入链表
printf("请输入书名,作者,出版社,价格:\n");
fflush(stdin); //清空缓冲区
scanf("%s%s%s%s", data.name, &data.writer, data.publisher, data.price);
insertNodeByHead(list, data);
break;
case 2:
printf("--------------【浏览图书信息】-----------\n");
printList(list); //打印链表
break;
case 3:
printf("--------------【修改图书信息】-----------\n");
printf("请输入要修改的书名:");
scanf("%s", data.name);
pMove = searchInfoByData(list, data.name);
if (pMove == NULL)
{
printf("未找到相关信息,无法修改!\n");
system("pause");
}
else
{
printf("请输入图书名,作者,出版社,价格:");
scanf("%s%s%s%s", data.name, &data.writer, data.publisher, data.price);
strcpy(pMove->data.name, data.name);
strcpy(pMove->data.writer, data.writer);
strcpy(pMove->data.publisher, data.publisher);
strcpy(pMove->data.price, data.price);
}
break;

case 4:
    printf("--------------【删除图书信息】-----------\n");
    printf("请输入删除的书名:");
    scanf("%s", data.name);
    deleteAppoinNode(list, data.name); 
    break;
case 5:
    printf("--------------【查找图书信息】-----------\n");
    printf("请输入要查找的书名:");
    scanf("%s", data.name);
    pMove = searchInfoByData(list, data.name);
    if (pMove == NULL)
    {
        printf("未找到相关信息,无法删除!\n");
        system("pause");
    }
    else
    {
        printf("书名\t作者\t出版社\t价格\n");
        printf("%s\t%s\t%s\t%s\n", pMove->data.name, pMove->data.writer, pMove->data.publisher, pMove->data.price);
    }
    break;
default:
    printf("选择错误,重新输入\n");
    system("pause");
    break;
}

}

int main()
{

while (1)
{
    menu();
    keyDown();  
    system("pause");
    system("cls");
}

system("pause");
return 0;

}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问