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

📄 crecord.cpp

📁 数据结构大作业-变长记录类库
💻 CPP
📖 第 1 页 / 共 3 页
字号:

  void ChangeRecord::MarkSearch2(char*fname1,ElemType&x)
  {
      ifstream ifs(fname1, ios::in|ios::nocreate|ios::binary);
      if(!ifs) 
      {
	Imageoutput(250,60,"没有找到文件",0);
	return;
      }
      ifs.seekg(0,ios::end);
      int b2=sizeof(ElemType);
      int n=ifs.tellg()/b2;
      ifs.seekg(0);
      for(int i=0;i<n;i++)
      {
	ElemType tm;
	ifs.seekg(i*b2);
	ifs.read((char*)&tm,b2);
	if(tm.key!=DeleteMark&&strcmp(x.Mark,tm.Mark)==0)
	{
	  x=tm;
	  Imageoutput(202,40,"记录如下",0);
	  setviewport(1,119,635,415,1);
	  clearviewport();
	  rectangle(1,40,634,62);
	  gprintf(9,41,"%d",x.key);
	  line(200,40,200,62);
	  gprintf(209,41,"%s",x.Mark);
	  line(400,40,400,62);
	  gprintf(409,41,"%s",x.Elem);
	  setviewport(0,0,639,420,1);
	  return;
	}
      }
      ifs.close();
      Imageoutput(250,40,"没有找到",0);
      return; 
  }

  void ChangeRecord::MarkReplace(char*fname1,ElemType&x)
  {
      ifstream ifs(fname1,ios::in|ios::nocreate|ios::binary);
      if(!ifs)
      {
	Imageoutput(250,60,"没有找到文件",0);
	return;
      }
      ofstream ofs("C:\\temp2",ios::out|ios::binary);
      if(!ofs)
      {
	Imageoutput(250,60,"没有打开文件",0);
	return;
      }
      int b1=sizeof(ElemType);
      ElemType tm,y;
      int flags=0;
      int n;
      int k;
      while(!ifs.eof())
      {
	ifs.read((char*)&tm,b1);
	if(ifs.eof())
	  break;
	if(strcmp(tm.Mark,x.Mark)==0&&tm.key!=DeleteMark)
	{ 
	  do
	  {
	    Imageoutput(202,40,"输入新的记录标识",0);
	    vsget(202,180,"%s",y.Mark);
	    k=MarkSearch(fname1,y);
	    if(k)
	    {
	      Imageoutput(202,40,"记录标识已经存在",0);
	      Imageoutput(250,60,"重新输入",1);
	      vsget(202,200,"%s",y.Mark);
	    }
            n=strlen(y.Mark);
	  }while(n<1||n>10);
          strcpy(tm.Mark,y.Mark);
	  ofs.write((char*)&tm,b1);
	  Imageoutput(202,40,"修改记录标识成功",0);
	  flags=1;
          continue;
	}
	if(strcmp(tm.Mark,x.Mark)!=0)
	{
	  ofs.write((char*)&tm,b1);
	}
      }
      if(!flags)
      {
        Imageoutput(250,40,"没有找到",0);
	ifs.close();
	ofs.close();
	return;
      }
      ifs.close();
      ofs.close();
      remove(fname1);
      rename("C:\\temp2",fname1);
  }
  
  void ChangeRecord::RecordReplace(char*fname1,ElemType&x)
  {
      ifstream ifs(fname1,ios::in|ios::nocreate|ios::binary);
      if(!ifs)
      {
        Imageoutput(250,60,"没有找到文件",0);
	return;
      }
      ofstream ofs("C:\\temp2",ios::out|ios::binary);
      if(!ofs)
      {
	Imageoutput(250,60,"没有打开文件",0);
	return;
      }
      int b1=sizeof(ElemType);
      ElemType tm,y;
      int flags=0;
      char choice[1];
      while(!ifs.eof())
      {
	ifs.read((char*)&tm,b1);
	if(ifs.eof())
	  break;
	if(strcmp(tm.Mark,x.Mark)==0&&tm.key!=DeleteMark)
	{ 
	  Imageoutput(202,40,"记录改为链接方式吗",0);
          outtextxy(350,155,"(y/n)");
	  vsget(202,180,"%s",choice);
	  if(strcmp(choice,"y")==0)
            y.kind=1;
	  else
            y.kind=0;
          tm.kind=y.kind;
	  Imageoutput(202,40,"输入新的记录内容",0);
	  vsget(202,180,"%s",y.Elem);
	  tm.len=y.len=strlen(y.Elem);
          strcpy(tm.Elem,y.Elem);
	  ofs.write((char*)&tm,b1);
	  Imageoutput(202,40,"重写记录成功",0);
	  flags=1;
	}
	if(strcmp(tm.Mark,x.Mark)!=0)
	{
	  ofs.write((char*)&tm,b1);
	}
      }
      if(!flags)
      {
        Imageoutput(250,40,"没有找到",0);
	ifs.close();
	ofs.close();
	return;
      }
      ifs.close();
      ofs.close();
      remove(fname1);
      rename("C:\\temp2",fname1);
  }

  void ChangeRecord::PrintLinkFile(ElemType&x)
  {
      char *fname3;
      strcpy(fname3,x.Elem);
      char choice[1];
      void*buf;
      int size;
      int key;
      size=imagesize(180,130,530,240);
      if(size!=-1){
        buf=malloc(size);
	if(buf)
	  getimage(180,130,530,240,buf);
      }
      rectangle(190,132,528,238);
      setfillstyle(SOLID_FILL,1);
      bar(191,133,527,237);
      out16hz(202,155,0,14,"是否打开文件");
      outtextxy(300,155,"(n/y)");
      vsget(202,180,"%s",choice);
      if(strcmp(choice,"y")==0){
        char c[40];
	ifstream finin(fname3,ios::in|ios::nocreate|ios::binary);
	if(!finin)
	{
	  rectangle(190,132,528,238);
	  setfillstyle(SOLID_FILL,1);
	  bar(191,133,527,237);
	  out16hz(202,155,0,14,"没有找到文件");
	  for(long int count=0;count<100000000;count++);
	  putimage(180,130,buf,COPY_PUT);
	  free(buf);
	  return;
	}
	rectangle(190,132,528,238);
	setfillstyle(SOLID_FILL,1);
	bar(191,133,527,237);
	finin.read((char*)&c,35);
	gprintf(200,150,"%s",c);
	key=bioskey(0);
	if(key==Enter);
      }
      putimage(180,130,buf,COPY_PUT);
      free(buf);
  }

  ////////////////////
  void ChangeRecord::SeqInsert(IndexItem A[], int mm, IndexItem x)
  {
      for(int i=mm-1; i>=0; i--) 
      { 
        if(A[i].key>x.key)
	  A[i+1]=A[i];
        else 
	{
	  A[i+1]=x;
	  break;
	}
      }
      if(i<0) 
        A[0]=x;
  }

  void ChangeRecord::IFInsert(char *fname2, IndexItem x)
  {
      ifstream ifs(fname2, ios::in|ios::binary);
      if(!ifs) 
      {
	Imageoutput(250,60,"没有找到文件",0);
	return;
      }
      ofstream ofs("C:\\temp",ios::out|ios::binary);
      if(!ofs) 
      {
	Imageoutput(250,60,"没有打开文件",0);
	return;
      }
      IndexItem* A=new IndexItem[m+1];
      ifs.seekg(0);
      int b2=sizeof(IndexItem);
      while(1)
      {
        ifs.read((char*)A, m*b2);
	int s=ifs.gcount()/b2;
	if(s==m)
	{  
	  if(A[m-1].key<x.key) 
	  {
	    ofs.write((char*)A, m*b2);
	  }
          else 
	  {
	    SeqInsert(A, m, x);  
	    ofs.write((char*)A,(m+1)*b2);
	    while(!ifs.eof()) 
	    {  
	      ifs.read((char*)A, m*b2);
              s=ifs.gcount()/b2;
	      ofs.write((char*)A, s*b2);
	    }
	    break;  
	  }
	}
	else
	{
	  SeqInsert(A, s, x);
	  ofs.write((char*)A, (s+1)*b2);
	  break; 
	}
      }
      delete [] A;
      ifs.close();
      ofs.close();
      remove(fname2);
      rename("C:\\temp", fname2);
  }


  int ChangeRecord::SeqDelete(IndexItem A[], int mm, IndexItem& x)
  {
      int i=0;
      while(i<mm && A[i].key<x.key)
        i++;
      if(i==mm || A[i].key!=x.key) return 0;
      x=A[i]; 
      for(int j=i+1; j<mm; j++) 
        A[j-1]=A[j];
      return 1;
  }

  int ChangeRecord::IFDelete(char*fname2,IndexItem&x)
  {
      ifstream ifs(fname2, ios::in|ios::nocreate|ios::binary);
      if(!ifs) 
      {
        Imageoutput(250,60,"没有找到文件",0);
	return 0;
      }
      ofstream ofs("C:\\temp",ios::out|ios::binary);
      if(!ofs) 
      {
        Imageoutput(250,60,"没有打开文件",0);
	return 0;
      }
      int b2=sizeof(IndexItem);
      IndexItem* A=new IndexItem[m];
      int d;
      while(1)
      {
        ifs.read((char*)A, m*b2);
	int s=ifs.gcount()/b2;
	if(s==m)
	{ 
	  if(A[m-1].key<x.key) 
	  {
	    ofs.write((char*)A, m*b2);
	  }
          else 
	  {
	    d=SeqDelete(A, m, x);
	    if(d) 
	      ofs.write((char*)A,(m-1)*b2);
	    else 
	      ofs.write((char*)A,m*b2);
	    while(!ifs.eof()) 
	    { 
	      ifs.read((char*)A, m*b2);
	      s=ifs.gcount()/b2;
	      ofs.write((char*)A, s*b2);
	    }
	    break;  
	  }
	}
	else
	{
	  d=SeqDelete(A, s, x);
	  if(d) 
	    ofs.write((char*)A, (s-1)*b2);
	  else 
	    ofs.write((char*)A,s*b2);
	  break; 
	}
      }
      delete [] A;
      ifs.close();
      ofs.close();
      remove(fname2);
      rename("C:\\temp", fname2);
      if(d) return 1;
      else return 0;
  }

  int ChangeRecord::IFSearch(char*fname2,IndexItem&x)
  {
      ifstream ifs(fname2, ios::in|ios::nocreate|ios::binary);
      if(!ifs) 
      {
	Imageoutput(250,60,"没有找到文件",0);
	return 0;
      }
      ifs.seekg(0,ios::end);
      int b2=sizeof(IndexItem);
      int n=ifs.tellg()/b2;
      ifs.seekg(0);
      int low=0, high=n-1;
      while(low<=high)
      {	
      	int mid=(low+high)/2;
	IndexItem tm;
	ifs.seekg(mid*b2);
	ifs.read((char*)&tm,b2);
	if(x.key==tm.key) 
	{
	  x=tm;
	  ifs.close();
	  return 1;
	}
	else if(x.key<tm.key)
	  high=mid-1;
	else
	  low=mid+1;
      }
      ifs.close();
      return 0;  
  }
  int ChangeRecord::MarkSearch(char*fname1,ElemType&x)
  {
      ifstream ifs(fname1, ios::in|ios::nocreate|ios::binary);
      if(!ifs) 
      {
	Imageoutput(250,60,"没有找到文件",0);
	return 0;
      }
      ifs.seekg(0,ios::end);
      int b2=sizeof(ElemType);
      int n=ifs.tellg()/b2;
      ifs.seekg(0);
      for(int i=0;i<n;i++)
      {
        ElemType tm;
	ifs.seekg(i*b2);
	ifs.read((char*)&tm,b2);
	if(tm.key!=DeleteMark&&strcmp(x.Mark,tm.Mark)==0) 
	{
	  ifs.close();
	  return 1;
	}
      }
      ifs.close();
      return 0;  
  }
  
  int ChangeRecord::KeySearch(char*fname1,ElemType&x)
  {
      ifstream ifs(fname1, ios::in|ios::nocreate|ios::binary);
      if(!ifs) 
      {
	Imageoutput(250,60,"没有找到文件",0);
	return 0;
      }
      ifs.seekg(0,ios::end);
      int b2=sizeof(ElemType);
      int n=ifs.tellg()/b2;
      ifs.seekg(0);
      for(int i=0;i<n;i++)
      {
	ElemType tm;
	ifs.seekg(i*b2);
	ifs.read((char*)&tm,b2);
	if(tm.key!=DeleteMark&&tm.key==x.key) 
	{
	  ifs.close();
	  return 1;
	}
      }
      ifs.close();
      return 0;  
  }


  void ChangeRecord::Imageoutput(int x,int y,char*s,int choice)
  {
      if(!choice)
      {
      	setviewport(1,120,637,415,1);
	clearviewport();
	rectangle(200,20,500,120);
	out16hz(x,y,0,14,s);
      }
      else{
        setviewport(1,120,637,415,1);
	out16hz(x,y,0,14,s);
      }
      setviewport(0,0,639,420,1); 
  }

  //----------------------------------------------------------------------------------------------------------------------------------------------------------------------
  #include<bios.h>
  #include<dos.h>
  #include<io.h>
  #include<alloc.h>
  #include<stdio.h>
  #include<stdlib.h>
  #include<conio.h>
  #include<fcntl.h>
  #include<graphics.h>
  #define RIGHT     77
  #define LEFT      75
  #define UP        72
  #define DOWN      80
  #define ESC       27
  #define F1        59
  #define ENTER     13
  #define ALT_X     45
  #define SPACE     32
  #define MAX_FRAME 5


  void main_frame();
  void pull_menu(int oldnum);
  void dis_menu(int num);
  void help();
  int  specialkey();
  void keybuffer(char k1,char k2);
  int  out24hz(int,int,int,int,int,int,char*);
  int  convert(unsigned char ch,int n);
  void Imageoutput(int x,int y,char*s,int choice);
  int  handle; 
  void*buffer;
  void*branch_buf;
  struct menu_frame{
      int startx,starty;
      int count;
      char*content[5];
      void*p;
  }frame[MAX_FRAME]={
    {109,34,4,{"成块插入","数字删除","标识删除","放弃操作",},},
    {215,34,3,{"数字查找","标识查找","放弃操作",},},
    {321,34,3,{"修改标识","重写记录","放弃操作",},},
    {427,34,4,{"读取记录","读取索引","回收站","放弃操作",},},
    {533,34,2,{"退出","放弃",},},
  };
  char*menu[]={"提示帮助","增删操作","查找操作","修改操作","顺序读取","退出系统",};
  char*bottom[]={"帮助","移动光条","选择","返回","退出",};
  int  select;
  int  flag16,flag24;

  void main()
  {
      register int i;
      int x;
      char c;
      int Driver=DETECT,Mode=0;

⌨️ 快捷键说明

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