#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <conio.h>
#include <algorithm>
#include <limits>
#include <sstream>
#include <unordered_map>
// 定义文件类型常量
const int folder = 1, cmd = 2, txt = 3;
// 全局计数器和当前文件/目录指针
int NodeTop = 2;
int FoldTop = 2;
int TxtTop = 1;
int CmdTop = 1;
// 文件/目录节点结构
struct node {
std::string nam;
int fa;
int typ;
int g;
} fil[200001];
// 文件夹结构
struct Folder {
std::vector<int> so;
} fol[50001];
// 命令文件结构
struct Cmd {
std::string s;
} cm[50001];
// 文本文件结构
struct Txt {
std::string s;
} tx[50001];
// 函数声明
void NewFold(int u, const std::string& na);
void NewCmd(int u, const std::string& na);
void NewTxt(int u, const std::string& na);
int inFile(int u);
void init();
void clearConsole();
void displayPath(int u);
void displayItems(int currentDirId, int selectedItemIndex, const std::vector<int>& children);
std::string getInput(const std::string& prompt);
bool isNameExists(int parentDirId, const std::string& name, int type);
void editTextFile(int txtNodeId);
void editCmdFile(int cmdNodeId);
void deleteItem(int currentDirId, int selectedItemIndex, const std::vector<int>& children);
void deleteFolderRecursive(int folderNodeId);
bool executeCmdFile(int cmdNodeId, int currentDirId); // 执行命令文件
int findItemByName(int parentDirId, const std::string& name); // 按名称查找项目
bool copyItem(int srcNodeId, int destDirId); // 复制项目
std::string resolveVariables(const std::string& input, const std::unordered_map<std::string, std::string>& vars);
// 检查当前目录下是否存在同名同类型的节点
bool isNameExists(int parentDirId, const std::string& name, int type) {
std::vector<int>& children = fol[fil[parentDirId].g].so;
for (int childId : children) {
if (fil[childId].nam == name && fil[childId].typ == type) {
return true;
}
}
return false;
}
// 创建新文件夹
void NewFold(int u, const std::string& na) {
fil[NodeTop].fa = u;
fil[NodeTop].typ = folder;
fil[NodeTop].g = FoldTop;
fil[NodeTop].nam = na;
fol[FoldTop].so.clear();
NodeTop++;
FoldTop++;
fol[fil[u].g].so.push_back(NodeTop - 1);
}
// 创建新命令文件
void NewCmd(int u, const std::string& na) {
fil[NodeTop].fa = u;
fil[NodeTop].typ = cmd;
fil[NodeTop].g = CmdTop;
fil[NodeTop].nam = na;
cm[CmdTop].s = "// 简易命令文件\n// 支持的命令:\n";
cm[CmdTop].s += "// create [file|folder|cmd] 名称\n";
cm[CmdTop].s += "// copy 源文件 目标目录\n";
cm[CmdTop].s += "// edit 文件名 内容\n";
cm[CmdTop].s += "// delete 文件名\n";
cm[CmdTop].s += "// set 变量名=值 (定义变量)\n";
cm[CmdTop].s += "// for 变量名 in (值1 值2 ...) { 命令 }\n";
NodeTop++;
CmdTop++;
fol[fil[u].g].so.push_back(NodeTop - 1);
}
// 创建新文本文件
void NewTxt(int u, const std::string& na) {
fil[NodeTop].fa = u;
fil[NodeTop].typ = txt;
fil[NodeTop].g = TxtTop;
fil[NodeTop].nam = na;
tx[TxtTop].s = "这是一个文本文档,可以编辑内容。\n";
NodeTop++;
TxtTop++;
fol[fil[u].g].so.push_back(NodeTop - 1);
}
// 清屏函数
void clearConsole() {
system("cls");
}
// 显示当前路径
void displayPath(int u) {
std::vector<std::string> pathParts;
while (u != 1) {
pathParts.push_back(fil[u].nam);
u = fil[u].fa;
}
pathParts.push_back(fil[1].nam);
std::cout << "当前路径: ";
for (auto it = pathParts.rbegin(); it != pathParts.rend(); ++it) {
std::cout << *it;
if (std::next(it) != pathParts.rend()) {
std::cout << " > ";
}
}
std::cout << std::endl << "----------------------------------------" << std::endl;
}
// 获取用户输入的字符串
std::string getInput(const std::string& prompt) {
std::string input;
std::cout << prompt;
std::cin >> input;
return input;
}
// 编辑文本文件内容
void editTextFile(int txtNodeId) {
clearConsole();
std::cout << "--- 编辑文本文件: " << fil[txtNodeId].nam << " ---" << std::endl;
std::cout << "当前内容:" << std::endl;
std::cout << tx[fil[txtNodeId].g].s << std::endl;
std::cout << "\n请输入新内容 (输入 . 结束编辑):" << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::string newContent;
std::string line;
while (true) {
std::getline(std::cin, line);
if (line == ".") {
break;
}
newContent += line + "\n";
}
if (!newContent.empty()) {
tx[fil[txtNodeId].g].s = newContent;
std::cout << "\n文件已保存!";
} else {
std::cout << "\n未修改文件内容。";
}
std::cout << "\n按任意键返回...";
_getch();
}
// 编辑命令文件内容
void editCmdFile(int cmdNodeId) {
clearConsole();
std::cout << "--- 编辑命令文件: " << fil[cmdNodeId].nam << " ---" << std::endl;
std::cout << "当前内容:" << std::endl;
std::cout << cm[fil[cmdNodeId].g].s << std::endl;
std::cout << "\n请输入新命令 (输入 . 结束编辑):" << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::string newContent;
std::string line;
while (true) {
std::getline(std::cin, line);
if (line == ".") {
break;
}
newContent += line + "\n";
}
if (!newContent.empty()) {
cm[fil[cmdNodeId].g].s = newContent;
std::cout << "\n命令文件已保存!";
} else {
std::cout << "\n未修改命令内容。";
}
std::cout << "\n按任意键返回...";
_getch();
}
// 封装显示逻辑
void displayItems(int currentDirId, int selectedItemIndex, const std::vector<int>& children) {
clearConsole();
displayPath(currentDirId);
std::cout << (selectedItemIndex == 0 ? "#< " : " < ") << "..(上级目录)" << std::endl;
for (size_t i = 0; i < children.size(); ++i) {
int childNodeId = children[i];
std::cout << (selectedItemIndex == static_cast<int>(i) + 1 ? "#< " : " < ")
<< fil[childNodeId].nam;
if (fil[childNodeId].typ == folder) std::cout << " [目录]";
else if (fil[childNodeId].typ == cmd) std::cout << " [命令文件]";
else if (fil[childNodeId].typ == txt) std::cout << " [文本文档]";
std::cout << std::endl;
}
std::cout << (selectedItemIndex == static_cast<int>(children.size()) + 1 ? "#< " : " < ")
<< "[+] 新建文件夹" << std::endl;
std::cout << (selectedItemIndex == static_cast<int>(children.size()) + 2 ? "#< " : " < ")
<< "[+] 新建文本文档" << std::endl;
std::cout << (selectedItemIndex == static_cast<int>(children.size()) + 3 ? "#< " : " < ")
<< "[+] 新建命令文件" << std::endl;
std::cout << std::endl
<< "----------------------------------------" << std::endl;
std::cout << "操作: [↑↓]移动 | [O]打开/进入 | [E]编辑 | [R]运行命令文件 | [N]新建 | [D]删除" << std::endl;
}
// 递归删除文件夹及其内容
void deleteFolderRecursive(int folderNodeId) {
std::vector<int>& children = fol[fil[folderNodeId].g].so;
while (!children.empty()) {
int childId = children[0];
if (fil[childId].typ == folder) {
deleteFolderRecursive(childId);
}
children.erase(children.begin());
}
}
// 删除文件/文件夹函数
void deleteItem(int currentDirId, int selectedItemIndex, const std::vector<int>& children) {
if (selectedItemIndex < 1 || selectedItemIndex > static_cast<int>(children.size())) {
return;
}
int targetNodeId = children[selectedItemIndex - 1];
std::string itemName = fil[targetNodeId].nam;
clearConsole();
std::cout << "确认删除 "
<< (fil[targetNodeId].typ == folder ? "文件夹" :
(fil[targetNodeId].typ == cmd ? "命令文件" : "文本文档"))
<< " \"" << itemName << "\" 吗?(Y/N): ";
char confirm = _getch();
if (confirm != 'Y' && confirm != 'y') {
return;
}
if (fil[targetNodeId].typ == folder) {
deleteFolderRecursive(targetNodeId);
}
std::vector<int>& parentChildren = fol[fil[currentDirId].g].so;
auto it = std::find(parentChildren.begin(), parentChildren.end(), targetNodeId);
if (it != parentChildren.end()) {
parentChildren.erase(it);
}
clearConsole();
std::cout << (fil[targetNodeId].typ == folder ? "文件夹" :
(fil[targetNodeId].typ == cmd ? "命令文件" : "文本文档"))
<< " \"" << itemName << "\" 删除成功!" << std::endl;
std::cout << "按任意键返回...";
_getch();
}
// 按名称查找项目
int findItemByName(int parentDirId, const std::string& name) {
std::vector<int>& children = fol[fil[parentDirId].g].so;
for (int childId : children) {
if (fil[childId].nam == name) {
return childId;
}
}
return -1;
}
// 复制项目 - 修复版
bool copyItem(int srcNodeId, int destDirId) {
// 检查源和目标是否为同一节点或存在父子关系
if (srcNodeId == destDirId) {
return false;
}
// 检查目标是否是源的子节点
int temp = destDirId;
while (temp != 1 && temp != srcNodeId) {
temp = fil[temp].fa;
}
if (temp == srcNodeId) {
return false; // 不能将父目录复制到子目录
}
std::string srcName = fil[srcNodeId].nam;
int srcType = fil[srcNodeId].typ;
// 检查目标目录是否已有同名同类型项目
if (isNameExists(destDirId, srcName, srcType)) {
return false;
}
if (srcType == folder) {
// 复制文件夹
NewFold(destDirId, srcName);
int newFolderId = NodeTop - 1;
// 复制子项
std::vector<int>& srcChildren = fol[fil[srcNodeId].g].so;
for (int childId : srcChildren) {
if (!copyItem(childId, newFolderId)) {
// 复制子项失败时回滚
deleteFolderRecursive(newFolderId);
std::vector<int>& parentChildren = fol[fil[destDirId].g].so;
auto it = std::find(parentChildren.begin(), parentChildren.end(), newFolderId);
if (it != parentChildren.end()) {
parentChildren.erase(it);
}
return false;
}
}
} else if (srcType == txt) {
// 复制文本文件
NewTxt(destDirId, srcName);
int newTxtG = TxtTop - 1;
tx[newTxtG].s = tx[fil[srcNodeId].g].s;
} else if (srcType == cmd) {
// 复制命令文件
NewCmd(destDirId, srcName);
int newCmdG = CmdTop - 1;
cm[newCmdG].s = cm[fil[srcNodeId].g].s;
}
return true;
}
// 解析变量引用
std::string resolveVariables(const std::string& input, const std::unordered_map<std::string, std::string>& vars) {
std::string result;
size_t pos = 0;
size_t n = input.length();
while (pos < n) {
if (input[pos] == '%' && pos + 1 < n) {
size_t end = input.find('%', pos + 1);
if (end != std::string::npos) {
std::string varName = input.substr(pos + 1, end - pos - 1);
auto it = vars.find(varName);
if (it != vars.end()) {
result += it->second;
}
pos = end + 1;
continue;
}
}
result += input[pos];
pos++;
}
return result;
}
// 执行命令文件 - 增加变量和循环支持
bool executeCmdFile(int cmdNodeId, int currentDirId) {
clearConsole();
std::cout << "--- 执行命令文件: " << fil[cmdNodeId].nam << " ---" << std::endl;
std::cout << "输出:\n" << std::endl;
std::string content = cm[fil[cmdNodeId].g].s;
std::istringstream iss(content);
std::string line;
int lineNum = 0;
bool success = true;
std::unordered_map<std::string, std::string> variables;
int loopDepth = 0;
std::vector<std::pair<std::string, std::vector<std::string>>> loopStack; // 循环变量名和值列表
std::vector<std::string> loopBody; // 当前循环体
while (std::getline(iss, line)) {
lineNum++;
// 忽略注释和空行
if (line.empty() || line.substr(0, 2) == "//") {
continue;
}
// 处理循环体
if (loopDepth > 0) {
// 检查循环结束符
if (line.find("}") != std::string::npos) {
loopDepth--;
if (loopDepth == 0) {
// 执行循环
auto& loopData = loopStack.back();
loopStack.pop_back();
std::string varName = loopData.first;
auto& values = loopData.second;
std::cout << "执行循环: for " << varName << " in (";
for (size_t i = 0; i < values.size(); ++i) {
std::cout << values[i];
if (i < values.size() - 1) std::cout << " ";
}
std::cout << ")" << std::endl;
// 保存当前变量值(如果存在)
std::string originalValue;
bool varExisted = false;
if (variables.find(varName) != variables.end()) {
originalValue = variables[varName];
varExisted = true;
}
// 循环执行循环体
for (const std::string& value : values) {
variables[varName] = value;
std::cout << "循环迭代: " << varName << " = " << value << std::endl;
// 执行循环体内的命令
for (const std::string& loopLine : loopBody) {
std::istringstream lineIss(resolveVariables(loopLine, variables));
std::vector<std::string> tokens;
std::string token;
while (lineIss >> token) {
tokens.push_back(token);
}
if (tokens.empty()) continue;
// 处理循环体内的命令(复用之前的命令处理逻辑)
if (tokens[0] == "create") {
// 复用create命令处理逻辑
if (tokens.size() < 3) {
std::cout << "错误 (循环体内): create命令格式错误" << std::endl;
success = false;
continue;
}
std::string type = tokens[1];
std::string name = tokens[2];
if (type == "file") {
if (name.find(".txt") == std::string::npos) name += ".txt";
if (isNameExists(currentDirId, name, txt)) {
std::cout << "错误 (循环体内): 文件 " << name << " 已存在" << std::endl;
success = false;
} else {
NewTxt(currentDirId, name);
std::cout << "已创建文件: " << name << std::endl;
}
} else if (type == "folder") {
if (isNameExists(currentDirId, name, folder)) {
std::cout << "错误 (循环体内): 文件夹 " << name << " 已存在" << std::endl;
success = false;
} else {
NewFold(currentDirId, name);
std::cout << "已创建文件夹: " << name << std::endl;
}
} else if (type == "cmd") {
if (name.find(".cmd") == std::string::npos) name += ".cmd";
if (isNameExists(currentDirId, name, cmd)) {
std::cout << "错误 (循环体内): 命令文件 " << name << " 已存在" << std::endl;
success = false;
} else {
NewCmd(currentDirId, name);
std::cout << "已创建命令文件: " << name << std::endl;
}
}
}
else if (tokens[0] == "copy") {
// 复用copy命令处理逻辑
if (tokens.size() < 3) {
std::cout << "错误 (循环体内): copy命令格式错误" << std::endl;
success = false;
continue;
}
std::string srcName = tokens[1];
std::string destName = tokens[2];
int srcNodeId = findItemByName(currentDirId, srcName);
if (srcNodeId == -1) {
std::cout << "错误 (循环体内): 源文件 " << srcName << " 不存在" << std::endl;
success = false;
continue;
}
int destDirId = findItemByName(currentDirId, destName);
if (destDirId == -1 || fil[destDirId].typ != folder) {
std::cout << "错误 (循环体内): 目标目录 " << destName << " 不存在或不是文件夹" << std::endl;
success = false;
continue;
}
if (copyItem(srcNodeId, destDirId)) {
std::cout << "已复制 " << srcName << " 到 " << destName << std::endl;
} else {
std::cout << "错误 (循环体内): 复制失败" << std::endl;
success = false;
}
}
// 可以在这里添加对其他命令的支持
}
}
// 恢复变量原始值
if (varExisted) {
variables[varName] = originalValue;
} else {
variables.erase(varName);
}
loopBody.clear();
continue;
}
}
loopBody.push_back(line);
continue;
}
// 处理变量引用
std::string resolvedLine = resolveVariables(line, variables);
std::istringstream lineIss(resolvedLine);
std::vector<std::string> tokens;
std::string token;
while (lineIss >> token) {
tokens.push_back(token);
}
if (tokens.empty()) continue;
// 解析命令
if (tokens[0] == "create") {
if (tokens.size() < 3) {
std::cout << "错误 (行 " << lineNum << "): create命令格式错误,应为: create [类型] [名称]" << std::endl;
success = false;
continue;
}
std::string type = tokens[1];
std::string name = tokens[2];
if (type == "file") {
if (name.find(".txt") == std::string::npos) name += ".txt";
if (isNameExists(currentDirId, name, txt)) {
std::cout << "错误 (行 " << lineNum << "): 文件 " << name << " 已存在" << std::endl;
success = false;
} else {
NewTxt(currentDirId, name);
std::cout << "已创建文件: " << name << std::endl;
}
} else if (type == "folder") {
if (isNameExists(currentDirId, name, folder)) {
std::cout << "错误 (行 " << lineNum << "): 文件夹 " << name << " 已存在" << std::endl;
success = false;
} else {
NewFold(currentDirId, name);
std::cout << "已创建文件夹: " << name << std::endl;
}
} else if (type == "cmd") {
if (name.find(".cmd") == std::string::npos) name += ".cmd";
if (isNameExists(currentDirId, name, cmd)) {
std::cout << "错误 (行 " << lineNum << "): 命令文件 " << name << " 已存在" << std::endl;
success = false;
} else {
NewCmd(currentDirId, name);
std::cout << "已创建命令文件: " << name << std::endl;
}
} else {
std::cout << "错误 (行 " << lineNum << "): 不支持的创建类型: " << type << std::endl;
success = false;
}
}
else if (tokens[0] == "copy") {
if (tokens.size() < 3) {
std::cout << "错误 (行 " << lineNum << "): copy命令格式错误,应为: copy [源文件] [目标目录]" << std::endl;
success = false;
continue;
}
std::string srcName = tokens[1];
std::string destName = tokens[2];
int srcNodeId = findItemByName(currentDirId, srcName);
if (srcNodeId == -1) {
std::cout << "错误 (行 " << lineNum << "): 源文件 " << srcName << " 不存在" << std::endl;
success = false;
continue;
}
int destDirId = findItemByName(currentDirId, destName);
if (destDirId == -1 || fil[destDirId].typ != folder) {
std::cout << "错误 (行 " << lineNum << "): 目标目录 " << destName << " 不存在或不是文件夹" << std::endl;
success = false;
continue;
}
if (copyItem(srcNodeId, destDirId)) {
std::cout << "已复制 " << srcName << " 到 " << destName << std::endl;
} else {
std::cout << "错误 (行 " << lineNum << "): 复制失败,目标目录中已存在同名项目或存在父子目录关系" << std::endl;
success = false;
}
}
else if (tokens[0] == "edit") {
if (tokens.size() < 3) {
std::cout << "错误 (行 " << lineNum << "): edit命令格式错误,应为: edit [文件名] [内容]" << std::endl;
success = false;
continue;
}
std::string fileName = tokens[1];
std::string content;
for (size_t i = 2; i < tokens.size(); ++i) {
content += tokens[i] + " ";
}
int fileNodeId = findItemByName(currentDirId, fileName);
if (fileNodeId == -1) {
std::cout << "错误 (行 " << lineNum << "): 文件 " << fileName << " 不存在" << std::endl;
success = false;
continue;
}
if (fil[fileNodeId].typ != txt && fil[fileNodeId].typ != cmd) {
std::cout << "错误 (行 " << lineNum << "): " << fileName << " 不是可编辑文件" << std::endl;
success = false;
continue;
}
if (fil[fileNodeId].typ == txt) {
tx[fil[fileNodeId].g].s = content + "\n";
} else {
cm[fil[fileNodeId].g].s = content + "\n";
}
std::cout << "已编辑文件: " << fileName << std::endl;
}
else if (tokens[0] == "delete") {
if (tokens.size() < 2) {
std::cout << "错误 (行 " << lineNum << "): delete命令格式错误,应为: delete [文件名]" << std::endl;
success = false;
continue;
}
std::string fileName = tokens[1];
int fileNodeId = findItemByName(currentDirId, fileName);
if (fileNodeId == -1) {
std::cout << "错误 (行 " << lineNum << "): 文件 " << fileName << " 不存在" << std::endl;
success = false;
continue;
}
// 执行删除
if (fil[fileNodeId].typ == folder) {
deleteFolderRecursive(fileNodeId);
}
std::vector<int>& parentChildren = fol[fil[currentDirId].g].so;
auto it = std::find(parentChildren.begin(), parentChildren.end(), fileNodeId);
if (it != parentChildren.end()) {
parentChildren.erase(it);
}
std::cout << "已删除: " << fileName << std::endl;
}
else if (tokens[0] == "set") {
// 变量定义命令: set var=value
if (tokens.size() < 2) {
std::cout << "错误 (行 " << lineNum << "): set命令格式错误,应为: set 变量名=值" << std::endl;
success = false;
continue;
}
std::string varExpr = tokens[1];
size_t eqPos = varExpr.find('=');
if (eqPos == std::string::npos || eqPos == 0) {
std::cout << "错误 (行 " << lineNum << "): set命令格式错误,应为: set 变量名=值" << std::endl;
success = false;
continue;
}
std::string varName = varExpr.substr(0, eqPos);
std::string varValue = varExpr.substr(eqPos + 1);
variables[varName] = varValue;
std::cout << "已设置变量: " << varName << " = " << varValue << std::endl;
}
else if (tokens[0] == "for") {
// 循环命令: for var in (val1 val2 ...) { ... }
if (tokens.size() < 4 || tokens[2] != "in" || tokens[3].substr(0, 1) != "(") {
std::cout << "错误 (行 " << lineNum << "): for命令格式错误,应为: for 变量名 in (值1 值2 ...) { 命令 }" << std::endl;
success = false;
continue;
}
std::string varName = tokens[1];
std::vector<std::string> values;
size_t pos = 3;
// 提取循环值
std::string currentToken = tokens[pos];
if (currentToken.size() > 1) {
values.push_back(currentToken.substr(1)); // 移除开头的(
}
pos++;
while (pos < tokens.size() && tokens[pos] != ")") {
values.push_back(tokens[pos]);
pos++;
}
if (pos >= tokens.size() || tokens[pos] != ")") {
std::cout << "错误 (行 " << lineNum << "): for命令缺少闭合括号" << std::endl;
success = false;
continue;
}
pos++;
if (pos >= tokens.size() || tokens[pos] != "{") {
std::cout << "错误 (行 " << lineNum << "): for命令缺少{" << std::endl;
success = false;
continue;
}
loopDepth++;
loopStack.emplace_back(varName, values);
std::cout << "发现循环: for " << varName << " in (";
for (size_t i = 0; i < values.size(); ++i) {
std::cout << values[i];
if (i < values.size() - 1) std::cout << " ";
}
std::cout << ")" << std::endl;
}
else {
std::cout << "错误 (行 " << lineNum << "): 未知命令: " << tokens[0] << std::endl;
success = false;
}
}
std::cout << "\n--- 执行完成 " << (success ? "(成功)" : "(有错误)") << " ---" << std::endl;
std::cout << "按任意键返回...";
_getch();
return success;
}
// 在文件/目录中导航
int inFile(int currentDirId) {
int selectedItemIndex = 0;
std::vector<int>& children = fol[fil[currentDirId].g].so;
while (true) {
int totalItems = 1 + children.size() + 3;
displayItems(currentDirId, selectedItemIndex, children);
char key = _getch();
switch (key) {
case 72: // 上箭头
selectedItemIndex = (selectedItemIndex - 1 + totalItems) % totalItems;
break;
case 80: // 下箭头
selectedItemIndex = (selectedItemIndex + 1) % totalItems;
break;
case 'o':
case 'O': {
if (selectedItemIndex == 0) {
return fil[currentDirId].fa;
} else if (selectedItemIndex >= 1 && selectedItemIndex <= static_cast<int>(children.size())) {
int targetNodeId = children[selectedItemIndex - 1];
if (fil[targetNodeId].typ == folder) {
return targetNodeId;
} else {
clearConsole();
std::cout << "--- " << fil[targetNodeId].nam << " ---" << std::endl;
if (fil[targetNodeId].typ == txt) {
std::cout << tx[fil[targetNodeId].g].s << std::endl;
} else if (fil[targetNodeId].typ == cmd) {
std::cout << cm[fil[targetNodeId].g].s << std::endl;
}
std::cout << std::endl << "按任意键返回...";
_getch();
}
}
break;
}
case 'e':
case 'E': {
if (selectedItemIndex >= 1 && selectedItemIndex <= static_cast<int>(children.size())) {
int targetNodeId = children[selectedItemIndex - 1];
if (fil[targetNodeId].typ == txt) {
editTextFile(targetNodeId);
} else if (fil[targetNodeId].typ == cmd) {
editCmdFile(targetNodeId);
} else {
clearConsole();
std::cout << "只能编辑文件类型!" << std::endl;
std::cout << "按任意键返回...";
_getch();
}
}
break;
}
case 'r':
case 'R': { // 运行命令文件
if (selectedItemIndex >= 1 && selectedItemIndex <= static_cast<int>(children.size())) {
int targetNodeId = children[selectedItemIndex - 1];
if (fil[targetNodeId].typ == cmd) {
executeCmdFile(targetNodeId, currentDirId);
} else {
clearConsole();
std::cout << "只能运行命令文件!" << std::endl;
std::cout << "按任意键返回...";
_getch();
}
}
break;
}
case 'n':
case 'N': {
int newFolderIndex = 1 + children.size();
int newFileIndex = newFolderIndex + 1;
int newCmdIndex = newFileIndex + 1;
if (selectedItemIndex == newFolderIndex) {
std::string folderName = getInput("请输入新文件夹名称: ");
if (!folderName.empty()) {
if (isNameExists(currentDirId, folderName, folder)) {
clearConsole();
std::cout << "错误:文件夹 \"" << folderName << "\" 已存在!" << std::endl;
std::cout << "按任意键返回...";
_getch();
} else {
NewFold(currentDirId, folderName);
}
}
} else if (selectedItemIndex == newFileIndex) {
std::string fileName = getInput("请输入新文本文档名称: ");
if (!fileName.empty()) {
if (fileName.find(".txt") == std::string::npos) {
fileName += ".txt";
}
if (isNameExists(currentDirId, fileName, txt)) {
clearConsole();
std::cout << "错误:文件 \"" << fileName << "\" 已存在!" << std::endl;
std::cout << "按任意键返回...";
_getch();
} else {
NewTxt(currentDirId, fileName);
}
}
} else if (selectedItemIndex == newCmdIndex) {
std::string cmdName = getInput("请输入新命令文件名称: ");
if (!cmdName.empty()) {
if (cmdName.find(".cmd") == std::string::npos) {
cmdName += ".cmd";
}
if (isNameExists(currentDirId, cmdName, cmd)) {
clearConsole();
std::cout << "错误:命令文件 \"" << cmdName << "\" 已存在!" << std::endl;
std::cout << "按任意键返回...";
_getch();
} else {
NewCmd(currentDirId, cmdName);
}
}
}
selectedItemIndex = 0;
break;
}
case 'd':
case 'D': {
deleteItem(currentDirId, selectedItemIndex, children);
break;
}
}
}
}
// 初始化文件系统
void init() {
fil[1].fa = 1;
fil[1].typ = folder;
fil[1].g = 1;
fil[1].nam = "jxh's Windys 11";
fol[1].so.clear();
}
int main() {
init();
int currentLocation = 1;
while (true) {
currentLocation = inFile(currentLocation);
}
return 0;
}操作系统
这是 2025年11月9日 存档的第 1 版,不是当前版本。查看当前版本