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

📄 简易图书管理系统.cpp

📁 初学者…… 很简单的东西…… ……
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    long num2;
    userInfo people;

    f4.seekp(0,ios::end);
    long posEnd2=f4.tellp();//记录文件末尾位置

    f.seekp(0,ios::end);
    long posEnd=f.tellp();//记录文件末尾位置

 //cout<<"借还书登记:\n";
    while(1)
	{   
 P:     f.seekp(0,ios::beg);//从文件头开始检索
	    cout<<"书名:";
		cin>>Book;
		do          //按书名查找
		{
			f.read((char*)&book,sizeof(bookData));
		}while(strcmp(book.bookName,Book)!=0&&f.tellp()!=posEnd);

		if(strcmp(book.bookName,Book)==0)//找到
		{   
			cout<<"图书编号:"<<book.TP<<endl;
            cout<<"图书名称:"<<book.bookName<<endl;
            cout<<"作者编号:"<<book.writerNumber1<<endl;
            cout<<"作者姓名:"<<book.writerName1<<endl;
            cout<<"        :"<<book.writerNumber2<<endl;
            cout<<"        :"<<book.writerName2<<endl;
            cout<<"        :"<<book.writerNumber3<<endl;
            cout<<"        :"<<book.writerName3<<endl;
            cout<<"出版社编号:"<<book.publishNumber<<endl;
			cout<<"出版社名称:"<<book.publish<<endl;
            cout<<"库存量:"<<book.balance<<endl;
            cout<<"剩余量:"<<book.balanceleave<<endl;
			cout<<endl;
			   
			cout<<"请键入操作选择:\n";
 L:         cout<<"1.借书  2.还书  0.退出;\n";

            int choice;
			cin>>choice;
			switch(choice)
			 {
			     case 1:
					 {
						 cout<<"书本数量:";
				         cin>>num;
				         if(num>0&&book.balanceleave>=num)//修改剩余量
						 { 
							 book.balanceleave-=num;
					     
					         f.seekp(-long(sizeof(bookData)),ios::cur);//文件指针复位
				             f.write((char*)&book,sizeof(bookData));//修改文件记录
						 
						     cout<<"图书编号:"<<book.TP<<endl;
                             cout<<"图书名称:"<<book.bookName<<endl;
                             cout<<"作者编号:"<<book.writerNumber1<<endl;
                             cout<<"作者姓名:"<<book.writerName1<<endl;
                             cout<<"        :"<<book.writerNumber2<<endl;
                             cout<<"        :"<<book.writerName2<<endl;
                             cout<<"        :"<<book.writerNumber3<<endl;
                             cout<<"        :"<<book.writerName3<<endl;
                             cout<<"出版社编号:"<<book.publishNumber<<endl;
			                 cout<<"出版社名称:"<<book.publish<<endl;
                             cout<<"库存量:"<<book.balance<<endl;
                             cout<<"剩余量:"<<book.balanceleave<<endl;
						     cout<<endl;
						 
						     strcpy(people.bbookName,book.bookName);
					 	     people.balance=num; 
						     cout<<"输入用户编号";
						     cin>>people.userNumber;

                             time_t lt;
                       	     lt = time(NULL);
	                         strcpy(people.bDate,ctime(&lt));
						
					         f4.seekp(0,ios::end);
					    	 f4.write((char*)&people,sizeof(userInfo));
						 
						     goto L;
						 
					   }

				       else
					   {  
						   cout<<"存量不足!\n";
				           goto L;
					   }
					 }
					   
				   
				    case 2:
					{
						 
                         cout<<"输入用户编号:";
						 cin>>num2;
						 cout<<"书本数量:";
						 cin>>num;
                         
  
				        f4.seekp(0,ios::beg);
					    do          //按书名查找
						{
							f4.read((char*)&people,sizeof(userInfo));

                            if(strcmp(people.bbookName,Book)==0
							   &&num>0
							   &&num<=people.balance
							   &&people.userNumber==num2
							   &&book.balance>=(num+book.balanceleave))//找到
							{
								flag=1;
						    	people.balance-=num; 
						     
						        book.balanceleave+=num;
					    
					            f.seekp(-long(sizeof(bookData)),ios::cur);//文件指针复位
				                f.write((char*)&book,sizeof(bookData));//修改文件记录
                        
					            cout<<"图书编号:"<<book.TP<<endl;
                                cout<<"图书名称:"<<book.bookName<<endl;
                                cout<<"作者编号:"<<book.writerNumber1<<endl;
                                cout<<"作者姓名:"<<book.writerName1<<endl;
                                cout<<"        :"<<book.writerNumber2<<endl;
                                cout<<"        :"<<book.writerName2<<endl;
                                cout<<"        :"<<book.writerNumber3<<endl;
                                cout<<"        :"<<book.writerName3<<endl;
                                cout<<"出版社编号:"<<book.publishNumber<<endl;
			                    cout<<"出版社名称:"<<book.publish<<endl;
                                cout<<"库存量:"<<book.balance<<endl;
                                cout<<"剩余量:"<<book.balanceleave<<endl;
						    	cout<<endl;
							 
							    f4.seekp(-long(sizeof(userInfo)),ios::cur);//文件指针复位
				                f4.write((char*)&people,sizeof(userInfo));//修改文件记录

							    if(people.balance==0)
								{
									userInfo *b;
                        	        int m,i;
	                        	    f4.close();
	                             	f4.open("userbookFile.dat",ios::binary|ios::in|ios::out);
	                        	    if(!f4)
									{
										cerr<<"error!";
	                        		    abort();
									}


	                        	     f4.seekg(0,ios::end);
                                     long posEnd2=f4.tellg();
	                                 m=posEnd2/sizeof(userInfo);
	                                 b=new userInfo[m];
	                                 f4.seekg(0,ios::beg);
	                                 i=0;
    
	                                 do 
									 {
										 f4.read((char *)&b[i],sizeof(userInfo));
		                                 i++;
									 }while (f4.tellp()!=posEnd2);

	                        	     f4.close();

	                        	     f4.open("userbookFile.dat",ios::binary|ios::trunc|ios::out);
	                        	     if(!f4)
									 {
										 cerr<<"error!";
	                        		     abort();
									 }

	                                 i=0;
									 do 
									 {
		                                 if(strcmp(b[i].bbookName,Book)==0&&b[i].userNumber==num2)
										 {
                                             goto  K;
										 }
				 
		                                 f4.write((char*)&b[i],sizeof(userInfo));
 K:                                      i++;

									 }while (i!=m);
 

	                              
	                                 cout<<"\t该记录已被删除!"<<endl;
         
	                        	     delete []b;

                                     f4.close();
	                        	     f4.open("userbookFile.dat",ios::binary|ios::in|ios::out);
	                        	     if(!f4)
									 {
	                        		     cerr<<"error!";
	                        		     abort();
									 }



								}
							
							    goto L;
							}

						 }while(f4.tellp()!=posEnd2);
			            
							 
						 if(flag==0)
						 {  cout<<"找不到借书信息!"<<endl;
						    goto L;
						 }


					}
				   

					case 0:manageBook(f,f4);
	                default:cout<<"输入错误,请再次输入:\n";

				   }

				   
			   }
			   else
				   cout<<"书名输入错误\n";
			       goto P;
	 }
	  

  }




//inquire.cpp,查询


void Inquire(fstream f,fstream f4)
{
	int choice;
    bookData book;
    char bName[40];
    char writerName[40];
    char bpublish[40];
    f.seekg(0,ios::end);
    long posEnd=f.tellp();
    long key;
    f4.seekg(0,ios::end);
    long posEnd2=f4.tellp();
    userInfo people;
    int flag=0;

    cout<<"查询"<<endl;
    while(1)
	{ 
		cout<<"请键入操作选择:"<<endl;
        cout<<"1:按图书名称查询"<<endl;
	    cout<<"2:按作者信息查询"<<endl;
    	cout<<"3:按出版社名称查询"<<endl;
    	cout<<"4:组合查询"<<endl;
	    cout<<"5:浏览图书信息"<<endl;
    	cout<<"6:按编号查询用户信息:"<<endl;
    	cout<<"7:浏览用户信息"<<endl;
    	cout<<"0:退出\n";
        cin>>choice;

	   switch(choice)
	   {   
	   case 1://按图书名称查询
		   {    
			   f.seekg(0,ios::beg);
	           cout<<"请输入要查找的图书名称:";
			   cin>>bName;

			   do
			   {
				   f.read((char*)&book,sizeof(bookData));
			   }while(strcmp(book.bookName,bName)!=0&&f.tellg()!=posEnd);

			   if(strcmp(book.bookName,bName)==0)
			   {    
				   cout<<"图书编号:"<<book.TP<<endl;
                   cout<<"图书名称:"<<book.bookName<<endl;
                   cout<<"作者编号:"<<book.writerNumber1<<endl;
                   cout<<"作者姓名:"<<book.writerName1<<endl;
                   cout<<"        :"<<book.writerNumber2<<endl;
                   cout<<"        :"<<book.writerName2<<endl;
                   cout<<"        :"<<book.writerNumber3<<endl;
                   cout<<"        :"<<book.writerName3<<endl;
                   cout<<"出版社编号:"<<book.publishNumber<<endl;
			       cout<<"出版社名称:"<<book.publish<<endl;
                   cout<<"库存量:"<<book.balance<<endl;
                   cout<<"剩余量:"<<book.balanceleave<<endl;
				   cout<<endl;
			  }

			  else
			  {
				  cout<<"查无此书!"<<endl;
			      cout<<endl;
			      continue;
			  }
			  break;
	}

    	case 2://按作者信息查询
			{
				f.seekg(0,ios::beg);
	            cout<<"请输入要查找的作者名称:";
			    cin>>writerName;
			    do
				{ 
					f.read((char*)&book,sizeof(bookData));
                    if(strcmp(book.writerName1,writerName)==0
				       ||strcmp(book.writerName2,writerName)==0
				       ||strcmp(book.writerName3,writerName)==0)

					{
						flag=1;
					    cout<<"图书编号:"<<book.TP<<endl;
                        cout<<"图书名称:"<<book.bookName<<endl;
                        cout<<"作者编号:"<<book.writerNumber1<<endl;
                        cout<<"作者姓名:"<<book.writerName1<<endl;
                        cout<<"        :"<<book.writerNumber2<<endl;
                        cout<<"        :"<<book.writerName2<<endl;
                        cout<<"        :"<<book.writerNumber3<<endl;
                        cout<<"        :"<<book.writerName3<<endl;
                        cout<<"出版社编号:"<<book.publishNumber<<endl;
			            cout<<"出版社名称:"<<book.publish<<endl;
                        cout<<"库存量:"<<book.balance<<endl;
                        cout<<"剩余量:"<<book.balanceleave<<endl;
					 cout<<endl;
					}

			  }while(f.tellg()!=posEnd);
			 
			  if(flag==0)
			  {
				  cout<<"查无此书!"<<endl;
                  cout<<endl;
			      continue;
			  }
			  break;
	}

		case 3://按出版社名称
			{
				f.seekg(0,ios::beg);
	            cout<<"请输入要查找的出版社名称:";
			    cin>>bpublish;
			    do
				{
					f.read((char*)&book,sizeof(bookData));

                    if(strcmp(book.publish,bpublish)==0)
					{
						flag=1;   
				        cout<<"图书编号:"<<book.TP<<endl;
                        cout<<"图书名称:"<<book.bookName<<endl;
                        cout<<"作者编号:"<<book.writerNumber1<<endl;
                        cout<<"作者姓名:"<<book.writerName1<<endl;
                        cout<<"        :"<<book.writerNumber2<<endl;
                        cout<<"        :"<<book.writerName2<<endl;
                        cout<<"        :"<<book.writerNumber3<<endl;
                        cout<<"        :"<<book.writerName3<<endl;
                        cout<<"出版社编号:"<<book.publishNumber<<endl;
			            cout<<"出版社名称:"<<book.publish<<endl;
                        cout<<"库存量:"<<book.balance<<endl;
                        cout<<"剩余量:"<<book.balanceleave<<endl;

⌨️ 快捷键说明

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