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

📄 main.cpp

📁 数据结构大作业的非界面版
💻 CPP
字号:
//main.cpp
//////////////////////////////////////////////////////////////////////
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<vector>
#include"changerecord.h"

void InterFace(ChangeRecord test,    //接口函数,test变长记录类
	ElemType a[],                //主文件中记录类型
        KeyType b[],                 //关键字类型
	char* fname1,                //主文件
        char* fname2,                //索引有序文件
	int i,                       //循环计数器
	int m,                       //块大小
	int n,                       //块大小,记录长度缓冲器
	char choice,                 //菜单选项1
	char choice2);               //菜单选项2
//...
////////////////////////////////////////////////////////////////////
void input1(char*fname1)
{
       cout<<"           输入主文件地址"<<endl;
       cout<<"           ";
       cin>>fname1;
       fstream iof(fname1,ios::in|ios::out|ios::binary);
       iof.close();
}
//...
///////////////////////////////////////////////////////////////////
void input2(char*fname2)
{
       cout<<"           输入索引有序文件地址"<<endl;
       cout<<"           ";
       cin>>fname2;
       fstream iof(fname2,ios::in|ios::out|ios::binary);
       iof.close();
       system("cls");
}
//主函数
///////////////////////////////////////////////////////////////////////
void main()
{
	ChangeRecord test;
	ElemType a[MAXLEN];
        KeyType b[MAXLEN];
	int i=0;
	int m=0;
	int n=0;
	char choice=0;
	char choice1=0;
	char choice2=0;
	char fname1[30]="F:\\test.rec";
	char fname2[30]="F:\\index.dex";
	fstream iof1(fname1,ios::in|ios::out|ios::binary);
        fstream iof2(fname2,ios::in|ios::out|ios::binary);
	iof1.close();
	iof2.close();
	cout<<"          要求先在硬盘上建立两个文件:"<<endl
	    <<"          主文件(默认地址为F:\\test.rec)"<<endl
	    <<"          索引有序文件(默认地址为F:\\index.dex)"<<endl
	    <<"          是否要求更新地址(是/y)";
	cin>>choice1;
	if(choice1=='y')
	{
	  input1(fname1);
	  input2(fname2); 
	}
	system("cls");
	InterFace(test,a,b,fname1,fname2,i,m,n,choice,choice2);
}

//HELP
//////////////////////////////////////////////////////////////////////
void Help()
{
        cout<<endl<<"=========================================================="<<endl
	    <<"              help(功能菜单)"<<endl
	    <<"              1.插入记录(支持块插入)"<<endl<<endl
	    <<"              2.删除记录______1.关键字删除(支持块删除)"<<endl
	    <<"                          |"<<endl
	    <<"                          |___2.记录标识删除"<<endl
            <<"                          |"<<endl
	    <<"                          |___3.退出删除操作"<<endl<<endl
	    <<"              3.查找记录______1.关键字查找(支持块查找)"<<endl
            <<"                          |"<<endl
	    <<"                          |___2.记录标识查找"<<endl
            <<"                          |"<<endl
	    <<"                          |___3.退出查找操作"<<endl<<endl
	    <<"              4.修改记录______1.修改记录标识"<<endl
	    <<"                          |"<<endl
	    <<"                          |___2.重写记录内容"<<endl
	    <<"                          |"<<endl
	    <<"                          |___3.退出修改操作"<<endl<<endl
	    <<"              5.顺序读________1.顺序读出记录"<<endl
	    <<"                          |"<<endl
	    <<"                          |___2.顺序读出索引"<<endl
	    <<"                          |"<<endl
	    <<"                          |___3.顺序读出已删除记录"<<endl
	    <<"                          |"<<endl
	    <<"                          |___4.退出顺序读操作"<<endl<<endl
	    <<"              6.help;"<<endl<<endl
	    <<"              7.退出本程序;"<<endl;
}
//时间缓冲
//////////////////////////////////////////////////////////////////////
void count()
{
        int count=0;
	cout<<endl;
	cout<<"return top...";
	cout<<endl;
	while(count<=1000000000)
	{
	  count++;
	}
//        system("cls");
}
///////////////////////////////////////////////////////////////////////
//函数名称:接口实现函数                                              //
//功能:操作菜单的实现                                                //
//函数入口:见函数声明                                                //
//......                                                             // 
///////////////////////////////////////////////////////////////////////
  void InterFace(ChangeRecord test,
	ElemType a[],
        KeyType b[],
	char*fname1,
	char*fname2,
	int i,
	int m,
	int n,
	char choice,
	char choice2)
{
	bool k;
        cout<<endl<<"                  oooooooooooooooooo";
	cout<<endl<<"                  o变长记录类库操作o";
        cout<<endl<<"                  oooooooooooooooooo";
	cout<<endl<<"               CopyRight@CS022::zhangbin"<<endl;
	while(true)
	{
	  cout<<endl<<"=========================================================="<<endl
	      <<"              请选择操作:"<<endl
	      <<"              1.插入记录(支持块插入);"<<endl
	      <<"              2.删除记录;"<<endl
	      <<"              3.查找记录;"<<endl
	      <<"              4.修改记录;"<<endl
	      <<"              5.顺序读;"<<endl
	      <<"              6.help;"<<endl
	      <<"              7.退出本程序;";
	  cout<<endl<<"->";
	  cin>>choice;
	  while(choice!='1'&&choice!='2'&&choice!='3'&&choice!='4'&&choice!='5'&&choice!='6'&&choice!='7')
	  {
            cout<<"error choose!"<<endl;
	    cin>>choice;
	  }
	  if(choice=='1')
	  {
	    cout<<"=========================================================="<<endl;
            cout<<"              输入插入的记录个数(<="<<MAXLEN<<")"<<endl;
	    cout<<"              ";
	    cin>>m;
	    for(i=0;i<m;i++)
	    {
	      cout<<"              第"<<i+1<<"个记录"<<endl;
	      //..........
	      cout<<"              <>输入关键字"<<endl;
              cout<<"              ";
	      cin>>a[i].key;
	      do
	      {
                k=test.KeySearch(fname1,a[i]);
	        if(k)
		{
	          cout<<"              已存在,重新输入关键字"<<endl;
		  cout<<"              ";
		  cin>>a[i].key;
		}
	      }while(k);
	      //..........
              do
	      {
	        cout<<"             <>输入记录标识(唯一)"<<endl;
		cout<<"              ";
	        cin>>a[i].Mark;
		do
		{
                  k=test.MarkSearch(fname1,a[i]);
	          if(k)
		  {
	            cout<<"              已存在,重新输入记录标识"<<endl;
		    cout<<"              ";
		    cin>>a[i].Mark;
		  }
		}while(k);
	        n=strlen(a[i].Mark);
	      }while(n<1||n>10);
	      //...........
	      cout<<"             <>输入记录类型(0表示字节流,1表示链接方式)"<<endl;
	      cout<<"              ";
	      do
	      {
		cin>>a[i].kind;
	      }while(a[i].kind!=0&&a[i].kind!=1);
	      //...........
	      cout<<"             <>输入数据内容"<<endl;
	      cout<<"              ";
	      cin>>a[i].Elem;
	      //...........
	      a[i].len=strlen(a[i].Elem);
	      //...........
	    }
	    test.MFAppend(fname1,fname2,a,m);
	    count();
	  }
	  else if(choice=='2')
	  {
	    cout<<"=========================================================="<<endl;
            cout<<"              请选择操作:"<<endl
                <<"              1.关键字删除(支持块删除);"<<endl
		<<"              2.记录标识删除;"<<endl
		<<"              3.退出删除操作;"<<endl;
	    cout<<"->";
	    cin>>choice2;
            switch(choice2)
	    {
	      case '1':cout<<"              输入删除块的记录个数(<="<<MAXLEN<<")"<<endl;
                       cout<<"              ";
		       cin>>n;
		       cout<<"              <>输入删除记录的关键字"<<endl;
		       for(i=0;i<n;i++)
		       {
		         cout<<"              ";
		         cin>>b[i]; 
		       }
	               test.MFDelete(fname1,fname2,b,n);
		       count();
		       break;
              case '2':cout<<"              输入删除记录的记录标识"<<endl;
		       cout<<"              ";
		       cin>>a[1].Mark;
		       test.MarkDelete(fname1,fname2,a[1]);
                       count();
		       break;
              case '3':break;
              default:cout<<"error choose!"<<endl; 
		      break;
	    }
	    
	  }
	  else if(choice=='3')
	  {
	    cout<<"=========================================================="<<endl;
	    cout<<"              请选择操作:"<<endl
                <<"              1.关键字查找(支持块查找);"<<endl
		<<"              2.记录标识查找;"<<endl
		<<"              3.退出查找操作;"<<endl;
	    cout<<"->";
	    cin>>choice2;
	    switch(choice2)
	    {
	      case '1':cout<<"              输入查找块的记录个数(<="<<MAXLEN<<")"<<endl;
		       cout<<"              ";
		       cin>>n;
		       cout<<"              <>输入查找记录的关键字"<<endl;
		       for(i=0;i<n;i++)
		       {
		         cout<<"              ";   
		         cin>>b[i]; 
		       }
	               test.MFSearch(fname1,fname2,b,n); 
		       count();
		       break;
              case '2':cout<<"              输入查找记录的记录标识"<<endl;
		       cout<<"              ";
		       cin>>a[1].Mark;
		       test.MarkSearch2(fname1,a[1]);
		       count();
		       break;
	      case '3':break;
              default:cout<<"error choose!"<<endl; 
		      break;
	    }
	  }
	  else if(choice=='4')
	  {
	    cout<<"=========================================================="<<endl;
	    cout<<"              请选择操作:"<<endl
                <<"              1.修改记录标识;"<<endl
		<<"              2.重写记录数据;"<<endl
		<<"              3.退出修改操作;"<<endl;
	    cout<<"->";
	    cin>>choice2;
	    switch(choice2)
	    {
	      case '1':cout<<"              请输入修改的记录标识"<<endl;
		       cout<<"              ";
		       cin>>a[0].Mark;
		       test.MarkReplace(fname1,a[0]);
		       count();
		       break;
              case '2':cout<<"              请输入重写记录的的记录标识"<<endl;
		       cout<<"              ";
		       cin>>a[0].Mark;
		       test.RecordReplace(fname1,a[0]);
		       count();
		       break;
	      case '3':break;
              default:cout<<"error choose!"<<endl; 
		      break;
	    }
	  }
	  else if(choice=='5')
	  {
	    cout<<"=========================================================="<<endl;
	    cout<<"              请选择操作:"<<endl
                <<"              1.顺序读出记录;"<<endl
                <<"              2.顺序读出索引;"<<endl
		<<"              3.顺序读出已删除记录;"<<endl
		<<"              4.退出顺序读操作;"<<endl;
	    cout<<"->";
	    cin>>choice2;
	    switch(choice2)
	    {
	      case '1':cout<<"=========================================================="<<endl;
		       cout<<"              输出记录内容"<<endl;
		       cout<<"=========================================================="<<endl;
       	               test.PrintMainFile(fname1);
		       count();
		       break;
              case '2':cout<<"=========================================================="<<endl;
		       cout<<"              输出索引内容"<<endl;
		       cout<<"=========================================================="<<endl;
      	               test.PrintIndexFile(fname2);
		       count();
	               break;
	      case '3':cout<<"=========================================================="<<endl;
		       cout<<"              输出已删除记录内容"<<endl;
		       cout<<"=========================================================="<<endl;
       	               test.PrintDeleteFile(fname1);
		       count();
		       break;
	      case '4':break;
              default:cout<<"error choose!"<<endl; 
		      break;
	    }
	  }
	  else if(choice=='6')
	  {
	    Help();
	    count();
	  }
	  else if(choice=='7')
	    return;
        }
}      

⌨️ 快捷键说明

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