【无标题】

#include<stdio.h>
#include<string.h>
#include<errno.h>
#include <stdlib.h>
struct Account
{
	char Name[20];
	char id[10];
	char password[20];
	int cnt;
	char book_id[10][20];
	struct Account* nxt;
};//账号结构体
struct Book
{
	char Name[20];
	char id[10];
	int cnt_tot;//书籍总数
	int cnt_rent;//借出的书籍数
	int cnt_rest;//剩余书籍总数
	struct Book* nxt;
};//书籍结构体
int Account_jud=0;
int Account_jud_administrator=0;
int Change_Account_data=0;
struct Account Account_Now;
void init_table()
{
	for(int q=1;q<=15;++q)
	{
		if(q==1) for(int i=1;i<=30;++i) printf("*");
		else
		{
			printf("*");
			if(q==2)
			{
				for(int i=2;i<=5;++i) printf(" ");
				printf("图书馆信息管理系统");
				for(int i=17;i<=22;++i) printf(" ");
			}
			if(q==3)
			{
				for(int i=2;i<=4;++i) printf(" ");
				for(int i=6;i<=25;++i) printf("_");
				for(int i=24;i<=28;++i) printf(" ");
			}
			if(4<=q && q<=13)
			{
				for(int i=2;i<=4;++i) printf(" ");
				if(q<=12) printf("%d->",q-3);
				if(q==4) printf("查询某本图书状态      ");
				if(q==5) printf("注册图书馆信息系统账号");
				if(q==6) printf("登录图书馆信息系统账号");
				if(q==7) printf("以管理员身份登录      ");
				if(q==8) printf("退出当前登录的账号    ");
				if(q==9) printf("图书借阅              ");
				if(q==10) printf("图书归还              ");
				if(q==11) printf("查询个人借阅情况      ");
				if(q==12) printf("在图书馆中增/删书籍   ");
				if(q==13) printf("0->退出当前程序          ");
			}
			if(q==14) for(int i=2;i<=29;++i) printf(" ");
			if(q==15) for(int i=2;i<=29;++i) printf("*");
			printf("*");
		}
		printf("\n");
	}
	printf("请选择功能:");
}
void Order_Insert_linklist_Account(struct Account** head,struct Account A)
{
	struct Account* New=(struct Account*)malloc(sizeof(struct Account));
	strcpy(New->Name,A.Name);
	strcpy(New->id,A.id);
	strcpy(New->password,A.password);
	New->cnt=A.cnt;
	for(int i=0;i<New->cnt;++i)
		strcpy(New->book_id[i],A.book_id[i]);
	//建立新的节点
	struct Account* cur=*head;
	struct Account* pre=NULL;
	while(cur!=NULL && strcmp(cur->id,New->id)<0)
	{
		pre=cur;
		cur=cur->nxt;
	}
	New->nxt=cur;
	if(pre!=NULL) pre->nxt=New;
	if(cur==*head) *head=New;
}
struct Account* Build_linklist_Account()
{
	FILE* pf= fopen("Account.txt", "r");
	struct Account* head=NULL;
	struct Account A;
	while(fscanf(pf,"%s %s %s %d",A.Name,A.id,A.password,&A.cnt)==4)
	{
//		printf("\nopen\n");
//		printf("right_password=%s\n",A.password);	
		Order_Insert_linklist_Account(&head,A);
	}//将后台的全部账号信息取出,建立一个新的链表
	fclose(pf);
	return head;
}
void Order_Insert_linklist_Book(struct Book** head,struct Book A)
{
	struct Book* New=(struct Book*)malloc(sizeof(struct Book));
	strcpy(New->Name,A.Name);
	strcpy(New->id,A.id);
	New->cnt_tot=A.cnt_tot;
	New->cnt_rent=A.cnt_rent;
	New->cnt_rest=A.cnt_rest;
	
	struct Book* cur=*head;
	struct Book* pre=NULL;
	while(cur!=NULL && strcmp(cur->id,New->id)<0)
	{
		pre=cur;
		cur=cur->nxt;	
	}
	New->nxt=cur;
	if(pre!=NULL) pre->nxt=New;
	if(cur==*head) *head=New;
}
struct Book* Build_linklist_Book()
{
	FILE* pf=fopen("Book.txt","r");
	struct Book* head=NULL;
	int tot=0;
	while(feof(pf))
	{
		struct Book A;
		fscanf(pf,"%s %s %d %d %d",A.Name,A.id,A.cnt_tot,A.cnt_rent,A.cnt_rest);
		Order_Insert_linklist_Book(&head,A);
	}
	fclose(pf);
	return head;
}
int judge_name(char Name[])
{
	int len=strlen(Name);
	for(int i=0;i<len;++i)
	{
		if(!('a'<=Name[i] && Name[i]<='z'))
		{
			return 0;
		}
	}
	return 1;
}//判断姓名格式是否合法
int judge_id(char id[])
{
	int len=strlen(id);
	if(len!=8) return 0;
	for(int i=0;i<len;++i)
	{
		if(!('0'<=id[i] && id[i]<='9'))
		{
			return 0;
		}
	}
	return 1;
}
int judge_password(char password[])
{
	int len=strlen(password);
	if(len<6 || len>20) return 0;
	for(int i=0;i<len;++i)
	{
		int jud=0;
		if('0'<=password[i] && password[i]<='9') jud=1;
		if('A'<=password[i] && password[i]<='Z') jud=1;
		if('a'<=password[i] && password[i]<='z') jud=1;
		if(!jud) return 0;
	}
	return 1;
}
int judge_cnt(char cnt[])
{
	int len=strlen(cnt);
	if(len>=6) return -1;
	for(int i=0;i<len;++i)
	{
		if(!('0'<=cnt[i] && cnt[i]<='9')) return 0;
	}
	int tot=0;
	for(int i=0;i<len;++i)
	{
		tot=tot*10+cnt[i]-'0';
	}
	return tot;
}
int judge_operation_id(char now[])
{
	int len=strlen(now);
	if(len!=1) return -1;
	if('0'<=now[0] && now[0]<='8') return now[0]-'0';
	else return -1;
}
void Update_data_Book(struct Book* head)
{
	FILE* pf=fopen("Book.txt","w");
	struct Book* current=head;
	while(current!=NULL)
	{
		fprintf(pf,"%s %s %d %d %d\n",current->Name,current->id,current->cnt_tot,current->cnt_rent,current->cnt_rest);
		current=current->nxt;
	}
	fclose(pf);
	pf=NULL;
}
void Update_data_Account(struct Account* head)
{
	struct Account* current=head;
	FILE* pf= fopen("Account.txt", "w");
	while(current!=NULL)
	{
		if(Change_Account_data)
		{
			if(strcpy(current->id,Account_Now.id)==0)
			{
				int cnt=Account_Now.cnt;
				current->cnt=cnt;
				for(int i=0;i<cnt;++i)
					strcpy(current->book_id[i],Account_Now.book_id[i]);
			}
		}
//		printf("?=%s %s %s %d\n",current->Name,current->id,current->password,current->cnt); 
		fprintf(pf,"%s %s %s %d\n",current->Name,current->id,current->password,current->cnt);
		for(int i=0;i<current->cnt;++i)
			fprintf(pf,"%s ",current->book_id[i]);
		if(current->cnt) fprintf(pf,"\n");
		current=current->nxt;
	}
	fclose(pf);
	pf=NULL;
}
void Build_New_Account(struct Account New)
{
	struct Account* head=Build_linklist_Account();
	Order_Insert_linklist_Account(&head,New);//将新节点插入链表
	Update_data_Account(head);
	//完成文件信息的更新
}//将新建立的账号存进后台文件中
void main_Register_New_Account()
{
	char Name[20],id[10],password1[25],password2[25];
	printf("注册新账号需要给出姓名、学号(或教职工编号)、并设置密码\n");
	printf("请输入姓名(要求为姓名拼音,只含小写字母,不含其他字符):\n");
	int flag_name=0,flag_id=0,flag_password1=0,flag_password2=0;
	while(1)
	{
		scanf("%s",Name);
		flag_name=judge_name(Name);
		if(flag_name)
		{
			printf("姓名格式符合要求,输入成功!\n");
			break;
		}
		else
		{
			printf("给出的姓名格式不符合要求,请重新输入姓名!:\n");
		}
	}//反复判断,直到用户输入格式正确的姓名
	printf("请输入学号/教职工编号(要求为八位数字,不含其他任何字符):\n");
	while(1)
	{
		scanf("%s",id);
		flag_id=judge_id(id);
		if(flag_id)
		{
			printf("学号/教职工编号符合要求,输入成功!\n");
			break;
		}
		else
		{
			if(strlen(id)!=8) printf("给出的学号长度错误,请重新输入学号!:\n");
			else printf("给出的学号含有除数字意外的非法字符,请重新输入!:\n");
		}
	}//反复判断,直到用户输入格式正确的学号
	printf("请设置密码(要求仅包含数字、大小写字母,长度在6-20个字符之间:\n");
	while(1)
	{
		scanf("%s",password1);
		flag_password1=judge_password(password1);
		int len=strlen(password1);
		if(len<6) printf("密码不足6位,请重新设置密码!:\n");
		else if(len>20) printf("密码超过20位,请重新设置密码!:\n");
		else if(!flag_password1) printf("密码含有非法字符,请重新设置密码!:\n");
		else
		{
			printf("密码设置成功!");
			break; //
		}
	}//输入密码
	printf("请再次输入密码,以确认密码的设置:\n");
	while(1)
	{
		scanf("%s",password2);
		if(strcmp(password1,password2)==0)
		{
			printf("密码确认成功!\n");
			break;//
		}
		else printf("两次输入的密码不一致,请重新确认密码:\n");
	}//再次确认密码
	struct Account New;
	strcpy(New.Name,Name);
	strcpy(New.id,id);
	strcpy(New.password,password1);
	New.cnt=0;
	Build_New_Account(New);
	printf("账号注册成功,退出当前界面后即可登录\n");
}
struct Account* Find_id_linklist_Account(struct Account* head,char id[])
{
	struct Account* cur=head;
	while(cur!=NULL && strcmp(cur->id,id)<=0)
	{
//		printf("%s\n",
		if(strcmp(cur->id,id)==0) return cur;
		cur=cur->nxt;
	}
	return NULL;
}
struct Account* Login_Account_common()
{
	char id[20],password[20];
	printf("请输入学号/教职工编号:\n");
	while(1)
	{
		scanf("%s",id);
		if(!judge_id(id)) printf("学号格式错误,请重新输入学号:\n");
		else break;
	}
	printf("请输入密码:\n"); 
	while(1)
	{
		scanf("%s",password);
		printf("\npassword=%s",password);
		if(!judge_password(password)) printf("密码格式错误,请重新输入密码:\n");
		else break;
	}
	struct Account* head=Build_linklist_Account();
	if(head==NULL) printf("NULL\n");
	struct Account* Get_place=Find_id_linklist_Account(head,id);
	if(Get_place==NULL)
	{
		printf("???\n");
		return NULL;//该账号不存在
	}
	else if(strcmp(id,Get_place->id)==0) return Get_place;//密码正确,登录成功
	else return NULL;
}
int Login_Account_administrator()
{
	char right_password[40],password_now[40];
	scanf("%s",right_password);
	FILE* pf=fopen("administrator_password.txt","r");
	fscanf(pf,"%s",right_password);
	fclose(pf);
	int flag_password=0;
	scanf("%s",password_now);
	if(strcmp(password_now,right_password)==0) return 1;
	else return 0;
}//以密码形式实现
struct Book* Find_id_linklist_Book(struct Book* head,char id[])
{
	struct Book* cur=head;
	while(cur!=NULL && strcmp(cur->id,id)<=0)
	{
		if(strcmp(cur->id,id)==0) return cur;
		cur=cur->nxt;
	}
	return NULL;
}
struct Book* Find_Name_linklist_Book(struct Book* head,char Name[])
{
	struct Book* cur=head;
	while(cur!=NULL)
	{
		if(strcmp(cur->Name,Name)==0) return cur;
	}
	return NULL;
}
void Add_New_book()
{
	char Name[20],id[10],cnt_c[10];
	int cnt=0;
	int flag_name=0,flag_id=0,flag_cnt=0;
	printf("请输入新加入书籍的名称、编号、数量:\n");
	printf("请先输入新加入书籍的名称(要求为书名拼音,只含大小写字母:\n");
	while(1)
	{
		scanf("%s",Name);
		flag_name=judge_name(Name);
		if(flag_name)
		{
			printf("书名格式符合要求,输入成功!\n");
			break;
		}
		else
		{
			printf("书名格式不符合要求,请重新输入书名:\n");
		}
	}
	printf("请输入书籍编号(要求为8位数字,不含其他字符):\n");
	while(1)
	{
		scanf("%s",id);
		flag_id=judge_id(id);
		if(flag_id)
		{
			printf("编号符合要求,输入成功:\n");
			break;
		}
		else
		{
			printf("编号不符合要求,请重新输入书籍编号:\n");
		}
	}
	printf("请输入本次入库的该本书籍数量(一次入库不得超过99999本):\n");
	while(1)
	{
		scanf("%s",cnt_c);
		int flag_cnt=judge_cnt(cnt_c);
		if(flag_cnt==0) printf("请输入合法的书籍数量:\n");
		else if(flag_cnt==-1) printf("单次入库数量过多,请分多次入库:\n");
		else
		{
			cnt=flag_cnt;
			printf("书籍入库数量输入成功!\n");
			break;
		}
	}

	struct Book* head=Build_linklist_Book();
	struct Book* Get_place=Find_id_linklist_Book(head,id);
	if(Get_place==NULL)
	{
		struct Book A;
		strcpy(A.Name,Name);
		strcpy(A.id,id);
		A.cnt_tot=flag_cnt;
		A.cnt_rest=flag_cnt;
		A.cnt_rent=0;//新建节点
		Order_Insert_linklist_Book(&head,A);
//		Build_New_Book(A);
	}
	else
	{
		Get_place->cnt_tot+=flag_cnt;
		Get_place->cnt_rest+=flag_cnt;
	}
	Update_data_Book(head);
	printf("书籍入库成功!\n");
}
void Return_book()
{
	char id[10];
	int cnt=0;
	int flag_id=0;
	printf("请输入归还的书籍的编号\n");
	printf("请输入书籍编号(要求为8位数字,不含其他字符):\n");
	while(1)
	{
		scanf("%s",id);
		flag_id=judge_id(id);
		if(flag_id)
		{
			printf("编号符合要求,输入成功:\n");
			break;
		}
		else
		{
			printf("编号不符合要求,请重新输入书籍编号:\n");
		}
	}
	struct Book* head=Build_linklist_Book();
	struct Book* Get_place=Find_id_linklist_Book(head,id);
	Get_place->cnt_tot+=1;
	Get_place->cnt_rest+=1;
	Update_data_Book(head);
	char trans_book_id[10][40];
	int len=0;
	for(int i=0;i<Account_Now.cnt;++i)
	{
		if(strcpy(Account_Now.book_id[i],id)==0) continue;
		else 
		{
			strcpy(trans_book_id[len],Account_Now.book_id[i]);
			++len;	
		}
	}	
	for(int i=0;i<len;++i)
	{
		strcpy(Account_Now.book_id[i],trans_book_id[i]);
	}
	Account_Now.cnt--;
	struct Account* Head=Build_linklist_Account();
	Update_data_Account(Head); 
	printf("书籍归还成功!\n");
}
void main_search_book()
{
	printf("\n\n书籍查询界面\n");
	int flag=1;
	while(flag)
	{
		printf("输入1根据书籍编号查询书籍状态\n");
		printf("输入2根据书籍名称查询书籍状态\n");
		printf("输入0返回上一级界面\n");
		printf("请输入操作编号:\n");
		scanf("%d",&flag);
		if(flag==1)
		{
			printf("请输入书籍编号:\n"); 
			struct Book* head=Build_linklist_Book();
			char id[10];
			scanf("%s",id);
			struct Book* Get_place=Find_id_linklist_Book(head,id);
			if(Get_place==NULL) printf("本书不存在\n");
			else
			{
				printf("本书在图书馆书库中\n");
				printf("编号=%s 书名=%s\n",Get_place->id,Get_place->Name);
				printf("共有=%d本 其中借出=%d本 剩余=%d本\n",Get_place->cnt_tot,Get_place->cnt_rent,Get_place->cnt_rest);
			}
		}
		if(flag==2)
		{
			struct Book* head=Build_linklist_Book();
			char Name[40];
			scanf("%s",Name);
            struct Book* Get_place=Find_Name_linklist_Book(head,Name);
			if(Get_place==NULL) printf("本书不存在\n");
			else
			{
				printf("本书在图书馆书库中\n");
				printf("编号=%s 书名=%s\n",Get_place->id,Get_place->Name);
				printf("共有=%d本 其中借出=%d本 剩余=%d本\n",Get_place->cnt_tot,Get_place->cnt_rent,Get_place->cnt_rest);
			}
		}//根据书名查找书籍
		if(flag==0) break;
	}
}
void main_Login_Account()
{
	while(1)
	{
		int flag=0;
		struct Account* Get_place=Login_Account_common();
		if(Get_place==NULL) printf("该账号不存在或账号密码错误\n");
		else
		{
			printf("账号登录成功\n,即将返回上一界面");
			strcpy(Account_Now.Name,Get_place->Name);
			strcpy(Account_Now.id,Get_place->id);
			Account_Now.cnt=Get_place->cnt;
			for(int i=0;i<Account_Now.cnt;++i)
				strcpy(Account_Now.book_id[i],Get_place->book_id[i]);
			Account_jud=1;
			break;
		}
		printf("输入1重新尝试账号登录\n");
		printf("输入0退出当前页面,返回上一级菜单:\n");
		scanf("%d",&flag);
		if(flag==0) break;
	}
}
void main_Return_book()
{
	int flag=0;
	while(1)
	{
		printf("输入1归还书籍\n");
		printf("输入0结束归还,返回上一级界面\n");
		printf("请输入操作码:\n");
		scanf("%d",&flag);
		if(flag==0) break;
		Return_book();
	}
}
void main_Login_Account_administrator()
{
	while(1)
	{
		Account_jud_administrator=Login_Account_administrator();
		int flag=0;
		if(Account_jud_administrator) printf("以管理员身份登录失败\n");
		else
		{
			printf("以管理员身份登录成功,即将返回上一级界面\n");
			break;
		}
		printf("输入1继续尝试以管理员身份登录\n");
		printf("输入0放弃登录,返回上一级界面\n");
		printf("请输入操作码:\n");
		scanf("%d",&flag);
		if(flag==0) break;
	}
}
void main_Add_new_book()
{
	if(Account_jud_administrator==0)
	{
		printf("请先以管理员身份登录后再进行图书入库操作\n");
		printf("即将自动返回上一级界面\n");
	}
	else
	{
		int flag=0;
		while(1)
		{
			printf("输入1进行图书入库\n");
			printf("输入0结束入库,返回上一级界面\n");
			printf("请输入操作码:\n");
			scanf("%d",&flag);
			if(!flag) break;
			Add_New_book();
		}
	}
}
void main_Show_rent_situation()
{
	printf("共借阅%d本,编号如下\n",Account_Now.cnt);
	for(int i=0;i<Account_Now.cnt;++i)
	{
		printf("%s ",Account_Now.book_id[i]);
	}
	printf("查询完毕,即将自动返回上一级菜单\n");
}
void Rent_book_Name()
{
	printf("请先输入新加入书籍的名称(要求为书名拼音,只含大小写字母:\n");
	char Name[20];
	struct Book* head=Build_linklist_Book();
	while(1)
	{
		int flag=1;
		scanf("%s",Name);
		if(judge_name(Name)) printf("书名输入成功\n");
		else printf("书名不符合格式要求,请重新输入\n");
	}
	struct Book* Get_place=Find_Name_linklist_Book(head,Name);
	if(Get_place==NULL) printf("该书不存在\n");
	else
	{
		if(Get_place->cnt_rest==0) printf("该书已被全部借阅\n");
		else
		{
			printf("该书剩余%d本\n",Get_place->cnt_rest);
			printf("请确认是否要借阅该书\n");
			printf("输入1确认借阅\n");
			printf("输入0放弃借阅\n");
			printf("请输入操作码:\n");
			int jud=0;
			scanf("%d",&jud);
			if(jud==1)
			{
				Get_place->cnt_rest-=1;
				Get_place->cnt_rent+=1;
				Account_Now.cnt+=1;
				strcpy(Account_Now.book_id[Account_Now.cnt],Get_place->id);
				Update_data_Book(head);
				struct Account* Head=Build_linklist_Account();
				Update_data_Account(Head);
				printf("借阅成功!\n");
				printf("即将返回上一级界面\n");
			}
		}
	}
}
void main_Rent_book()
{
	if(Account_jud==0)
	{
		printf("请先登录后再书籍借阅\n");
		printf("即将自动返回上一级界面\n");
		return;
	}
	if(Account_Now.cnt==10)
	{
		printf("该用户仍有10本书未归还,达到借阅上限,无法继续借阅\n");
		printf("即将自动返回上一级界面\n");
		return;
	}
	while(1)
	{
		int flag=0;
		printf("输入1根据书籍名称借阅\n");
		printf("输入0返回上一届界面\n");
		printf("请输入操作码:\n");
		scanf("%d",&flag);
		if(flag==0) break;
		if(flag==1) Rent_book_Name();
	}
}
void use_functions(int type)
{
	if(type==1) main_search_book();
	if(type==2) main_Register_New_Account();
	if(type==3) main_Login_Account();
	if(type==4) main_Login_Account_administrator();
	if(type==5)
	{
		Account_jud=0;//退出账号
		Account_jud_administrator=0;
		printf("当前登录的普通账号和管理员账号均已退出\n");
		printf("即将返回初始页面\n\n"); 
	}
	if(type==6) main_Rent_book();//借书
	if(type==7) main_Return_book();//还书
	if(type==8)  main_Show_rent_situation();
	if(type==9) main_Add_new_book();
}
int main()
{
	while(1)
	{
		printf("欢迎使用图书馆信息管理系统\n");
		init_table();
		int type_op=0;
		while(1)
		{
			char now[10];
			scanf("%s",now);
			type_op=judge_operation_id(now);
			if(type_op==-1) printf("请输入合法的操作序号:\n");
			else break;
		}
		if(type_op==0) break;
		else use_functions(type_op);
	}
	return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值