C语言课程设计之图书管理系统(涉及链表、文件)

本文介绍了使用C语言进行课程设计的图书管理系统,该系统涉及到链表和文件操作。通过代码展示了如何实现图书信息的存储和管理。
摘要由CSDN通过智能技术生成

效果图如下:

效果图

(修正时间:2021年7月6日  ,修复了评论区的所有问题)

C语言代码如下:

//全部都是原创的,自己写的,历时近五天。
//运用了链表进行数据处理,然后用了文件存储数据。每次处理后又重新存进文件。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//这个student就是读者,也就是借书证的信息
struct student
{
	char stu_num[10];
	char stu_name[20];
	char borrow_book[50];
	struct student *next;
}*stu1,*stu2;
//这是书籍信息
struct book
{
	char book_num[10];
	char book_name[60];
	char book_writer[20];
	int book_price;
	int here;
	char borrow_book_man[20];
	struct book *next;
}*book1,*book2;
//创建卡,也就是新的借书证,有了新读者
int creat_ka(struct student *head1,int n)
{
	FILE *fp;
	int i = 0;
	struct student *tail,*pnew; 
	tail=head1;
	system("cls");
	for(i=0;i<n;i++)
	{
		tail=tail->next;
	}
	pnew=(struct student *)malloc(sizeof(struct student));
	printf("输入新办的借书证号:");
	scanf("%s",&pnew->stu_num);
	printf("输入新的读者名:");
	scanf("%s",pnew->stu_name);
	strcpy(pnew->borrow_book,"无");
	pnew->next=NULL;
	tail->next=pnew;
	fp=fopen("ka.txt","a");
	fwrite(pnew,sizeof(struct student),1,fp);
	fclose(fp);
	system("cls");
	printf("***************************[操作成功]*****************************\n\n");
	return ++n;
}
//增加书
int add_book(struct book *head,int n)
{
	FILE *fp;
	struct book *tail,*pnew;
	int i = 0;
	tail=head;
	system("cls");
	for(i=0;i<n;i++)
	{
		tail=tail->next;
	}
	pnew=(struct book *)malloc(sizeof(struct book));
	printf("新书的书号:\n");
	scanf("%s",pnew->book_num);
	printf("新书的书名:\n");
	scanf("%s",pnew->book_name);
	printf("新书作者:\n");
	scanf("%s",pnew->book_writer);
	printf("单价:\n");
	scanf("%d",&pnew->book_price);
	printf("请你设定书的状态(1在架/0不在):\n");
	scanf("%d",&pnew->here);
	pnew->next=NULL;
	tail->next=pnew;
	fp=fopen("book.txt","a");
	fwrite(pnew,sizeof(struct book),1,fp);
	fclose(fp);
	printf("***************************[操作成功]*****************************\n\n");
	return ++n;
}
/****************************
 * 删除书
 * head:头节点  n:书籍数
 * 返回值为书籍数
 ****************************
 */
int del_book(struct book *head,int n)
{
	FILE *fp;
	struct book *tail,*pnew1,*pnew2;
	char booknum[10];
	int i,j,biaozhi_NoBook = 1;

	tail=head->next;
	pnew1=head;
	system("cls");

	printf("输入要删除的书的书号:\n");
	scanf("%s",booknum);
	
	//遍历链表,寻找是否存在该书籍
	for(i=0;tail!=NULL;i++)
	{
		//若匹配
		if(strcmp(tail->book_num,booknum)==0)
		{
			i++;
			biaozhi_NoBook=0;//存在则标志位清零
			printf("存在这个书号,第 %d 本\n", i);//第i本就是该书籍
			break;
		}
		
		//若不匹配,则指向下一个
		tail=tail->next;
	}

	//若遍历结束也不存在
	if(biaozhi_NoBook)
	{
		printf("不存在这个书号\n");
		return n;
	}
	
	//pnew1指向要删除的节点的前一个
	for(j=0 ; j < i - 1 ; j++)
	{
		pnew1=pnew1->next;
	}
	
	//删除操作
	pnew1->next=tail->next;
	free(tail);
	
	//重新写入文件(覆盖原有)
	fp=fopen("book.txt","w");


	//若删除一个后还有书籍
	if(head->next != NULL)
	{
		pnew2=head->next;

		do
		{
			fwrite(pnew2,sizeof(struct book),1,fp);
			if((pnew2->next) != NULL)
			{
				pnew2=pnew2->next;
			}
			else
			{
				break;
			}
		}while((pnew2->next) != NULL);

		fwrite(pnew2,sizeof(struct book),1,fp);//存最后一个
	}
	else
	{
		//若删除一个后没有书籍
		fwrite(NULL,0,1,fp);
	}
	fclose(fp);
	system("cls");
	printf("***************************[操作成功]*****************************\n\n");
	return --n;
}

/****************************
 * 注销借书证
 * head:头节点  n:卡数
 * 返回值为卡数
 ****************************
 */
int del_ka(struct student *head,int n)
{
	FILE *fp;
	struct student *tail,*pnew1,*pnew2;
	char ka_num[10];
	int i,j,biaozhi_NoKa = 1;

	tail=head->next;
	pnew1=head;
	system("cls");

	printf("要删除的卡号:\n");
	scanf("%s",ka_num);

	//遍历链表,寻找是否存在该卡
	for(i=0;tail!=0;i++)
	{
		if(strcmp(tail->stu_num,ka_num)==0)
		{
			i++;
			biaozhi_NoKa=0;
			break;
		}
		tail=tail->next;
	}

	//若遍历结束也不存在
	if(biaozhi_NoKa)
	{
		printf("不存在这个卡号\n");
		return n;
	}
	
	//pnew1指向要删除的节点的前一个
	for(j=0 ; j < i - 1; j++)
	{
		pnew1=pnew1->next;
	}

	//删除操作
	pnew1->next=tail->ne
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

崭蓝码农

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

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

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

打赏作者

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

抵扣说明:

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

余额充值