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

📄 crecord.cpp

📁 数据结构大作业-变长记录类库
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  /*This demo chinese pulldown menu*/
  /*It needn't chinese DOS         */
  #include<iomanip.h>
  #include<fstream.h>
  #include<string.h>
  #include<iostream.h>
  #include<stdio.h>
  #include<bios.h>
  #include<stdlib.h>
  #include<graphics.h>
  #include<stdarg.h>
  #include<dos.h>
  #include<conio.h>
  #define DOWN        80
  #define ESC         27
  #define Enter       0x1c0d
  #define Left        0x4b00
  #define SpaceBack   0x0e08
  /////////////////////
  int gprintf(int xloc,int yloc,char*fmt,...)
  {
      va_list argptr;
      char str[140];
      int cnt,xp,yp;
      va_start(argptr,fmt);
      cnt=vsprintf(str,fmt,argptr);
      outtextxy(xloc,yloc,str);
      yloc+=textheight("H")+2;
      va_end(argptr);
      return(cnt);
  }

  void getstr(int x,int y,char*str)
  {
      int key;
      char ch;
      char s[2];
      int i=0;
      s[1]='\0';
      setfillstyle(SOLID_FILL,BLACK);
      outtextxy(x+i*15,y,"-");
      while((key=bioskey(0))!=Enter)
      {
        if((key==SpaceBack)||(key==Left))
        {
          if(i>0){
	    i--;
	    bar(x+(i%18)*15,y+(i/18)*25,x+(i%18+2)*15,y+(i/18)*25+25);
	    if(i%18!=0||i==0)outtextxy(x+(i%18)*15,y+(i/18)*25,"-");
          }
        }
        else 
	{
	  ch=(char)(key&0x00ff);
	  if(ch){
	    str[i]=ch;
	    s[0]=ch;
	    bar(x+(i%18)*15,y+(i/18)*25,x+(i%18+1)*15,y+(i/18)*25+25);
	    outtextxy(x+(i%18)*15,y+(i/18)*25,s);
	    if((i+1)%18!=0)outtextxy(x+(i%18+1)*15,y+(i/18)*25,"-");
            i++;
          }
          else{
            sound(1000);
            delay(100);
            nosound();
          }
        }
      }
      str[i]='\0';
      bar(x+(i%18)*15,y+(i/18)*25,x+(i%18+1)*15,y+(i/18)*25+25);
  }

  void vsget(int x,int y,char *fmt,...)
  {
      va_list vptr;
      char str[256];
      getstr(x,y,str);
      va_start(vptr,fmt);
      vsscanf(str,fmt,vptr);
      va_end(vptr);
  }
  int out16hz(int,int,int,int,char*);
  /////////////////////
  const int m         =5;
  const int DeleteMark=-100;
  const int MAXLEN    =100;
  typedef int KeyType;
  typedef int Elemkind;
  struct ElemType 
  {    
      KeyType  key;      
      char     Mark[10];   
      int      len;      
      Elemkind kind;    
      char     Elem[MAXLEN];
  };

  struct IndexItem 
  {  
      KeyType  key;     
      int      next;         
  };
  class ChangeRecord
  {
  private:
      //...
      void PrintLinkFile(ElemType&x);
      void IFInsert(char *fname2, IndexItem x);
      int  IFDelete(char *fname2, IndexItem& x);
      int  IFSearch(char* fname2, IndexItem& x);
      void Imageoutput(int x,int y,char*s,int choice);
      void SeqInsert(IndexItem A[], int mm, IndexItem x);
      int  SeqDelete(IndexItem A[], int mm, IndexItem& x);
  public:
      //...
      void PrintMainFile(char* fname1);
      void PrintIndexFile(char* fname2);
      void PrintDeleteFile(char* fname1);
      void MFAppend(char* fname1, char* fname2, ElemType a[], int n);
      void MFDelete(char* fname1, char* fname2, KeyType a[], int n);
      void MFSearch(char* fname1, char* fname2, KeyType a[], int n);
      int  MarkSearch(char*fname1,ElemType&x);
      int  KeySearch(char*fname1,ElemType&x);
      void MarkSearch2(char*fname1,ElemType&x);
      void MarkDelete(char*fname1,char*fname2,ElemType&x);
      void MarkReplace(char*fname1,ElemType&x);
      void RecordReplace(char*fname1,ElemType&x);
  };
  ////////////////////
  void ChangeRecord::PrintMainFile(char* fname1)
  {
      int key;
      int flag=0;
      int clock=0;
      int cx=0;
      ifstream fin(fname1, ios::in|ios::nocreate|ios::binary);
      if(!fin)
      {
        Imageoutput(250,60,"没有找到文件",0);
	return;
      }
      ElemType x;
      fin.seekg(0,ios::end);         
      int b1=sizeof(ElemType);
      int n=fin.tellg()/b1;         	 
      fin.seekg(0);
      setviewport(1,119,637,415,1);
      clearviewport();
      rectangle(1,1,635,295);
      outtextxy(9,21,"KeyWord");
      outtextxy(209,21,"Record id");
      outtextxy(409,21,"Data Contents");
      line(1,22,635,22);
      line(1,44,635,44);
      for(int i=0;i<n;i++,cx++)
      {
	fin.read((char*) &x, b1);
	if(cx<12)
	{
	  if(x.key==DeleteMark)
	  {cx--;continue;}
	  if(x.kind==1)
	  {
	    gprintf(9,41+cx*20,"%d",x.key);
	    gprintf(209,41+cx*20,"%s",x.Mark);
	    gprintf(409,41+cx*20,"%s",x.Elem);
	    line(1,41+cx*20+22,635,41+cx*20+22);
	    rectangle(580,41+(cx)*20+2,630,41+(cx+1)*20);
	    outtextxy(581,41+(cx)*20,"open?");
	    PrintLinkFile(x);
	  }
	  if(x.kind==0)
	  {
	    line(1,41+cx*20+22,635,41+20*cx+22);
	    gprintf(9,41+cx*20,"%d",x.key);
	    gprintf(209,41+cx*20,"%s",x.Mark);
	    gprintf(409,41+cx*20,"%s",x.Elem);
	  }
	}
	if((cx>0)&&(cx-12)%12==0)
	{
	  while(bioskey(1)==0);
	  key=bioskey(0);
	  key=key&0xff?key&0xff:key>>8;
	  if(key==DOWN)
	  {
            setviewport(4,161,635,410,1);
	    clearviewport();
	    flag=1;
	    clock++;
	  }
	}
	if(flag)
	{
	  if(x.key==DeleteMark)
	  {cx--;continue;}
	  if(x.kind==1)
	  {
	    gprintf(9,0+(cx-12*clock)*20,"%d",x.key);
	    gprintf(209,0+(cx-12*clock)*20,"%s",x.Mark);
	    gprintf(409,0+(cx-12*clock)*20,"%s",x.Elem);
	    line(1,0+(cx-12*clock)*20+22,635,0+(cx-12*clock)*20+22);
	    rectangle(580,(cx-12*clock)*20,630,(cx-12*clock+1)*20);
	    outtextxy(581,(cx-12*clock)*20,"open?");
	    PrintLinkFile(x);
	  }
	  if(x.kind==0)
	  {
	    line(1,0+(cx-12*clock)*20+22,635,0+(cx-12*clock)*20+22);
	    gprintf(9,0+(cx-12*clock)*20,"%d",x.key);
	    gprintf(209,0+(cx-12*clock)*20,"%s",x.Mark);
	    gprintf(409,0+(cx-12*clock)*20,"%s",x.Elem);
	  }
	}
      }
      fin.close();
      setviewport(0,0,639,420,1);
  }

  void ChangeRecord::PrintIndexFile(char* fname2)
  {
      int key;
      int flag=0;
      int clock=0;
      ifstream fin(fname2, ios::in|ios::nocreate|ios::binary);
      if(!fin) 
      {
        Imageoutput(250,60,"没有找到文件",0);
	return;
      }
      IndexItem x;
      fin.seekg(0,ios::end);      
      int b2=sizeof(IndexItem);
      int n=fin.tellg()/b2;         
      fin.seekg(0);
      setviewport(1,119,637,415,1);
      clearviewport();
      rectangle(1,1,635,295);
      line(1,22,635,22);
      outtextxy(199,21,"KeyWord");
      outtextxy(399,21,"Record Memory Place");
      line(1,44,635,44);
      for(int i=0;i<n;i++)
      {
        fin.read((char*) &x, b2);
	if(i<12)
	{   
	  line(1,41+i*20+22,635,41+i*20+22);
	  gprintf(199,41+i*20,"%d",x.key);
	  gprintf(399,41+i*20,"%d",x.next);
	}
	if((i>0)&&(i-12)%12==0)
	{
	  while(bioskey(1)==0);
	  key=bioskey(0);
	  key=key&0xff?key&0xff:key>>8;
	  if(key==DOWN)
          {
	    setviewport(4,161,635,410,1);
	    clearviewport();
	    flag=1;
	    clock++;
	  }
	}
	if(flag)
	{   
          line(1,0+(i-12*clock)*20+22,635,0+(i-12*clock)*20+22);
	  gprintf(209,0+(i-12*clock)*20,"%d",x.key);
	  gprintf(409,0+(i-12*clock)*20,"%d",x.next);
	}
      }
      fin.close();
      setviewport(0,0,639,420,1);
  }

  void ChangeRecord::PrintDeleteFile(char* fname1)
  {
      int key;
      int flag=0;
      int clock=0;
      int cx=0;
      ifstream fin(fname1, ios::in|ios::nocreate|ios::binary);
      if(!fin)
      {
	Imageoutput(250,60,"没有找到文件",0);
	return;
      }
      ElemType x;
      fin.seekg(0,ios::end);        
      int b1=sizeof(ElemType);
      int n=fin.tellg()/b1;          	 
      fin.seekg(0);
      setviewport(1,119,637,415,1);
      clearviewport();
      rectangle(1,1,635,295);
      line(1,22,635,22);
      outtextxy(9,21,"KeyWord");
      outtextxy(209,21,"Record id");
      outtextxy(409,21,"Data Contents");
      line(1,44,635,44);
      for(int i=0;i<n;i++)
      {
        fin.read((char*)&x, b1);    
	if(x.key==DeleteMark)
	{
	  if(cx<12)
	  {
	    if(x.kind==1)                 
	    {
	      gprintf(9,41+cx*20,"%d",x.key);
	      gprintf(209,41+cx*20,"%s",x.Mark);
              gprintf(409,41+cx*20,"%s",x.Elem);
	      line(1,41+cx*20+22,635,41+cx*20+22);
	      rectangle(580,41+cx*20+2,630,41+(cx+1)*20);
	      outtextxy(581,41+cx*20,"open?");
	      PrintLinkFile(x);
	    }
	    if(x.kind==0)               
	    { 
	      line(1,41+cx*20+22,635,41+cx*20+22);
	      gprintf(9,41+cx*20,"%d",x.key);
	      gprintf(209,41+cx*20,"%s",x.Mark);
	      gprintf(409,41+cx*20,"%s",x.Elem);
	    }
	  }
	  if((cx>0)&&(cx-12)%12==0)
	  {
	    while(bioskey(1)==0);
	    key=bioskey(0);
	    key=key&0xff?key&0xff:key>>8;
	    if(key==DOWN)
	    {
	      setviewport(4,161,635,410,1);
	      clearviewport();
	      flag=1;
	      clock++;
	    }
	  }
	  if(flag)
	  {
	    if(x.kind==1)
	    {
              gprintf(9,0+(cx-12*clock)*20,"%d",x.key);
	      gprintf(209,0+(cx-12*clock)*20,"%s",x.Mark);
	      gprintf(409,0+(cx-12*clock)*20,"%s",x.Elem);
	      line(1,0+(cx-12*clock)*20+22,635,0+(cx-12*clock)*20+22);
	      rectangle(580,(cx-12*clock)*20,630,(cx-12*clock+1)*20);
	      outtextxy(581,(cx-12*clock)*20,"open?");
	      PrintLinkFile(x);
	    }
	    if(x.kind==0)               
	    { 
	      line(1,0+(cx-12*clock)*20+22,635,0+(cx-12*clock)*20+22);
	      gprintf(9,0+(cx-12*clock)*20,"%d",x.key);
	      gprintf(209,0+(cx-12*clock)*20,"%s",x.Mark);
	      gprintf(409,0+(cx-12*clock)*20,"%s",x.Elem);
	    }
	  }
	  cx++;
	}
	else continue;
      }
      fin.close();
      setviewport(0,0,639,420,1);
  }

  void ChangeRecord::MFAppend(char* fname1, char* fname2, ElemType a[], int n)
  {
      ofstream ofs(fname1,ios::app|ios::binary);
      if(!ofs) 
      {
        Imageoutput(250,60,"没有找到文件",0);
	return;
      }
      int i;
      int b1=sizeof(ElemType);
      ofs.seekp(0, ios::end);
      int flen=ofs.tellp()/b1;
      for(i=0; i<n; i++) 
        ofs.write((char*) &a[i], b1);
      ofs.close();  
      IndexItem x;
      for(i=0; i<n; i++) 
      {
        x.key=a[i].key;
	x.next=flen+i;
	IFInsert(fname2, x); 
      }
  }

  void ChangeRecord::MFDelete(char* fname1, char* fname2, KeyType a[], int n)
  {
      fstream fio(fname1,ios::in|ios::out|
		ios::nocreate|ios::binary);
      if(!fio) 
      {
        Imageoutput(250,60,"没有找到文件",0);
	return;
      }
      int b1=sizeof(ElemType);
      IndexItem x;
      ElemType y;
      int i;	
      for(i=0; i<n; i++) 
      {
        x.key=a[i];
	int k=IFDelete(fname2,x);
	if(!k) 
	{
	  Imageoutput(202,40,"记录没有找到",0);
	  continue;
	}
	fio.seekg(x.next*b1);
	fio.read((char*) &y, b1);
	y.key=DeleteMark;
	fio.seekg(-b1, ios::cur);
	fio.write((char*) &y, b1);
	Imageoutput(250,40,"删除成功",0);
      }
      fio.close();  
  }

  void ChangeRecord::MarkDelete(char*fname1,char*fname2,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;
      IndexItem y;
      int flags=0;
      ifs.seekg(0);
      while(!ifs.eof())
      {
        ifs.read((char*)&tm,b1);
	if(ifs.eof())
	  break;
	if(strcmp(tm.Mark,x.Mark)==0&&tm.key!=DeleteMark)
	{
          y.key=tm.key;
	  int k=IFDelete(fname2,y);
	  if(k)
	  {
	    tm.key=DeleteMark;
	    ofs.write((char*)&tm,b1);
	    Imageoutput(250,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::MFSearch(char* fname1, char* fname2, KeyType a[], int n)
  {
      ifstream ifs(fname1,ios::in|ios::nocreate|ios::binary);
      if(!ifs) 
      {
	Imageoutput(250,60,"没有找到文件",0);
	return;
      }
      int b1=sizeof(ElemType);
      IndexItem x;
      ElemType y;
      int i;
      for(i=0; i<n; i++) 
      {
	x.key=a[i];
	int k=IFSearch(fname2,x);
	if(!k)
	{
	  Imageoutput(202,40,"记录没有找到",0);
	  continue;
	}
	ifs.seekg(x.next*b1);
	ifs.read((char*) &y, b1);
	Imageoutput(202,40,"记录已经找到",0);
	Imageoutput(202,60,"如下",1);
	setviewport(1,119,635,415,1);
	clearviewport();
	rectangle(1,40,634,62);
	gprintf(9,41,"%d",y.key);
	line(200,40,200,62);
	gprintf(209,41,"%s",y.Mark);
	line(400,40,400,62);
	gprintf(409,41,"%s",y.Elem);
	setviewport(0,0,639,420,1);
      }
      ifs.close();  
  }

⌨️ 快捷键说明

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