操作系统课设
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<windows.h>
#include<algorithm>
using namespace std;
#define mod 25
#define N 50 //每组块数
int M=8; //组数
int suo=0;
struct snode{
int x;
};
struct node{
int num; //大小
struct snode *data[N];
struct node *next;
}*head;
struct filenode{
int n; //文件占盘块数
vector<struct snode*>data; //文件内容
};
map<string,filenode>q;
node superBlock; //超级块
int numblock=N*(M+1) ; //剩余磁盘块数
void init(); //初始化超级块
void ui(); //用户界面
void allocate(); //分配超级块
void showblock();
void showfile();
void realease();
void winnnnn();
int main()
{
init();
ui();
}
void winnnnn()
{
cout<<"***************************************************************************************************"<<endl<<endl;
}
void init()
{
int num=N*(M+1)-1;
node *first;
snode *y;
//********************************初始化尾块
head=(node*)malloc(sizeof(node));
head->next=NULL;
head->num=N;
for(int i=0;i<N;i++)
{
y=(snode*)malloc(sizeof(snode));
y->x=num--;
head->data[i]=y;
}
//********************************初始化其他块
first=head;
node *x;
for(int i=0;i<M-1;i++)
{
x=(node*)malloc(sizeof(node));
x->next=first;
x->num=N;
for(int i=0;i<N;i++)
{
y=(snode*)malloc(sizeof(snode));
y->x=num--;
x->data[i]=y;
}
first=x;
}
superBlock.next=x;
superBlock.num=N;
for(int i=0;i<N;i++)
{
y=(snode*)malloc(sizeof(snode));
y->x=num--;
superBlock.data[i]=y;
}
//
// for(int i=N-1;i>=0;i--)
// {
// cout<<superBlock.data[i]<<" ";
// }
}
void ui()
{
string order;
while(1)
{
winnnnn();
cout<<"1.空间块分配 2.空间块释放 3.查看文件 4.查看空闲块 5.退出"<<endl<<endl;
winnnnn();
cin>>order;
// system("cls");
if(order.size()>1){
cout<<"输入格式错误请重试"<<endl;
continue;
}
int c=order[0]-'0';
switch(c)
{
case 1:
allocate();
break;
case 2:
realease();
break;
case 3:
showfile();
break;
case 4:
showblock();
break;
case 5:
cout<<"程序即将退出"<<endl;
Sleep(200);
return ;
default :
break;
}
}
}
void showblock()
{
winnnnn();
cout<<"现剩余空闲盘块号数:"<<numblock<<endl;
cout<<"超级块数据块: "<<"剩余块号数"<<superBlock.num<<endl;
//************************输出超级块
int rendl=1;
for(int i=superBlock.num-1;i>=0;i--)
{
//cout<<superBlock.data[i]<<" ";
printf("%3d ",superBlock.data[i]->x);
if(rendl%mod==0) cout<<endl;
rendl++;
}
cout<<endl;
//************************输出盘块号
node *x,*first=&superBlock;
for(int i=1;i<=M;i++)
{
if(first->next==NULL){
cout<<"这里出错了"<<endl;
break;
}
rendl=1;
x=first->next;
winnnnn();
cout<<"第"<<i<<"组盘块: "<<"剩余块号数"<<x->num<<endl;
for(int j=N-1;j>=0;j--)
{
// cout<<x->data[j]<<" ";
printf("%3d ",x->data[j]->x);
if(rendl%mod==0) cout<<endl;
rendl++;
}
cout<<endl;
first=x;
}
}
void allocate()
{
while(suo==1){
Sleep(10);
}
suo=1;
string name;
int numb;
winnnnn();
cout<<"请输入申请文件名和申请磁盘块号数: 剩余盘块号数"<<numblock<<endl;
cin>>name>>numb;
if(numb<=0){
cout<<"申请磁盘块号数量输入错误请重试"<<endl;
suo=0;
return ;
}
if(numb>numblock){ //当剩余盘块数不足时
cout<<"现剩余盘块号数"<<numblock<<"个,盘块号数不足请重试"<<endl;
suo=0;
return ;
}
if(q.count(name)!=0){
cout<<"文件名已存在,请重试"<<endl;
return ;
}
q[name].n=numb;
for(int i=0;i<numb;i++)
{
if(superBlock.num>0){
q[name].data.push_back(superBlock.data[--superBlock.num]);
}else{
M--;
node *x;
x=superBlock.next;
superBlock = *x;
free(x);
q[name].data.push_back(superBlock.data[--superBlock.num]);
}
}
numblock-=numb;
cout<<"申请成功"<<endl;
suo=0;
}
void showfile()
{
winnnnn();
cout<<"文件名如下:"<<endl;
// for(auto it=q.begin();it!=q.end();it++)
// {
// cout<<it->first<<" ";
// for(int i=0;i<it->second.data.size();i++)
// {
// cout<<it->second.data[i]<<" ";
// }
// cout<<endl;
// }
int rendl=1;
for(auto[key,val]:q)
{
cout<<key<<" 占盘块数"<<val.data.size()<<endl;
rendl =1;
for(int i=0;i<val.data.size();i++)
{
//cout<<val.data[i]<<" ";
printf("%4d",val.data[i]->x);
if(rendl%mod==0) cout<<endl;
rendl++;
}
cout<<endl;
}
}
void realease()
{
while(suo==1)
{
Sleep(10);
}
suo=1;
showfile();
string name;
winnnnn();
cout<<"请输入要释放的文件名"<<endl;
cin>>name;
if(name=="exit")
{
suo=0;
return ;
}
if(q.count(name)==0) {
cout<<"不存在该文件,请重试"<<endl;
suo=0;
return ;
}
//cout<<q[name].data.size()<<endl;
int filenum=q[name].data.size();
for(int i=0;i<filenum;i++)
{
if(superBlock.num!=50){
superBlock.data[superBlock.num++]=q[name].data.back();
q[name].data.pop_back();
}else{
node *x,*y;
x=(node*)malloc(sizeof(node));
y=(node*)malloc(sizeof(node));
*x=superBlock;
y->next=x;
superBlock=*y;
superBlock.num=0;
superBlock.data[superBlock.num++]=q[name].data.back();
q[name].data.pop_back();
M++;
}
}
numblock+=filenum;
q.erase(name);
cout<<"删除成功"<<endl;
suo=0;
}
注意:devc++报错 codeblock能运行