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

📄 dzy.cpp

📁 一个数据结构的大作业
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				temp.close();
		}//end if
		else{
			cout<<"没有找到匹配记录号!请重新确定输入是否有误"<<endl;
		}//end else
		break;
	case '0'://0.返回
		cout<<"你确实想返回吗?(Y/N)"<<endl;
		cin>>ch;
        if(ch=='y'||ch=='Y')	return;
		else	break;//end case 0
	default:cout<<"输入选项错误,请重新输入!\n\n";
	}  //end switch

	}//end while

}


/**************************************************************/

void ProduceNewFile(fstream & f)    //写为新文件
{
	char ch,choice,s[FILENAME];
	int c;
	RecordFile RF;    //声明对象
	int n;            //存放记录号
	char k[KEYLENGTH];//存放记录标识
	char newFile[FILENAME];//新文件名
	long posEnd;      //记录末尾位置信息
	long track;       //用于跟踪指针位置
	f.seekp(0,ios::end);
	posEnd=f.tellp();	//得到末尾位置信息
	if(posEnd==0)
	{
		cerr<<"该记录文件为空,系统将自动返回至上一级菜单."<<endl;
		return;
	}
	CountNum(f);

	cout<<"| * * * * * * * * * * * * 重写一条记录 * * * * * * * * * * * |"<<endl;
	cout<<"| 请键入操作选择:                                            |"<<endl;
	cout<<"| 1.将若干记录写到一个新文件中                               |"<<endl;
	cout<<"| 2.将全部记录写到一个新文件中                               |"<<endl;
	cout<<"| 0.返回                                                     |"<<endl;
    cout<<"| * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  |"<<endl;
	cin>>choice;
	fstream existFile("existFile.dat",ios::binary|ios::in|ios::out);
	track=existFile.tellg();
	existFile.seekg(0,ios::end);
	posEnd=existFile.tellg();
	bool flag=1;
	while(flag)
	{
		cout<<"请为新文件命名:(请加后缀.dat)[如想返回可按0]"<<endl;
		cin>>newFile;
		if(strcmp(newFile,"0")==0) return;
		existFile.seekg(0);
		while(track!=posEnd&&strcmp(newFile,s))
		{
			existFile.read(s,FILENAME-1);
			track=existFile.tellg();
		}
		if(strcmp(s,newFile)==0)
			cout<<newFile<<"文件已经存在,请重新命名!"<<endl;
		else{
			existFile.seekp(0,ios::end);
			existFile.write(newFile,FILENAME-1);
			flag=0;
		}
	}
	fstream file(newFile,ios::out|ios::binary);
	posEnd=f.tellp();	//得到末尾位置信息
	switch(choice)
	{
	case '1'://1.将若干记录写到一个新文件中
		do{
			cout<<"请选择加入记录的方式(按记录号为1,按记录标识为2):"<<endl;
			cin>>c;
			if(c!=1&&c!=2)
				cout<<"输入选项错误,请重新输入!"<<endl;
		}while(c!=1&&c!=2);
		if(c==1){
			while(1)
			{
				cout<<"请输入被加入记录的记录号:"<<endl;
				cin>>n;
				cout<<"请稍等..."<<endl;
				f.seekp(0);
				do{
					RF.ReleaseSpace();
					RF.SequenceReadHead(f);
					RF.SequenceReadData(f);
					track=f.tellp();
				}while(RF.GetNum()!=n&&track!=posEnd);
				if(RF.GetNum()==n&&!RF.GetDeletionSign()){
					RF.SequenceWriteHead(file);
					RF.SequenceWriteData(file);
					cout<<"记录已被加入!"<<endl;
					cout<<"要继续加入记录吗(Y/N)?"<<endl;
					cin>>ch;
					if(ch=='y'||ch=='Y')	continue;
					else{
						file.close();
						cout<<"新文件"<<newFile<<"已经生成!"<<endl;
						return;
					}
				}//end if
				else
					cout<<"未找到该记录,请确定输入是否有误!"<<endl;
			}//end while
		}//end if
		else{
			while(1)
			{
				cout<<"请输入被加入记录的记录标识:"<<endl;
				cin>>k;
				cout<<"请稍等..."<<endl;
				f.seekp(0);
				do{
					RF.ReleaseSpace();
					RF.SequenceReadHead(f);
					RF.SequenceReadData(f);
					track=f.tellp();
				}while(strcmp(RF.GetKey(),k)!=0&&track!=posEnd);
				if(strcmp(RF.GetKey(),k)==0&&!RF.GetDeletionSign()){
					RF.SequenceWriteHead(file);
					RF.SequenceWriteData(file);
					cout<<"记录已被加入!"<<endl;
					cout<<"要继续加入记录吗(Y/N)?"<<endl;
					cin>>ch;
					if(ch=='y'||ch=='Y')	continue;
					else{
						file.close();
						cout<<"新文件"<<newFile<<"已经生成!"<<endl;
						return;
					}
				}//end if
				else
					cout<<"未找到该记录,请确定输入是否有误!"<<endl;
			}//end while
		}//end else
		break;
	case '2'://2.将全部记录写到一个新文件中
		f.seekp(0);
		do{
			RF.ReleaseSpace();
			RF.SequenceReadHead(f);
			RF.SequenceReadData(f);
			if(!RF.GetDeletionSign()){
				RF.SequenceWriteHead(file);
				RF.SequenceWriteData(file);
			}
			track=f.tellp();
		}while(track!=posEnd);
		cout<<"新文件"<<newFile<<"已经生成!"<<endl;
		file.close();
		break;
	case '0'://0.返回
		cout<<"你确实想返回吗?(Y/N)"<<endl;
		cin>>ch;
        if(ch=='y'||ch=='Y')	return;
		else	break;//end case 0
	}

}

void CountNum(fstream & f)
{
	int sum=0;        //记录总数
	long posEnd;      //记录末尾位置信息
	long length=0;    //记录总长度
	RecordFile RF;    //声明对象
	f.seekp(0,ios::end);
	posEnd=f.tellp();
	if(posEnd==0)
	{
		cerr<<"该记录文件为空,系统将自动返回至上一级菜单."<<endl;
		return;
	}
	f.seekp(0);
	long track;   //跟踪指针
	do{                                  //查找与记录标识匹配的记录
		RF.SequenceReadHead(f);
		if(RF.GetDeletionSign()==0){
			sum++;
			length+=sizeof(RecordNode)+RF.GetLength();
		}
		f.seekp(RF.GetLength(),ios::cur);
		track=f.tellp();
	}while(track!=posEnd);
	cout<<"记录文件记录数为:"<<sum<<endl;
	cout<<"记录文件字节数为:"<<length<<endl;
	cout<<"记录文件中各记录分别为:"<<endl;
	cout<<"记录号"<<'\t'<<"记录标识"<<'\t'<<"记录标签";
	cout<<'\t'<<"记录长度"<<endl;
	f.seekp(0);
	do{
		RF.SequenceReadHead(f);
		if(RF.GetDeletionSign()==0){
			cout<<RF.GetNum()<<'\t'<<RF.GetKey()<<'\t';
			if(RF.GetTag()==0)cout<<"链接"<<'\t';
			else cout<<"多媒体文件"<<'\t';
			cout<<RF.GetLength()<<endl;
		}
		f.seekp(RF.GetLength(),ios::cur);
		track=f.tellp();
	}while(track!=posEnd);
}

void Browse(fstream & f)          //浏览记录文件内容
{
	int c;
	RecordFile RF;    //声明对象
	int n;            //存放记录号
	char k[KEYLENGTH];//存放记录标识
	long posEnd;      //记录末尾位置信息
	long track;       //用于跟踪指针位置
	f.seekp(0,ios::end);
	posEnd=f.tellp();	//得到末尾位置信息
	if(posEnd==0)
	{
		cerr<<"该记录文件为空,系统将自动返回至上一级菜单."<<endl;
		return;
	}
	CountNum(f);
	
	cout<<"| * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *|"<<endl;
	cout<<"| * * * * * * * * * * *浏览记录文件内容 * * * * * * * * * * *|"<<endl;
	cout<<"| * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *|"<<endl;
	
	do{
		cout<<"请选择浏览记录文件的方式(按记录号为1,按记录标识为2):"<<endl;
		cin>>c;
		if(c!=1&&c!=2)
		cout<<"输入选项错误,请重新输入!"<<endl;
	}while(c!=1&&c!=2);
	if(c==1){
		cout<<"请输入记录号:"<<endl;
		cin>>n;
		cout<<"请稍等..."<<endl;
		f.seekp(0);
		do{
			RF.ReleaseSpace();
			RF.SequenceReadHead(f);
			RF.SequenceReadData(f);
			track=f.tellp();
		}while(RF.GetNum()!=n&&track!=posEnd);
		if(RF.GetNum()==n&&!RF.GetDeletionSign()){
			if(RF.GetTag()==1){
				DataNode * head;
				head=RF.GetData();
				system(head->str);
			}
			else{
				fstream file(RF.GetKey(),ios::out|ios::binary);
				RF.SequenceWriteData(file);
				file.close();
				system(RF.GetKey());
				remove(RF.GetKey());
			}
			
		}
		else
			cout<<"未找到该记录,请确定输入是否有误!"<<endl;
	}
	else{
		cout<<"请输入记录标识:"<<endl;
		cin>>k;
		cout<<"请稍等..."<<endl;
		f.seekp(0);
		do{
			RF.ReleaseSpace();
			RF.SequenceReadHead(f);
			RF.SequenceReadData(f);
			track=f.tellp();
		}while(strcmp(RF.GetKey(),k)!=0&&track!=posEnd);
		if(strcmp(RF.GetKey(),k)==0&&!RF.GetDeletionSign()){
			if(RF.GetTag()==1){
				DataNode * head;
				head=RF.GetData();
				system(head->str);
			}
			else{
				fstream file(RF.GetKey(),ios::out|ios::binary);
				RF.SequenceWriteData(file);
				file.close();
				system(RF.GetKey());
				remove(RF.GetKey());
			}
			
		}
		else
			cout<<"未找到该记录,请确定输入是否有误!"<<endl;
	}
}

/**************************************************************/

//输入子文件详细资料
void InputDetails(fstream & f,RecordFile & RF,char * fileName)
{
	long len;					//存放记录多媒体文件流中的末尾位置
    char k[KEYLENGTH];		    //存放记录标识
	NumNode NN;					//存放记录文件名和记录数
	int t;						//存放记录标签
	char path[PATHLENGTH];      //存放文件路径
	DataNode * head;            //存放多媒体文件字节流
	bool flag=1;                //设置循环条件
	RF.ReleaseSpace();          //如果数据结点不为空,释放空间
	long posCur=f.tellp();      //保存当前记录文件指针位置
	f.seekp(0,ios::end);        //移动指针到末尾
	long posEnd=f.tellp();      //得到末尾指针位置 
	long track;                 //得到即时指针位置
	while(flag)         //循环用于保证记录标识输入没有重复
	{
		cout<<"请输入记录标识(少于50个字符):"<<endl;
		cin>>k;
		f.seekp(0);           //指针定位于开始
		if(posEnd!=0)
		{
			do{                //查找与记录标识匹配的记录
				RF.SequenceReadHead(f);
				f.seekp(RF.GetLength(),ios::cur);
				track=f.tellp();
			}while(strcmp(RF.GetKey(),k)!=0&&track!=posEnd);
			if(strcmp(RF.GetKey(),k)==0&&!RF.GetDeletionSign())
				cout<<"已经有同样的记录标识,请重新输入!"<<endl;
			else flag=0;  //跳出循环
		}
		else flag=0;      //跳出循环
	}
	f.seekp(posCur,ios::beg);    //恢复现场
	
	fstream index("index.dat",ios::out|ios::in|ios::binary);
	track=index.tellg();
	index.seekg(0,ios::end);
	posEnd=index.tellg();
	index.seekg(0);
	if(posEnd==0)
	{
		NN.num=1;
		strcpy(NN.name,fileName);
		index.write((char*)&NN,sizeof(NN));
	}
	else
	{
		while(track!=posEnd&&strcmp(fileName,NN.name)!=0)
		{
			index.read((char*)&NN,sizeof(NN));
			track=index.tellg();
		}
		if(strcmp(fileName,NN.name)!=0)
		{
			NN.num=1;
			strcpy(NN.name,fileName);
			index.write((char*)&NN,sizeof(NN));
		}
		else
		{
			NN.num++;
			index.seekg(-sizeof(NN),ios::cur);
			index.write((char*)&NN,sizeof(NN));
		}
	}
	
	index.close();

	do{
		cout<<"请确定多媒体文件的存储方式(提供链接为1,直接存储为2):"<<endl;
		cin>>t;
		if(t!=1&&t!=2)
			cout<<"输入选项有误,请重新输入!"<<endl;
	}while(t!=1&&t!=2);
	
	if(t==1){
		RF.SetNum(NN.num);
		RF.SetTag(t);
		RF.SetKey(k);
		RF.SetDeletionSign(0);
		cout<<"请输入多媒体文件的链接地址(长度须小于100个字符):"<<endl;
		head=new DataNode;
		cin>>head->str;
		head->next=NULL;
		RF.SetLength(DATANODELENGTH-1);
		RF.SetData(head);
	}//end if
	else{
		fstream multiMedia;
		cout<<"请输入要直接存储的多媒体文件路径:"<<endl;
		cin>>path;
		multiMedia.open(path,ios::binary|ios::in); //打开多媒体文件
		if(!multiMedia){
			cerr<<"文件不能打开,请重新输入文件名!"<<endl;
			return;
		}
		cout<<"请稍等..."<<endl;
		RF.SetNum(NN.num);
		RF.SetKey(k);
		RF.SetTag(t);
		RF.SetDeletionSign(0);
		multiMedia.seekg(0,ios::end);   //读指针移到文件末尾
		len=multiMedia.tellg();         //记录多媒体文件长度
		if(!len)
		{
			cout<<"此多媒体文件为空!"<<endl;
			return;
		}
		multiMedia.seekg(0,ios::beg);   //读指针移到文件头
		DataNode * s, * p;
		head=NULL;
		long n,sum=0;
		s=new DataNode;
		/*下面步骤将多媒体数据用链表连接起来*/
		while(sum<len)
		{
			if(len-sum<DATANODELENGTH-1)
				n=len-sum;
			else
				n=DATANODELENGTH-1;
			multiMedia.read(s->str,n);
			s->str[n]='\0';
			if(head==NULL) head=s;
			p=s;
			s=new DataNode;
			p->next=s;
			sum+=DATANODELENGTH-1;
		}
		p->next=NULL;
		delete s;//结束

		RF.SetLength(sum);          //设置数据部分长度
		RF.SetData(head);           //设置数据结点头指针
		multiMedia.close();         //关闭多媒体文件
	}//end else
}

⌨️ 快捷键说明

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