C语言——图书管理系统(不带文件操作) 2021-05-17

说明:这是本人2021年寒假在家写的一个C语言大作业,供大家参考,写的不好的地方大家指正,有需要的朋友可以拿走,题目要求如下:
在这里插入图片描述

代码如下:

#include<stdio.h> 
#include<stdlib.h>
#include<time.h>

struct Number
{
	char alph;
	int num;
};

struct Book
{
	char title[128];
	char public[128];
	char author[128];
	int price;
    struct Number number;
	struct Book *next;
};

void menu() 
{
	printf("=====菜单=====\n");
	printf("1.插入书包\n");
	printf("2.删除书本\n");
	printf("3.修改书本\n");
	printf("4.打印书籍信息\n");
	printf("5.退出\n");
	printf("请输入你要执行的操作:");
}

void inseart(struct Book **head)
{
	srand(time(0));
	struct Book *previous;
	struct Book *current;
	struct Book *new;
	new=(struct Book*)malloc(sizeof(struct Book));
	int num=rand()%900000+100000;
	int alph=rand()%26;
	alph+='A';
	new->number.num=num;
	new->number.alph=alph;
	printf("请输入书名:");
	scanf("%s",&new->title);
	printf("请输入出版社:");
	scanf("%s",&new->public);
	printf("请输入作者:");
	scanf("%s",&new->author);
	printf("请输入价格:");
	scanf("%d",&new->price);
	current=*head;
	previous=NULL;
	while((current!=NULL)&&(current->number.num>num))
	{
		previous=current;
		current=current->next;
	}
	new->next=current;
	if(previous==NULL)
	{
		*head=new;
	}
	else
	{
		previous->next=new;
	}
}

void delete(struct Book **head)
{
	int num;
	printf("请输入你要删除的书的编号(数字部分):");
	scanf("%d",&num);
	struct Book *previous;
	struct Book *current;
	current=*head;
	previous=NULL;
	while(current!=NULL&&current->number.num!=num)
	{
		previous=current;
		current=current->next;
	}
	if(current==NULL)
	{
		printf("找不到匹配的结点\n");
		return;
	}
	else
	{
		if(previous==NULL)
		{
			*head=current->next;
		}
		else
		{
			previous->next=current->next;
		}
		free(current);
		printf("删除成功!\n");
	}
}

void print(struct Book *head)
{
	struct Book *book;
	int cnt=1;
	book=head;
	while(book!=NULL)
	{
		printf("Book%d\n",cnt);
		printf("书名:%s\n",book->title);
		printf("出版社:%s\n",book->public);
		printf("作者:%s\n",book->author);
		printf("价格:%d\n",book->price);
		printf("编号:%c%d\n",book->number.alph,book->number.num);
		book=book->next;
	}
	printf("========\n");
}

void modify(struct Book *head)
{
	int num;
	struct Book *book;
	book=head;
	printf("请输入你要修改的书籍的编号(数字部分):");
	scanf("%d",&num);
	while(book->number.num!=num)
	{
		book=book->next;
	}
	if(book==NULL)
	{
		printf("很抱歉没有找到书籍信息,你输入的编号有误\n");
	}
	else
	{
		printf("已找到你要修改的书籍.....\n");
    	printf("书名:%s\n",book->title);
    	printf("出版社:%s\n",book->public);
     	printf("作者:%s\n",book->author);
	    printf("价格:%d\n",book->price);
    	printf("编号:%c%d\n",book->number.alph,book->number.num);
		modifybook();
		int get;
		scanf("%d",&get);
		if(get==1)
		{
			printf("请输入修改后的书名:");
			scanf("%s",&book->title);
		}
		else if(get==2)
		{
			printf("请输入修改后的出版社:");
			scanf("%s",&book->public);
		}
		else if(get==3)
		{
			printf("请输入修改后的作者:");
			scanf("%s",&book->author);
		}
		else if(get==4)
		{
			printf("请输入修改后的价格:");
			scanf("%d",&book->price);
		}
		printf("修改成功!\n");
	} 
}

void modifybook()
{
	printf("=====请选择要修改的信息=====\n");
	printf("1.书名\n");
	printf("2.出版社\n");
	printf("3.作者\n");
	printf("4.价格\n");
	printf("请输入:"); 
}

void release(struct Book *head)
{
	struct Book *book;
	book=head;
	while(book!=NULL)
	{
		free(book);
		book=book->next;
	}
}

int main(void)
{
	struct Book *head=NULL;
	while(1)
	{
		int ch;
		menu();
		scanf("%d",&ch);
		if(ch==1)
		{
			inseart(&head);
		}
		else if(ch==2)
		{
			delete(&head);
		}
		else if(ch==3)
		{
			modify(head);
		}
		else if(ch==4)
		{
			print(head);
		}
		else if(ch==5)
		{
			break;
		}
		else
		{
			printf("你输入的操作有误\n");
		}
	}
	release(head);
	return 0;
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超霸霸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值