?? sq_index_file.cpp
字號:
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>
using namespace std;
struct sq_file{//連續(xù)文件的數(shù)據(jù)結(jié)構(gòu)
int f_id;//文件id
int length;//文件長度
int start_Address;//文件起始地址
};
/*struct index_table{
int log_address;
int phs_address;
};*/
struct index_file{//索引文件的數(shù)據(jù)結(jié)構(gòu)
int f_id;//文件id
int length;//文件長度
// index_table *index_point;
// int log_address;
int phs_address;//這是一個臨時變量,存放當(dāng)前邏輯地址對應(yīng)的物理地址
};
int storage,number; //全局變量,storage表示存儲空間大小,number表示文件個數(shù)
void input(){
cout<<"Please input the storage:";//輸入給定的存儲空間大小
cin>>storage;
cout<<"Please input the number of files:";//輸入文件的個數(shù)
cin>>number;
}
void sqFile_stor(){//模擬連續(xù)文件的存儲過程
input();
int i,total_length=0,count;
sq_file *file_array=new sq_file[number];//---------動態(tài)創(chuàng)建數(shù)組
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標(biāo)識實際能存儲的文件個數(shù).至少第一個可以存儲
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];//---------動態(tài)創(chuàng)建數(shù)組
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++){//為每個文件的邏輯塊分配隨機(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.模擬連續(xù)文件的存儲過程."<<endl;
cout<<"2.模擬索引文件的存儲過程."<<endl;
cin>>choice;
switch(choice){
case 0:return 0;
case 1: sqFile_stor();break;//模擬連續(xù)文件的存儲過程
case 2: indexFile_stor();break;//模擬索引文件的存儲過程
default:cout<<"Wrong choice!"<<endl;//錯誤的輸入
}
k=0;
}while(choice!=0);
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -