⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 41023.cpp

📁 本程序是数据结构的大作业之一
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <cstdlib>
#include <cstring>
#include <new>
#include <iostream>
#include <fstream>
#include <ctime>
#include <iomanip>
#include <windows.h>
#include <string.h>
//#include "schface.h"
#include "Btree.h" //b树的操作函数.
using namespace std;

typedef struct Book{//存放图书信息
	int num;  //书号
	char name[30];//书名
	char author[30];//作者
	int Now_store;//现存量
	int Total_store;//总库存
}Book;

typedef struct reader{ //存放读者信息
	long int ID;//借阅证号
	int num;//所借阅的书的序号
	struct tm Re_data;//应当还书的日期.借书后一个月要归还.
}Reader;

extern void setcolor(unsigned short color)
{ 
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); 
SetConsoleTextAttribute(hCon,color); 
} 
//颜色函数

//setcolor(10);
/*	函数声明  */

void AddBookInfo(int rec,Book &REC,FILE *fp);//改变图书的现存量和总存量.
void input_BI(Book &REC);//输入图书信息.
int LoadFile(FILE *fp,BTree &T);//读取书库的图书信息,按书号建立B树索引
void ReaderInfo(FILE *fp2);//显示读者的信息
void AddBook(FILE *fp,BTree &T);//图书入库.
void ReturnBook(FILE *fp,FILE *fp2,BTree &T);//归还图书。
void BorrowBook(FILE *fp,FILE *fp2,BTree &T);//借阅图书
void DelBook(FILE *fp,BTree &T);//清除库存.
void SearchBook1(FILE *fp,BTree T);//按书号精确查找某本书,并显示该书的具体信息
void SearchBook2(FILE *fp,BTree T);//按书名模糊查找某本书,并显示该书的具体信息
void ShowBook(FILE *fp);//输出书库中所有图书的信息
void BookInfo(Book REC);//输出图书的具体信息.
void main_switch(char i,FILE *fp,BTree &,FILE *);//操作选择函数
void Menu();//菜单函数
void Menu_name();

/*-----------------------------------------*/

int rec=0; //全局变量,用来表示图书信息在文件中的相对于文件头的位置

void main(void){
	
#ifdef DEBUG_BTREE
	display();	
#else	
	int j;
	char a[100];
	
	char re_choose[]={"\n选择无效,请重新输入:\n"};
    BTree T=NULL;
	FILE *fp,*fp2;

#ifdef DEBUG
	if((fp=fopen("lib2.dat","rb+"))==NULL) 
		{
			printf("cannot open the file of library data!");
			exit(0);
		}
	if((fp2=fopen("reader2.dat","rb+"))==NULL) 
		{
			printf("cannot open the file of reader data!");
			exit(0);
		}
#else
	if((fp=fopen("lib.dat","rb+"))==NULL) 
		{
			printf("cannot open the file of library data!");
			exit(0);
		}
	if((fp2=fopen("reader.dat","rb+"))==NULL) 
		{
			printf("cannot open the file of reader data!");
			exit(0);
		}
#endif

	rec=LoadFile(fp,T);//读取之前文件中存放的信息
	if(!rec) rec-=rec;
	
    system("cls");
	Menu_name();
	system("pause");
	system("cls");

    while(1)
	{
		
		Menu();
		for(j=0;j<100;j++)
		{
			scanf("%c",&a[j]);
			if(a[j]=='\n') break;
		}   /*功能选择*/
		
        if(a[1]!='\n'){
			cout <<"\n"<<re_choose<<endl;
			system("pause");
			system("cls");
			continue;
		}
        else{
			if(a[0]=='0') break;
			main_switch(a[0],fp,T,fp2);
			
			
		}
	}//end while
	fclose(fp);
	fclose(fp2);
#endif
}


/*---------------------------------------------*/


void main_switch(char i,FILE *fp,BTree &T,FILE *fp2)
  //操作选择函数
{
	setcolor(10);
	char re_choose[]={"\n选择无效,请重新输入...\n"};
    switch(i)
			{	
			case '1' ://增加图书到库存里
				system("cls");
				AddBook(fp,T);
				system("pause");
				system("cls");
				getchar();
				break;
			case '2' ://图书借阅
				system("cls");
				BorrowBook(fp,fp2,T);
				system("pause");		
				system("cls");
				getchar();
				break;	
			case '3'://查看读者的借阅情况
				system("cls");
				cout<<"\n                           以下是读者借阅情况:\n"<<endl;
				ReaderInfo(fp2);
				system("pause");
				system("cls");
				break;
			case '4'://从库存里清除没用的图书
				system("cls");
				DelBook(fp,T);
				getchar();
				system("pause");
				system("cls");
				break;
			case '5' ://归还图书
				system("cls");
				ReturnBook(fp,fp2,T);
				getchar();
				system("pause");
				system("cls");
				break;

			case '6'://查看库存里所有图书的信息
				system("cls");
				cout<<"\n                            以下是库存里所有图书的信息:\n"<<endl;
				ShowBook(fp);
				system("pause");
				system("cls");
				break;


			case '7'://查看某本书的资料
				system("cls");
				SearchBook2(fp,T);
				getchar();
				system("pause");
				system("cls");
				break;

			case '8'://查看某本书的资料
				system("cls");
				SearchBook1(fp,T);
				getchar();
				system("pause");
				system("cls");
				break;


			case '9'://查看B树的状态
				system("cls");
				showtree(T,1);
				cout<<"\n\n";
				system("pause");
				system("cls");
				break;


			default  :
				cout <<re_choose<<endl;
				system("pause");
				system("cls");
		
			}//end switch
}


void Menu()
     //菜单函数

{
		setcolor(14);
    	char Input[]={"\t请输入选项所对应的数字: "};
    	cout <<"\n\n\t"<<"   ★★★★★★★★★★★★  读书使人进步  ★★★★★★★★★★★★"<<endl;
	    cout<<"                                                                 "<<endl;       
        cout<<"           +=┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓=+"<<endl;
     	cout<<"           +=┃                                                      ┃=+"<<endl;
     	cout<<"           +=┃                                                      ┃=+"<<endl;
        cout<<"           +=┃      ╒═══════════════════╕      ┃=+"<<endl;
        cout<<"           +=┃       |___欢迎来到“我爱读书”图书管理系统___|       ┃=+"<<endl;
        cout<<"           +=┃       ║       |◇|▔▔▔★▔▔▔|◇|       ║       ┃=+"<<endl;
        cout<<"           +=┃       ║  ★ __|读|_╭────╮_|死|___ ★ ║       ┃=+"<<endl;
        cout<<"           +=┃       ║ __╱  |死|              |读|   ╲__║       ┃=+"<<endl;
        cout<<"           +=┃       ║|______|书|              |书|______|║       ┃=+"<<endl;
        cout<<"           +=┃       ║| ╭-╮|好|              |坏|╭-╮ |║       ┃=+"<<endl;
        cout<<"           +=┃       ║▓|   ||◇|__          __|◇||   |▓║       ┃=+"<<endl;
        cout<<"           +=┃       ║▓|   |▓▓※|       ▕※▓▓|   |▓║       ┃=+"<<endl;
        cout<<"           +=┃       ║▓|   |▓▓※|       ▕※▓▓|   |▓║       ┃=+"<<endl;
        cout<<"           +=┃       ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔       ┃=+"<<endl;
		cout<<"           +=┃     ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔     ┃=+"<<endl;
		cout<<"           +=┃   ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔   ┃=+"<<endl;
	    cout<<"           +=┃▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔┃=+"<<endl;
        cout<<"           +=┠───────────────────────────┨=+"<<endl;
        cout<<"           +=┃    1、采编入库     2、借阅       3.查看读者信息      ┃=+"<<endl;
        cout<<"           +=┃                                                      ┃=+"<<endl;
        cout<<"           +=┃    4、清除入库     5、归还       6.查看书本信息      ┃=+"<<endl;                  
        cout<<"           +=┃                                                      ┃=+"<<endl;
		cout<<"           +=┃    7、按书名查找   8、按书号查找 9.显示B树  0.退出   ┃=+"<<endl;
        cout<<"           +=┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛=+"<<endl; 
    
	    
	    cout<<"    \n\n                请选择要运行的程序(0--9):"<<endl;

//	cout <<"\t★★★★★★★★★★★★★★★★★★★★\n"<<endl;

}//Menu();


void Menu_name()
     //作者信息
{ 
	setcolor(14);
	printf("\n\n\n\n\n\n\n");
	cout<< "             *************************************************"<<endl;
	cout<<"                           '我爱读书'图书管理系统"<<endl;
	cout<<"                           制作:袁伟"<<endl;
	cout<<"                           班级:计算机(3)班"<<endl;
	cout<<"                           学号: 4102325" <<endl;
	cout<<"              **************************************************" <<endl;
	printf("\n\n\n\t\t");
}



/*---------------------------------------------*/

void AddBookInfo(int rec,Book &REC,FILE *fp)
    //改变图书的现存量和总存量.
{

	int j=REC.Now_store;//j存放增加的数目
	
	fseek(fp,rec,0);
	fread(&REC,sizeof(Book),1,fp);
	int k=REC.Total_store;
	REC.Now_store+=j;
	REC.Total_store+=j;
	fseek(fp,rec,0);
	fwrite(&REC,sizeof(Book),1,fp);//覆盖原来的图书信息

}



void input_BI(Book &REC)
  //输入图书信息.
{
	printf("\n采集入库图书的信息");
	printf("\n书号:");
	scanf("%d%*c",&REC.num);
	printf("\n书名:");
	gets(REC.name);
	printf("\n作者:");
	gets(REC.author);
	printf("\n入库图书的数量:");
	scanf("%d",&REC.Now_store);
}





int LoadFile(FILE *fp,BTree &T)
   //读取书库的图书信息,按书号建立B树索引
{
	int rec=0;
	int flag=-2;//文件尾端的判断标志,用来防止尾端数据
				//重读的情况的发生
	Book REC;
	Result R;
	REC.num=flag;
	while(!feof(fp))
	{
		fread(&REC,sizeof(Book),1,fp);
		if(flag==REC.num) break;
		if(REC.num==-1) {
			rec+=sizeof(Book);
			continue;
		}
		R=SearchBTree(T,REC.num,0);
		InsertBTree(T,REC.num,R.pt,R.i,rec);
        rec+=sizeof(Book);
		flag=REC.num;
	}
	return(rec);
}

/*
void ReaderInfo(FILE *fp2){
	//显示读者的信息
	Reader person3;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -