操作系统课程设计:Nachos的文件管理模块升级
发布网友
发布时间:2022-05-14 05:30
我来回答
共1个回答
热心网友
时间:2024-02-24 12:15
这是我们做的、基本上满足你的要求
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
struct File
{
string name;
bool isDir;
int pos;
int len;
File* pPre;
File* pNxt;
File* pChd;
File* pPar;
File(){name = "";isDir = false;pos = len = 0;pChd = pPar = pPre = pNxt = NULL;}
};
class DirSystem
{
private:
File* pRoot;
public:
DirSystem(){pRoot = new File;pRoot->pChd = new File;pRoot->pPar = pRoot;}
File* Append(File*& tail,const File& f)
{
tail->pNxt = new File;
tail->pNxt->isDir = f.isDir;
tail->pNxt->len = f.len;
tail->pNxt->pos = f.pos;
tail->pNxt->name = f.name;
tail->pNxt->pChd = tail->pNxt->pNxt = tail->pNxt->pPar = NULL;
tail->pNxt->pPre = tail;
return tail->pNxt;
}
void Md(File*& pCur,const File& f)
{
if(pCur->pChd == NULL)
{
pCur->pChd = new File;
pCur->pChd->pPar = pCur;
Append(pCur->pChd,f);
}
else
{
File* tmp = pCur->pChd;
while(tmp->pNxt != NULL)
tmp = tmp->pNxt;
Append(tmp,f);
}
}
void Show(File* pF)
{
if(pF == NULL)
return;
File* cur = pF->pNxt;
while(cur != NULL)
{
cout << cur->name;
if(cur->isDir)
cout << "(文件夹) " << endl;
else
cout << "(文件) " << endl;
cur = cur->pNxt;
}
}
void Rd(File* pF)
{
if(pF == NULL)
cout << "文件不存在!" << endl;
else if(pF->pChd != NULL)
cout << "该文件夹中还有其它文件,拒绝删除!" << endl;
else
{
pF->pPre->pNxt = pF->pNxt;
if(pF->pNxt != NULL)
pF->pNxt->pPre = pF->pPre;
delete pF;
}
}
void Init()
{
int num;
cout << "输入分区数:" << endl;
cin >> num;
int i,pos;
File f,*t;
pos = 0;
t = pRoot->pChd;
f.isDir = true;
f.pChd = f.pNxt = f.pPar = f.pPre = NULL;
char ch = 'C';
for(i = 0;i < num;++i)
{
cout << "输入分区容量:" << endl;
cin >> f.len;
f.pos = pos;
pos += f.len;
f.name = ch++ + string(":");
t = Append(t,f);
}
}
void Run()
{
File* pCur;
pCur = pRoot->pChd->pNxt;
string hint,cmd,tmp;
hint = pCur->name + "\\>";
while(true)
{
cout << hint;
cin >> cmd;
cmd = Uniform(cmd);
if(cmd == "DIR")
{
cout << pCur->name << endl;
Show(pCur->pChd);
}
else if(cmd == "CD")
{
cin >> cmd;
cmd = Uniform(cmd);
File* ftmp = pCur;
string htmp = hint;
hint = "";
pCur = pRoot;
bool find = false;
vector<string> nm = Parse(cmd);
for(int i = 0;i < nm.size();++i)
{
find = false;
pCur = pCur->pChd->pNxt;
while(pCur != NULL)
{
if(pCur->name == nm[i])
{
hint += nm[i] + '\\';
find = true;
break;
}
pCur = pCur->pNxt;
}
if(!find)
break;
}
if(!find)
{
pCur = ftmp;
hint = htmp;
cout << "没有找到要转向的路径!" << endl;
}
else
{
if(nm.size() > 1)
hint.erase(hint.end()-1);
hint += ">";
}
}
else if(cmd == "MD")
{
cin >> cmd;
File f;
f.isDir = true;
f.pos = 0;
f.len = 100;
f.name = cmd;
f.pChd = f.pPar = f.pNxt = f.pPre = NULL;
Md(pCur,f);
}
else if(cmd == "MF")
{
cin >> cmd;
File f;
f.isDir = false;
f.pos = 0;
f.len = 100;
f.name = cmd;
f.pChd = f.pPar = f.pNxt = f.pPre = NULL;
Md(pCur,f);
}
else if(cmd == "RD")
{
cin >> cmd;
File* tar = pCur->pChd;
while(tar)
{
if(tar->name != cmd)
tar = tar->pNxt;
else
break;
}
Rd(tar);
}
else
{
cout << "您输入的命令本系统不识别!" << endl;
}
}
}
private:
vector<string> Parse(string tar)
{
vector<string> res;
int beg,end;
beg = 0;
end = tar.find('\\',beg);
while(true)
{
res.push_back(tar.substr(beg,end - beg));
if(end == -1)
break;
beg = end + 1;
end = tar.find('\\',beg);
}
return res;
}
string Uniform(string cmd)
{
string res = cmd;
int offset = 'A' - 'a';
for(int i = 0;i < cmd.size();++i)
{
if(cmd[i] >= 'a' && cmd[i] <= 'z')
res[i] = cmd[i] + offset;
}
return res;
}
};
int main()
{
DirSystem ds;
ds.Init();
ds.Run();
return 0;
}