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

📄 sources.cpp

📁 图书管理系统 用 C++实现 能完成图书的查询 入库出库 增加图书等
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include<windows.h>//获取系统日期
#include<fstream>
#include<iostream>
#include<string>
#include<sstream>//istringstream
#include<cstdlib>
#include<stdio.h>
using namespace std;

enum query_form {BookName,press,author,ISBN};

template <class T>bool from_string(T& t, const string& s, ios_base& (*f)(ios_base&))//字符串类型转换
{  
	istringstream iss(s);  
	return !(iss >> f >> t).fail();
}

/*函数声明*/
void menu();              //主 菜单
void sub_menu_Query();    //查询记录 菜单

void ADD();               //增加记录
void Modify();            //修改记录
void Delete();            //删除记录
void query(int choice_of_query,string query_keyword);             //查询记录 
string get_date()//获取当前系统日期并转换成yyyy-mm-dd格式
{
   string str;
   char s[10];
   SYSTEMTIME st;
   GetSystemTime(&st);
   sprintf(s,"%d",st.wYear); str= s;str+="-";
   sprintf(s,"%d",st.wMonth); str+=s; str+="-";
   sprintf(s,"%d",st.wDay);str+=s;
   return str;
}

/*类型定义*/
struct LNode
{
	int  sequenceNum;    //图书序号
	string ISBN;         //ISBN编号
	string BookName;     //书名 
	string press;        //出版社
	string author;       //作者
	string date_of_in;   //入库时间
	string date_of_out;  //出库时间
	int  store_number;   //库存数
	double price;        //单价

	LNode * next;  //指向下一个图书
};


/*链表基本操作*/

LNode * CreateDefaultLink( )//创建以默认数据建立的链表
{
	 ifstream book("Bookmanagement.txt");
	 if(!book)
	 {
		 cerr<<"打开文件失败!"<<endl;
		 char * s="Bookmanagement.txt";
	     cout<<"请重新输入完整文件路径及文件名(盘符:\\路径\\文件名.txt):"<<endl;
	 	 cin>>s;
	 	 book.open(s);
	 }
	 LNode *p1,*p2,*head=NULL;
	 p1=p2=new LNode;	 
	 p1->next =NULL;
	 int i=0,j=1; 
	 string read,r[11]; //read用来读取每一行关键字,r[1]-r[9]依次用来保存每个结点的关键字
     getline(book,read);
	 read.append(1,'\n');
	 while(j<10)
	  {   
		  string temp;
		  while( read[i]!= '\n'&& read[i++]!='\t')          
		  {
			  temp.append(1,read[i-1]);//依次分离一个单词,每个单词之间用 垂直制表符分割		  
		  }
		  r[j]=temp;
		  j++;
	 }      	  
	 from_string<int>(p1->sequenceNum, r[1], dec);
   	 p1->ISBN = r[2];
  	 p1->BookName = r[3];
     p1->author = r[4];
  	 p1->press = r[5];
	 from_string<int>(p1->store_number, r[6], dec);
	 from_string<double>(p1->price, r[7], dec);
     p1->date_of_in=r[8];
     p1->date_of_out=r[9];
 
	 while(!book.eof())
	 {
 
		 if(head==NULL)head=p1;
		 else p2->next=p1;
		 p2=p1;
		 p1=new LNode;
		 p1->next=NULL;
         getline(book,read);
	     read.append(1,'\n');
		 i=0,j=1;
    	 while(j<10)
		 {   
		      string temp;
		      while(read[i]!='\n' && read[i++]!='\t')          
			  {
			      temp.append(1,read[i-1]);//依次将一个单词
			  }
	    	  r[j]=temp;
	    	  j++;
		 }		    	  
 
	  	 from_string<int>(p1->sequenceNum, r[1], dec);//将r[1]转换成int型,序号
     	 p1->ISBN = r[2];
    	 p1->BookName = r[3];
         p1->author = r[4];
    	 p1->press = r[5];
		 from_string<int>(p1->store_number, r[6], dec);//将r[6]转换成int型,库存数
	     from_string<double>(p1->price, r[7], dec);//将r[7]转换成double型,单价
    	 p1->date_of_in=r[8];
    	 p1->date_of_out=r[9];

	 }
	 return head;
	 book.close();
}

void main()
{
	system("color f1"); //改变当前控制台窗口的背景颜色为F(白色),字体颜色为1(蓝色)
	menu();
	system("pause");

}

/*函数实现*/
void menu()
{
	cout<<"********************************************************************************"<<endl;

	cout<<"* "<<endl;
	cout<<"*	                    图书库存管理系统"<<endl;
	cout<<"*	 选择一个操作:                      *"<<endl;
	cout<<"*		<1> 图书入库                 *"<<endl;
	cout<<"*		<2> 修改一条图书记录         *"<<endl;
	cout<<"*		<3> 图书出库                 *"<<endl;
	cout<<"*		<4> 查询图书记录             *"<<endl;
	cout<<"*		<5> 清屏                     *"<<endl;
	cout<<"*		<6> 退出系统                 *"<<endl;
	cout<<"********************************************************************************"<<endl;
	cout<<endl;

	char choice='6';
	cout<<"		请输入您的选择(1-6):"<<endl;
	cin>>choice;
	while (int(choice)<49 || int(choice)>54)
	{
		cout<<"			输入不正确!请重新再输入一个1至6之间的整数:"<<endl;
		cin>>choice;
		cout<<"\n\n\n\n";
	}
	
	switch(choice)
	{
		case '1':cout<<"\n\n\n\n";ADD();break;
		case '2':cout<<"\n\n\n\n";Modify();break;
		case '3':cout<<"\n\n\n\n";Delete();break;
		case '4':cout<<"\n\n\n\n";sub_menu_Query();break;
		case '5':system("cls");menu();break;
		case '6':exit(0);
	}
	
}

void ADD()
{
	cout<<"请依次按照顺序输入图书的信息:  ISBN编号   书名   作者   出版社   单价"<<endl;
	LNode *new_book=new(LNode);
	char t[60];
  	cin>>new_book->ISBN;
    cin>>new_book->BookName;
    cin.get();  
    gets(t); new_book->author=t;	
	cin>>new_book->press;
	cin.get();
  	gets(t);  
	new_book->price=atof(t);
	new_book->date_of_in=get_date();
	new_book->sequenceNum=0;//初始化图书的序号,后面再修改。
	cout<<"您输入的图书的信息是:"<<endl;
	cout<<"ISBN编号		书名		作者		出版社		单价"<<endl;
 	cout<<new_book->ISBN<<"\t"<<new_book->BookName<<"\t"<<new_book->author<<"\t"<<new_book->press<<"\t"<<new_book->price<<"\t"<<new_book->date_of_in<<endl;
	//查找该书是否存在,如果是,打印信息,并将其库存数加1.如果没有,则插入到链表的最后。
	LNode *x= CreateDefaultLink();
	LNode *head=x;
	int exist_tag=0;   //图书存在识别标志
	while(x->next!=NULL && (int)x->store_number)
	{				  
	   if( new_book->ISBN.compare(x->ISBN)==0)//判断入库图书是否已经存在,使用ISBN判别
	   {
		    cout<<endl;
     	    cout<<"此书已经存在,库存数目加1"<<endl;
		    x->store_number++;
			exist_tag=1;   //图书存在识别标志.
			break;
	   }
	   x=x->next;	

	}
	if(exist_tag==0)
	{
		 cout<<"此书是新书,文件记录中将放到最后"<<endl;
		 new_book->sequenceNum=x->sequenceNum+1;
		 new_book->store_number=1;
		 x->next=new_book;
		 new_book->next=NULL;
		
	}
    //写入文件,因为是文本模式打开的,所以要全部重写文件。
	ofstream output_book("Bookmanagement.txt",ios::trunc);
	cout<<"是否将修改写入文件?(Y/N)"<<endl;
	char decision;
	cin>>decision;
	if(decision =='y' ||decision =='Y')
	{
		output_book<<"序号	ISBN		书名			作者		出版社		库存数		单价		入库时间	出库时间\n";	
		head=head->next;//跳过第一行
		while(head)
		{
		   	   
	        output_book<<head->sequenceNum<<"\t"<<head->ISBN<<"\t"<<head->BookName<<"\t"<<head->author<<"\t"<<head->press<<"\t"<<head->store_number<<"\t"<<head->price<<"\t"<<head->date_of_in<<"\t"<<head->date_of_out<<"\n";
	        head=head->next;
		}
		cout<<"写入完毕\n\n";
		output_book.close();
	    system("%windir%\\notepad.exe Bookmanagement.txt");
		system("pause");
	}	
	cout<<"是否继续?(Y/N):"<<endl;
	cin>>decision;
	if(decision =='y' ||decision =='Y')menu();
	else cout<<"谢谢使用!"<<endl;
	
}
void Delete()
{
	cout<<"请选择要出库的ISBN编号:"<<endl;		
	LNode *new_book=new(LNode);
  	cin>>new_book->ISBN;	
	LNode *x= CreateDefaultLink();
	LNode *head=x;       
	int exist_tag=0;   //图书存在识别标志
	while(x!=NULL && (int)x->store_number)
	{				  
	   if( new_book->ISBN.compare(x->ISBN)==0)//判断入库图书是否已经存在,使用ISBN判别
	   {
		    cout<<endl;
     	    cout<<"此书书库中存在"<<endl;
        	cout<<"ISBN编号		书名		作者		出版社		单价"<<endl;
			cout<<x->ISBN<<"\t"<<x->BookName<<"\t"<<x->author<<"\t"<<x->press<<"\t"<<x->price<<"\t"<<x->date_of_in<<endl;
		    x->store_number--;//库存数减少1
			exist_tag=1;   //图书存在识别标志.
			break;
	   }  
	   x=x->next;  	
	}

	if(exist_tag==0)
	{
		 cout<<"此书书库中不存在,不可以出库!"<<endl;		
	}
    //写入文件,因为是文本模式打开的,所以要全部重写文件。
	else 

⌨️ 快捷键说明

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