📄 sq_index_file.cpp
字号:
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>
using namespace std;
struct sq_file{//连续文件的数据结构
int f_id;//文件id
int length;//文件长度
int start_Address;//文件起始地址
};
/*struct index_table{
int log_address;
int phs_address;
};*/
struct index_file{//索引文件的数据结构
int f_id;//文件id
int length;//文件长度
// index_table *index_point;
// int log_address;
int phs_address;//这是一个临时变量,存放当前逻辑地址对应的物理地址
};
int storage,number; //全局变量,storage表示存储空间大小,number表示文件个数
void input(){
cout<<"Please input the storage:";//输入给定的存储空间大小
cin>>storage;
cout<<"Please input the number of files:";//输入文件的个数
cin>>number;
}
void sqFile_stor(){//模拟连续文件的存储过程
input();
int i,total_length=0,count;
sq_file *file_array=new sq_file[number];//---------动态创建数组
cout<<"Please input the id and length of every file:"<<endl;
cout<<"f_id"<<" f_length"<<endl;
for(i=0;i<number;i++)//依次输入文件的id和长度
cin>>file_array[i].f_id>>file_array[i].length;
if(file_array[0].length>storage){//若第一个文件长度就大于存储空间,提示"空间不足",并返回
cout<<"The space is short!"<<endl;
return;
}
else{//否则
count=1;//count标识实际能存储的文件个数.至少第一个可以存储
file_array[0].start_Address=0;
total_length=file_array[0].length+file_array[1].length;
for(i=1;i<number&&total_length<=storage;i++){//计算依次输入的文件的起始地址
file_array[i].start_Address=file_array[i-1].start_Address+file_array[i-1].length;
if(i+1<number)//保证第i+1个文件的长度存在
total_length+=file_array[i+1].length;
count++;
}
for(i=0;i<count;i++){//依次输出文件占用空间的情况
cout<<"file "<<file_array[i].f_id<<": ";
for(int j=0;j<file_array[i].length;j++)
cout<<file_array[i].start_Address+j<<" ";
cout<<endl;
}
if(count<number)//若给count个文件分配空间后,再给第count+1个文件分配空间时失败,则提示"空间不足",并返回
cout<<"ERROR:file "<<i+1<<":The space is short!"<<endl;
}
}
int rand_array[100];//存放已占用了的物理地址
int k=0;
bool In_Array(int t){//判断t是否在rand_array[100]中
for(int i=0;i<k;i++){
if(t==rand_array[i])//若在,则返回true
return true;
}
return false;//若不在,则返回false
}
void indexFile_stor(){//模拟索引文件的存储过程
input();
int i,total_length=0;
index_file *file_array=new index_file[number];//---------动态创建数组
cout<<"Please input the id and length of every file:"<<endl;
cout<<"f_id"<<" f_length"<<endl;
for(i=0;i<number;i++)//依次输入文件的id和长度
cin>>file_array[i].f_id>>file_array[i].length;
if(file_array[0].length>storage){//若第一个文件长度就大于存储空间,提示"空间不足",并返回
cout<<"The space is short!"<<endl;
return;
}
else{
total_length=file_array[0].length;
for(i=0;i<number&&total_length<=storage;i++){//依次输出文件占用空间的情况
cout<<"file"<<file_array[i].f_id<<": log_address "<<" phs_address"<<endl;
for(int j=0;j<file_array[i].length;j++){//为每个文件的逻辑块分配随机物理地址
srand((unsigned)time(NULL));
int temp=rand()%storage;
while(In_Array(temp))//temp在rand_array[100]中,则重新分配物理地址,直到无地址冲突为止
temp=rand()%storage;
rand_array[k++]=temp;
file_array[i].phs_address=temp;
cout<<setw(9)<<j<<setw(13)<<file_array[i].phs_address<<endl;
}
if(i+1<number)//保证第i+1个文件的长度存在
total_length+=file_array[i+1].length;
}
if(total_length>storage)
cout<<"ERROR:file "<<i+1<<":The space is short!"<<endl;
}
}
int main(){
int choice;
do{
cout<<"选择:"<<endl;
cout<<"0.退出."<<endl;
cout<<"1.模拟连续文件的存储过程."<<endl;
cout<<"2.模拟索引文件的存储过程."<<endl;
cin>>choice;
switch(choice){
case 0:return 0;
case 1: sqFile_stor();break;//模拟连续文件的存储过程
case 2: indexFile_stor();break;//模拟索引文件的存储过程
default:cout<<"Wrong choice!"<<endl;//错误的输入
}
k=0;
}while(choice!=0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -