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

📄 实验六.cpp

📁 本程序是使用数据结构的算法
💻 CPP
字号:
#include "sort_all.h"
void main()
//主函数
{
	SLList L;
	LinkList H;
	int findnum;					//存找到的数据号
	KeysType key[MAX_NUM_OF_KEY];	//用于存储存要查的关键字
	InitSLList(L);			        //初始化表
    char c;
	bool n1=true,n2=true; 
	while(n1)                       //此循环保证了选择某一操作后还能回到主目录,而不是直接退出
	{
    cout<<"\n";
    cout<<"         ┏━━━━━━━━━━━━━━━━━━━━━━━┓\n";
	cout<<"         ┃         * * * 图书馆管理系统 * * *           ┃\n"; 
	cout<<"         ┃    ========================================= ┃\n";
	cout<<"         ┃      c. 建图书表        s. 查询图书          ┃\n";
	cout<<"         ┃      i.插入图书         n.更新图书           ┃\n";
	cout<<"         ┃      d.删除图书         p.总览图书           ┃\n";
	cout<<"         ┃      w.保存文件   │    r.读取文件           ┃\n";	
	cout<<"           ━━━━━━━━━━━━━━━━━━━━━━━                                                ┃\n";
	cout<<"         ┃    ----------------------------------------- ┃\n";
	cout<<"         ┃       * 请您在上述操作中选择相应的字母 *     ┃\n";
    cout<<"         ┗━━━━━━━━━━━━━━━━━━━━━━━┛\n";
	cin>>c;
	switch(c)
	{
	case 'c':                           //建表create
	case 'C':		
		GetData(L);						//获得数据
		cout<<endl<<"恭喜你,建表成功!具体如下:"<<endl;
	    RadixSort(L);					//基数排序
	    Arrange(L);						//重整
	    SLListTraverse(L);
		n1=true;
		break;

	case 's':                           //查询search
	case 'S':
		cout<<"您选择的操作——查询(search)"<<endl;
		if(L.recnum==0)                 //抛出异常
		{printf("\a");  
			cout<<endl<<"还没数据,请先插入数据!";
		}
		else{
		cout<<endl<<"1.按图书号查询; 2.按图书名称查询;3.按作者查询.";
        int i;
		cin>>i;
		switch(i)
		{
		case 1:
		cout<<"您选择的操作——1.按图书号查询"<<endl;
		GetSearchKey(key);				//得到需要查找的图书号
	    findnum=Search_Bin(L,key);
	    if(findnum) 
		{
		   cout<<"你要查询的图书是No."<<findnum<<endl;
		   cout<<endl<<"查询结果如下:"<<endl;
		   DataTraverse(L,findnum);	    //打印找到的数据
		}
	    else 
		{
		printf("\a");                       //声音提示
		printf("对不起,没有这条记录!\n");
		}
		break;

		case 2:
		cout<<"您选择的操作——2.按图书名称查询"<<endl;
		GetSearchEquiname(L,key);
	    findnum=Search_Bin(L,key);
	    cout<<"你要查询的图书是No."<<findnum<<endl;
		cout<<endl<<"查询结果如下:"<<endl;
		DataTraverse(L,findnum);	    //打印找到的数据
		break;

		case 3:
		cout<<"您选择的操作——3.按作者查询"<<endl;
		GetSearchAuthor(L,key);
	    findnum=Search_Bin(L,key);
	    cout<<"你要查询的图书是No."<<findnum<<endl;
		cout<<endl<<"查询结果如下:"<<endl;
		DataTraverse(L,findnum);	    //打印找到的数据
		break;

		default:
	    printf("\a");                       //声音提示
        cout<<"非法输入,请输入1或2!";
		break;
		}//switch
}//else
		
		n1=true;
		break;
	case 'i':                          //插入insert
	case 'I':
		if(L.recnum==0)
	{ 	printf("\a");                       //声音提示
		cout<<endl<<"还没记录,请先建表或读取数据!";
		}//if                               //抛出异常
	else
	{
		InsertElem(L);  
	}
		n1=true;
		break;

	case 'n':                            //更新new
	case 'N':
		if(L.recnum==0)                  //抛出异常
		{
		printf("\a");                       //声音提示
		cout<<endl<<"还没数据,请先插入数据!";
		}
		else{

		cout<<endl<<"1.按图书号更新; 2.按图书名称更新.";

		int i;
		cin>>i;
		switch(i)
		{
		case 1:
		cout<<"您选择的操作——1.按图书号更新"<<endl;
		GetSearchKey(key);				//得到需要查找的关键字
	    findnum=Search_Bin(L,key);
		while(!findnum)
		{
		printf("\a");                       //声音提示
		cout<<"对不起,没有这条记录!"<<endl;
		GetSearchKey(key);				//得到需要查找的关键字
	    findnum=Search_Bin(L,key);
		}
		cout<<"\n你要更新的图书是No."<<findnum<<endl;
		DataTraverse(L,findnum);	     //打印找到的数据
		break;

		case 2:
		cout<<"您选择的操作——2.按图书名称更新"<<endl;
		GetSearchEquiname(L,key);
		findnum=Search_Bin(L,key);
		while(!findnum)
		{
		printf("\a");                       //声音提示
		cout<<"对不起,没有这条记录!"<<endl;
		GetSearchEquiname(L,key);			//得到需要查找的关键字
	    findnum=Search_Bin(L,key);
		}
		cout<<endl<<"你要的图书是No."<<findnum;
	    cout<<endl<<"结果如下:"<<endl;
	    DataTraverse(L,findnum);	//打印找到的数据
		break;

		default:
		printf("\a");                       //声音提示
        cout<<"非法输入,请输入1或2!";
		break;
    while(n2)                           //此循环保证了选择某一操作后还能回到主目录,而不是直接退出
	{
		cout<<endl<<"你要:1.更新图书号;2.更新图书名称;3.更新作者;4.更新出版时间;5.更新ISBN号;5.更新书价;6.退出";
		int c;
		cout<<endl<<"请输入操作选项:";
	cin>>c;
	char numstr[50];

	switch(c)
	{	cout<<"你要更新为:";
	case 1:
		  int i,i1,k1;
		  cout<<"您选择的操作是:1——更新图书号(5位)"<<endl;
	      KeysType key;
	      printf("图书号更新为:");
		    for(i=0;i<MAX_NUM_OF_KEY;i++)
			{
		
			   cin>>key;
			   if(i==2&&key>'Z') key=(char)(key-'a'+'A');
			   if(key=='#') break;
			   L.r[0].keys[i]=key;
			}
for( i1=1;i1<=L.recnum;i1++)
	while(Equal(L.r[0].keys,L.r[i1].keys))            //抛出重复的异常
	{	printf("\a");                       //声音提示
		cout<<"这个图书号已存在,请重新输入(否则按'#'结束):";
		if(key=='#') break;
		for(int k=0;k<MAX_NUM_OF_KEY;k++)
	{
		cin>>key;
		if(k==2&&key>'Z') key=(char)(key-'a'+'A');
		if(key=='#') break;
		L.r[0].keys[k]=key;
	}
		}
	for( k1=0;k1<MAX_NUM_OF_KEY;k1++)
	L.r[findnum].keys[k1]=L.r[0].keys[k1];
   if(key=='#') break;
    n2=true;
    break;
	case 2:
		cout<<"您选择的操作是:2——更新图书名称"<<endl;
		printf("图书名称更新为:");
		gets(L.r[findnum].otheritems.bookname);
		while(strlen(L.r[findnum].otheritems.bookname)==0)        //抛出输入为空的异常
	{	printf("\a");                     //声音提示
		printf("图书名称不能为空,请重新输入:");
		gets(L.r[findnum].otheritems.bookname);
	}n2=true;
        break;

	case 3:
		cout<<"您选择的操作是:3——更新作者"<<endl;
		printf("作者更新为:");
		gets(L.r[findnum].otheritems.author);	
		while(strlen(L.r[findnum].otheritems.author)==0)        //抛出输入为空的异常
	{	printf("\a");                     //声音提示
		printf("图书作者不能为空,请重新输入:");
		gets(L.r[findnum].otheritems.author);
	}n2=true;
        break;
	case 4:
	cout<<"您选择的操作是:4——更新出版时间"<<endl;
		printf("出版时间更新为:");
		gets(L.r[findnum].otheritems.publishtime);	
	while(strlen(L.r[findnum].otheritems.publishtime)==0)        //抛出输入为空的异常
	{	printf("\a");                     //声音提示
		printf("出版时间不能为空,请重新输入:");
		gets(L.r[findnum].otheritems.publishtime);
	}n2=true;
        break;
case 5:
		cout<<"您选择的操作是:5——更新ISBN号"<<endl;
		printf("ISBN号更新为:");
		gets(L.r[findnum].otheritems.ISBN);	
		while(strlen(L.r[findnum].otheritems.ISBN)==0)        //抛出输入为空的异常
	{	printf("\a");                     //声音提示
		printf("图书作者不能为空,请重新输入:");
		gets(L.r[findnum].otheritems.ISBN);
	}n2=true;
        break;
	case 6:
		cout<<"您选择的操作是:4——更新单价"<<endl;
		printf("单价更新为:");
		gets(numstr);
		while(!(atof(numstr)>0.0))
	{	printf("\a");                       //声音提示
		printf("非法输入,请重新输入:");
		gets(numstr);
		}                                  //抛出输入单价的异常
	    L.r[findnum].otheritems.price=atof(numstr);
		n2=true;
        break;

	case 7:
		cout<<"您选择的操作是:6——退出"<<endl;
		n2=false;
        break;
	}
		cout<<"恭喜你,修改成功!"<<endl;
		cout<<endl<<"结果如下:"<<endl;
		SLListTraverse(L);
	} 
		}
		}//if
      break;
	
	case 'd':                               //删除   
	case 'D':
		if(L.recnum==0)
		{ 	printf("\a");                  //声音提示
			cout<<endl<<"还没数据!";
		}                                  //抛出异常
		else{	DeleteElem(L,p,H);  }
		n1=true;
	    break;

	case 'p':                              //打印全部数据
	case 'P':
        cout<<"您选择的操作——总览(print-all)"<<endl;
		if(L.recnum==0)
		{ 	printf("\a");                  //声音提示
			cout<<endl<<"还没数据,无法查看!";
		}
		else
		{
			SLListTraverse(L);
		}
        n1=true;
	    break;

	case 'w':                              //保存链表数据write
    case 'r':                              //读取链表数据read
	case 'R':
		cout<<"您选择的操作——读取文件(readfile)"<<endl;
		ReadFile(L);
		n1=true;
		break;

	case 'q':                                //退出
	case 'Q':
		char YorN;
		cout<<"您选择的操作——退出(quit)"<<endl;
		if(L.recnum==0)
		{ 	printf("\a");                    //声音提示
			cout<<endl<<"你真的要退出吗?(还没数据)"<<"是(y)  否(n)";
		}
		else
		{	
		printf("\a");                        //声音提示
		cout<<"你真的要退出吗?(请先保存数据)"<<"是(y)  否(n)";
		}
		cin>>YorN;
		switch(YorN)
		{
		case 'y':
		case 'Y':
		WriteFile(L);
		n1=false;
		printf("\a");                       //声音提示
		cout<<"很荣幸为你服务!";
		break;

		case 'n':
		case 'N':
		n1=true;
		cout<<"请继续!";
		break;

		default:
		printf("\a");                       //声音提示
        cout<<"非法输入,请输入y或n!";
        n1=true;
		break;
		}
        break;

		default:
		printf("\a");                       //声音提示
        cout<<"非法输入,请输入上述字母中的一个!";//抛出异常
		break;
	}
	}
}

⌨️ 快捷键说明

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