最近和组员们用c语言写了一个图书管理系统。我负责的是其中的查找图书和输出全部图书信息两个功能。
全码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct book_info
{
char book_id[20];
char book_name[20];
int book_num;
struct book_info *next;
};
struct book_info *book_init();
struct book_info *book_insert(struct book_info *head, struct book_info *bookd);
struct book_info *book_delete(struct book_info *head, char book_id[20]);
struct book_info *book_find(struct book_info *head, char book_id[20]);
void book_print(struct book_info *head);
int main()
{
int choice, book_num;
char book_id[20], book_name[20];
struct book_info *head, *p;
int size = sizeof(struct book_info);
do
{
printf("欢迎来到图书信息管理系统\n");
printf("输入1初始化图书信息\n");
printf("输入2插入图书信息\n");
printf("输入3删除图书信息\n");
printf("输入4查找指定图书信息\n");
printf("输入5查看全部图书信息\n");
printf("输入0退出\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
head = book_init();
break;
case 2:
printf("请输入图书id、名称、数量\n");
scanf("%s %s %d", book_id, book_name, &book_num);
p = (struct book_info *)malloc(size);
strcpy(p->book_id, book_id);
strcpy(p->book_name, book_name);
p->book_num = book_num;
head = book_insert(head, p);
break;
case 3:
printf("请输入图书id\n");
scanf("%s", book_id);
head = book_delete(head, book_id);
break;
case 4:
printf("请输入图书id\n");
scanf("%s", book_id);
book_find(head, book_id);
break;
case 5:
book_print(head);
break;
case 0:
printf("退出成功\n");
break;
default:
printf("非法输入\n");
break;
}
} while (choice != 0);
return 0;
}
struct book_info *book_init()
{
struct book_info *head, *p;
int book_num;
char book_id[20], book_name[20];
int size = sizeof(struct book_info);
head = NULL;
printf("请输入图书的id、名称、数量\n");
scanf("%s %s %d", book_id, book_name, &book_num);
p = (struct book_info *)malloc(size);
strcpy(p->book_id, book_id);
strcpy(p->book_name, book_name);
p->book_num = book_num;
head = book_insert(head, p);
return head;
}
struct book_info *book_insert(struct book_info *head, struct book_info *bookd)
{
struct book_info *ptr, *ptr1, *ptr2;
ptr2 = head;
ptr = bookd;
if (head == NULL)
{
head = ptr;
head->next = NULL;
}
else
{
while ((strcmp(ptr->book_id, ptr2->book_id) == 1) && (ptr2->next != NULL))
{
ptr1 = ptr2;
ptr2 = ptr2->next;
}
if (strcmp(ptr->book_id, ptr2->book_id))
{
if (head == ptr2)
head = ptr;
else
ptr1->next = ptr;
ptr->next = ptr2;
}
else
{
ptr2->next = ptr;
ptr->next = NULL;
}
}
return head;
}
struct book_info *book_delete(struct book_info *head, char book_id[20])
{
struct book_info *ptr1, *ptr2;
while (head != NULL && strcmp(head->book_id, book_id) == 0)
{
ptr2 = head;
head = head->next;
free(ptr2);
}
if (head == NULL)
return NULL;
ptr1 = head;
ptr2 = head->next;
while (ptr2 != NULL)
{
if (strcmp(ptr2->book_id, book_id) == 0)
{
ptr1->next = ptr2->next;
free(ptr2);
}
else
{
ptr1 = ptr2;
ptr2 = ptr1->next;
}
}
return head;
}
struct book_info *book_find(struct book_info *head, char book_id[20])
{
struct book_info *p = head;
while (p)
{
if (strcmp(p->book_id, book_id) == 0)
{
printf("图书的名称是: %s, 数量是: %d\n", p->book_name, p->book_num);
break;
}
else
{
p = p->next;
}
}
if (p == NULL)
{
printf("该图书不存在\n");
}
}
void book_print(struct book_info *head)
{
struct book_info *ptr;
if (head == NULL)
{
printf("空\n");
return;
}
for (ptr = head; ptr != NULL; ptr = ptr->next)
{
printf("图书id是: %s, 名称是: %s, 数量是: %d\n", ptr->book_id ,ptr->book_name, ptr->book_num);
}
}
查找图书:
输出图书信息:
运行结果如下: