#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
struct book_information{
	char name[20];    //书名
	char chuban[20]; //出版
	char year[5];    //年号
    char ISBN[20];   
	char number[20];  //书号
	char zhubian[20];   //主编
	char shuliang[2];
};
typedef struct book_information book;
extern int numbook;   //图书数量
extern book*records;//图书信息
extern char savedTag;//信息保存标志
void handle_menu(void);
int menu_select(int);
void jiechu(void);
void guihuan(void);
int find(char*target,int targetType,int from);
void add(void);
void modify(void);
int saveRecords(int);
void showTable(void);
void quit(void);


#include"head.h"
int numbook=0;
book*records=NULL;
char savedTag=0;
void main()
{
	int i,A;
	printf("\t************************************************\n");
	printf("\t            欢迎使用广商图书管理系统            \n");
	printf("\t                                                \n");
	printf("\t                                                \n");
	printf("\t                                                \n");
	printf("\t                                                \n");
	printf("\t                                                \n");
	printf("\t                                                \n");
	printf("\t                                设 计 人:      \n");
	printf("\t                                班    级:      \n");
	printf("\t                                指导老师:      \n");
	printf("\t                                时间:2015.04.14\n");
	printf("\t************************************************\n");
	printf("请输入图书管理员用户密码:");
	scanf("%d",&A);
	for(i=1;i<=3;i++)
	if(A!=123)
    {
		printf("密码错误,请重新输入:");
        scanf("%d",&A);
    }
	handle_menu();
}
void handle_menu()
{
	for(;;){
		switch(menu_select())//这里为什么错了?
		{
		case 1:
			jiechu();
			break;
		case 2:
			guihuan();
			break;
		case 3:
			find();//这里为什么又错了?T_T
			break;
		case 4:
			add();
			break;
		case 5:
			modify();
			break;
		case 6:
			quit();
		}
	}
}
int menu_select(int)
{
	char s[2];
	int cn=0;
	printf("\n");
	printf("\t1.借出图书\n");
	printf("\t2.归还图书\n");
	printf("\t3.查找图书\n");
	printf("\t4.添加图书\n");
	printf("\t5.修改图书\n");
	printf("\t6.退出程序\n");
	printf("\n\t左边序号对应功能选择,请选择1-6:\n");
	for(;;)
	{
		gets(s);
		cn=atoi(s);
		if(cn<1||cn>9)printf("\n\t请重新选择,1-6:\n");
		else break;
	}
	return cn;
}
void quit()
{
	char str[5];
	if(savedTag==1)
	{
		printf("是否保存原来的记录?(Y/N)");
		gets(str);
		if(str[0]!='N')
			saveRecords();//这里为什么还错了。。。奔泪
	}
	free(records);
	exit(0);
}




#include"head.h"
void jiechu(void)
{
	char str[5];
	char target[20];
	int type;
	int i;
	int tmpi;
	float sum,mark;
	int count = 0;	

    if(numbook==0)
	{
		printf("没有可供借出的图书");
		return;
	}

	while(1) {
		printf("请输入如何找到欲借出的图书的方式:\n");
		printf("1. 按书名\n");
		printf("2. 按主编\n");
		printf("3. 按书号\n");
		gets(str);
		if (strlen(str) == 0) break;

		if (str[0] == '1')
		{
			printf("请输入该图书的书名:");
			gets(target);
			type = 0;
		} else if (str[0] == '2')
		{
			printf("请输入该图书的主编:");
			gets(target);
			type = 1;
		} else
		{
			printf("请输入该图书的书号:");
			gets(target);
			type = 2;
		}

		i = find(target,type,0);
		if (i==-1) printf("没有符合条件的图书!\n");

		while(i != -1)
		{
			printf("书名:%s\n主编:%s\n出版:%s\n年号:%s\nISBN:%s\n书号:%s\n数量:%s\n",
				  records[i].name,records[i].zhubian,records[i].chuban,
				  records[i].year,records[i].ISBN,records[i].number,records[i].shuliang);
			printf("确定要借入这个图书吗?(y/N)");
			gets(str);
			if (str[0]=='y' || str[0]=='Y')	printf("书名:%s\n主编:%s\n出版:%s\n年号:%s\nISBN:%s\n书号:%s\n数量:%s\n",
				                                   records[i].name,records[i].zhubian,records[i].chuban,
				                                   records[i].year,records[i].ISBN,records[i].number,records[i].shuliang-1);
		}
	}
}





#include"head.h"
void guihuan(void)
{
	char str[5];
	char target[20];
	int type;
	int i;
	int count = 0;
	while(1) {
		printf("请输入如何找到欲归还的图书的方式:\n");
		printf("1. 按书名\n");
		printf("2. 按主编\n");
		printf("3. 按书号\n");
		gets(str);
		if (strlen(str) == 0) break;

		if (str[0] == '1')
		{
			printf("请输入该图书的书名:");
			gets(target);
			type = 0;
		} else if (str[0] == '2')
		{
			printf("请输入该图书的主编:");
			gets(target);
			type = 1;
		} else
		{
			printf("请输入该图书的书号:");
			gets(target);
			type = 2;
		}

		i = find(target,type,0);
		if (i==-1) printf("没有符合条件的图书!\n");

		while(i != -1)
		{
			printf("书名:%s\n主编:%s\n出版:%s\n年号:%s\nISBN:%s\n书号:%s\n数量:%s\n",
				  records[i].name,records[i].zhubian,records[i].chuban,
				  records[i].year,records[i].ISBN,records[i].number,records[i].shuliang);
			printf("确定要归还这个图书吗?(y/N)");
			gets(str);
			if (str[0]=='y' || str[0]=='Y')	printf("书名:%s\n主编:%s\n出版:%s\n年号:%s\nISBN:%s\n书号:%s\n数量:%s\n",
				                                   records[i].name,records[i].zhubian,records[i].chuban,
				                                   records[i].year,records[i].ISBN,records[i].number,records[i].shuliang+1);
		}
	}
}




#include"head.h"
void add(void)
{
	if(numbook==0)
		printf("原来没有记录,现在记入新的图书信息\n");
	else
		printf("下面在已有图书信息末尾添加新的信息\n");
	while(1)
	{
		printf("请输入书名:");
	    gets(records[numbook].name);
	    printf("请输入主编:");
	    gets(records[numbook].zhubian);	
     	printf("请输入出版:");
    	gets(records[numbook].chuban);
	    printf("请输入年号:");
		gets(records[numbook].year);
    	printf("请输入ISBN:");
    	gets(records[numbook].ISBN);
    	printf("请输入书号:");
    	gets(records[numbook].number);
		printf("请输入数量:");
		gets(records[numbook].shuliang);
    	numbook++;
	}
	printf("新图书已录入成功!\n");
	printf("现在一共有%d本图书\n",numbook);
	savedTag=1;
}




#include"head.h"
void find(int )
{
	char str[5];
	char target[20];
	int type;
	int count;
	int i;
	if(numbook==0)
	{
		printf("没有可查询的记录\n");
		return;
	}
	while(1)
	{
		printf("请选择查询方式:\n");
		printf("1.按书名\n");
		printf("2.按主编\n");
		printf("3.按书号\n");
		gets(str);
		if(str[0]=='1')
		{
			printf("请输入欲查询的图书的书名:\n");
			gets(target);
			type=0;
		}
		if(str[0]=='2')
		{
			printf("请输入欲查询的图书的主编:\n");
			gets(target);
			type=1;
		}
		if(str[0]=='3')
		{
			printf("请输入欲查询的图书的书号:\n");
			gets(target);
			type=2;
		}
		i=find(target,type,0);
		count=0;
		while(i!=-1)
		{
			count++;
			printf("书名:%s\n主编:%s\n出版:%s\n年号:%s\nISBN:%s\n书号:%s\n数量:%s\n",
				  records[i].name,records[i].zhubian,records[i].chuban,
				  records[i].year,records[i].ISBN,records[i].number,records[i].shuliang);
			i=find(target,type,i+1);
		}
		if(count==0)printf("没有合适的图书\n");
		else printf("一共找到了%d本图书\n",count);
	}
}





#include"head.h"
void modify(void)
{
	char str[5];
	char target[20];
	int type;
	int i;
	int tmpi;
	float sum,mark;
	int count = 0;	

    if(numbook==0)
	{
		printf("没有可供修改的记录");
		return;
	}

	while(1) {
		printf("请输入如何找到欲修改的图书的方式:\n");
		printf("1. 按书名\n");
		printf("2. 按主编\n");
		printf("3. 按书号\n");
		gets(str);
		if (strlen(str) == 0) break;

		if (str[0] == '1')
		{
			printf("请输入该图书的书名:");
			gets(target);
			type = 0;
		} else if (str[0] == '2')
		{
			printf("请输入该图书的主编:");
			gets(target);
			type = 1;
		} else
		{
			printf("请输入该图书的书号:");
			gets(target);
			type = 2;
		}

		i = find(target,type,0);
		if (i==-1) printf("没有符合条件的图书!\n");

		while(i != -1)
		{
			printf("书名:%s\n主编:%s\n出版:%s\n年号:%s\nISBN:%s\n书号:%s\n数量:%s\n",
				  records[i].name,records[i].zhubian,records[i].chuban,
				  records[i].year,records[i].ISBN,records[i].number,records[i].shuliang);
			printf("确定要修改这个图书的信息吗?(y/N)");
			gets(str);
			if (str[0]=='y' || str[0]=='Y')
			{
				printf("下面请重新输入该图书的信息:\n");
				printf("请输入书名:");
				gets(records[i].name);
				printf("请输入主编:");
				gets(records[i].zhubian);
				printf("请输入出版:\n");
				gets(records[i].chuban);
				printf("请输入年号:\n");
				gets(records[i].year);
				printf("请输入ISBN\n");
				gets(records[i].ISBN);
				printf("请输入书号:\n");
				gets(records[i].number);
				printf("请输入数量:");
				gets(records[i].shuliang);
			}
			i=find(target,type,i+1);
		}
	}
	savedTag=1;
}