跳到正文

个人记录

操作系统

Dennis_J约 67 分钟
原文
文章信息与版本

文章信息

发布
2025年11月8日
收录
2025年12月12日
更新
2026年10月28日
篇幅
约 67 分钟

版本

  1. v2当前
  2. v1
C++
#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;

// 剪切板状态枚举
enum ClipboardAction { NONE, COPY, CUT };

// 剪切板结构
struct Clipboard {
    ClipboardAction action;  // 操作类型:复制或剪切
    int nodeId;              // 被操作的节点ID
    int originalParentId;    // 原始父目录ID(用于剪切操作)
} clipboard;

// 文件/目录节点结构
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);
void copyToClipboard(int nodeId);
void cutToClipboard(int nodeId, int parentId);
bool pasteFromClipboard(int destDirId);
void clearClipboard();

// 检查当前目录下是否存在同名同类型的节点
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');
    if (std::cin.peek() == '\n') {
        std::cin.ignore();
    }
    
    std::string newContent;
    std::string line;
    bool isFirstLine = true;
    
    while (true) {
        if (!std::getline(std::cin, line)) {
            break; // 处理输入错误或EOF
        }
        
        if (line == ".") {
            break;
        }
        
        // 处理第一行,避免空内容时覆盖原始文件
        if (!isFirstLine) {
            newContent += "\n";
        }
        newContent += line;
        isFirstLine = false;
    }

    // 只有当用户确实输入了内容时才更新文件
    if (!newContent.empty() || !isFirstLine) {
        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');
    if (std::cin.peek() == '\n') {
        std::cin.ignore();
    }
    
    std::string newContent;
    std::string line;
    bool isFirstLine = true;
    
    while (true) {
        if (!std::getline(std::cin, line)) {
            break; // 处理输入错误或EOF
        }
        
        if (line == ".") {
            break;
        }
        
        // 处理第一行,避免空内容时覆盖原始文件
        if (!isFirstLine) {
            newContent += "\n";
        }
        newContent += line;
        isFirstLine = false;
    }

    // 只有当用户确实输入了内容时才更新文件
    if (!newContent.empty() || !isFirstLine) {
        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);

    // 显示剪切板状态
    if (clipboard.action == COPY) {
        std::cout << "[剪切板: 复制了 " << fil[clipboard.nodeId].nam << "]" << std::endl;
    } else if (clipboard.action == CUT) {
        std::cout << "[剪切板: 剪切了 " << fil[clipboard.nodeId].nam << "]" << std::endl;
    }
    if (clipboard.action != NONE) {
        std::cout << "----------------------------------------" << std::endl;
    }

    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]删除 | [C]复制 | [X]剪切 | [V]粘贴" << 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);
    }

    // 如果删除的是剪切板中的项目,清空剪切板
    if (clipboard.action != NONE && clipboard.nodeId == targetNodeId) {
        clearClipboard();
    }

    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 executeSingleCommand(const std::string& line, int currentDirId, 
                         std::unordered_map<std::string, std::string>& variables, int lineNum) {
    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()) return true;

    // 解析命令
    if (tokens[0] == "create") {
        if (tokens.size() < 3) {
            std::cout << "错误: create命令格式错误,应为: create [类型] [名称]" << std::endl;
            return false;
        }
        
        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;
                return false;
            } else {
                NewTxt(currentDirId, name);
                std::cout << "已创建文件: " << name << std::endl;
            }
        } else if (type == "folder") {
            if (isNameExists(currentDirId, name, folder)) {
                std::cout << "错误: 文件夹 " << name << " 已存在" << std::endl;
                return 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;
                return false;
            } else {
                NewCmd(currentDirId, name);
                std::cout << "已创建命令文件: " << name << std::endl;
            }
        } else {
            std::cout << "错误: 不支持的创建类型: " << type << std::endl;
            return false;
        }
    } 
    else if (tokens[0] == "copy") {
        if (tokens.size() < 3) {
            std::cout << "错误: copy命令格式错误,应为: copy [源文件] [目标目录]" << std::endl;
            return false;
        }
        
        std::string srcName = tokens[1];
        std::string destName = tokens[2];
        
        int srcNodeId = findItemByName(currentDirId, srcName);
        if (srcNodeId == -1) {
            std::cout << "错误: 源文件 " << srcName << " 不存在" << std::endl;
            return false;
        }
        
        int destDirId = findItemByName(currentDirId, destName);
        if (destDirId == -1 || fil[destDirId].typ != folder) {
            std::cout << "错误: 目标目录 " << destName << " 不存在或不是文件夹" << std::endl;
            return false;
        }
        
        if (copyItem(srcNodeId, destDirId)) {
            std::cout << "已复制 " << srcName << " 到 " << destName << std::endl;
        } else {
            std::cout << "错误: 复制失败,目标目录中已存在同名项目或存在父子目录关系" << std::endl;
            return false;
        }
    } 
    else if (tokens[0] == "edit") {
        if (tokens.size() < 3) {
            std::cout << "错误: edit命令格式错误,应为: edit [文件名] [内容]" << std::endl;
            return false;
        }
        
        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 << "错误: 文件 " << fileName << " 不存在" << std::endl;
            return false;
        }
        
        if (fil[fileNodeId].typ != txt && fil[fileNodeId].typ != cmd) {
            std::cout << "错误: " << fileName << " 不是可编辑文件" << std::endl;
            return false;
        }
        
        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 << "错误: delete命令格式错误,应为: delete [文件名]" << std::endl;
            return false;
        }
        
        std::string fileName = tokens[1];
        int fileNodeId = findItemByName(currentDirId, fileName);
        if (fileNodeId == -1) {
            std::cout << "错误: 文件 " << fileName << " 不存在" << std::endl;
            return false;
        }
        
        // 执行删除
        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") {
        if (tokens.size() < 2) {
            std::cout << "错误: set命令格式错误,应为: set 变量名=值" << std::endl;
            return false;
        }
        
        std::string varExpr = tokens[1];
        size_t eqPos = varExpr.find('=');
        if (eqPos == std::string::npos || eqPos == 0) {
            std::cout << "错误: set命令格式错误,应为: set 变量名=值" << std::endl;
            return false;
        }
        
        std::string varName = varExpr.substr(0, eqPos);
        std::string varValue = varExpr.substr(eqPos + 1);
        
        variables[varName] = varValue;
        std::cout << "已设置变量: " << varName << " = " << varValue << std::endl;
    }
    else {
        std::cout << "错误: 未知命令: " << tokens[0] << std::endl;
        return false;
    }
    
    return true;
}

// 执行命令文件
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::tuple<std::string, std::vector<std::string>, std::vector<std::string>, int>> loopStack;
    std::vector<std::string> loopBody;

    while (std::getline(iss, line)) {
        lineNum++;
        // 忽略注释和空行
        if (line.empty() || line.substr(0, 2) == "//") {
            continue;
        }

        // 处理循环体
        if (loopDepth > 0) {
            // 计算当前行的缩进层级
            int indent = 0;
            while (indent < line.size() && (line[indent] == ' ' || line[indent] == '\t')) {
                indent++;
            }
            
            // 检查循环结束符
            if (indent < line.size() && line.substr(indent) == "}") {
                loopDepth--;
                if (loopDepth == 0) {
                    // 执行循环
                    auto& loopData = loopStack.back();
                    loopStack.pop_back();
                    std::string varName = std::get<0>(loopData);
                    auto& values = std::get<1>(loopData);
                    int startLine = std::get<3>(loopData);

                    std::cout << "执行循环 (行 " << startLine << "): 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) {
                            if (!executeSingleCommand(loopLine, currentDirId, variables, lineNum)) {
                                success = false;
                                // 可根据需要决定是否继续执行其他迭代
                            }
                        }
                    }

                    // 恢复变量原始值
                    if (varExisted) {
                        variables[varName] = originalValue;
                    } else {
                        variables.erase(varName);
                    }

                    loopBody.clear();
                    continue;
                }
            }
            // 仅添加缩进后的实际内容
            loopBody.push_back(line.substr(indent));
            continue;
        }

        // 处理变量引用
        if (!executeSingleCommand(line, currentDirId, variables, lineNum)) {
            success = false;
        } else {
            // 检查是否是for命令(因为executeSingleCommand不会处理for循环)
            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.size() >= 4 && tokens[0] == "for" && tokens[2] == "in" && tokens[3].substr(0, 1) == "(") {
                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::vector<std::string>(), lineNum);
                std::cout << "发现循环 (行 " << lineNum << "): 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;
            }
        }
    }

    // 检查是否有未闭合的循环
    if (loopDepth > 0) {
        std::cout << "错误: 存在未闭合的循环结构" << std::endl;
        success = false;
    }

    std::cout << "\n--- 执行完成 " << (success ? "(成功)" : "(有错误)") << " ---" << std::endl;
    std::cout << "按任意键返回...";
    _getch();
    return success;
}

// 复制到剪切板 - 修改:允许复制所有类型
void copyToClipboard(int nodeId) {
    clipboard.action = COPY;
    clipboard.nodeId = nodeId;
    clipboard.originalParentId = fil[nodeId].fa;
    
    clearConsole();
    std::cout << "已复制: " << fil[nodeId].nam << std::endl;
    std::cout << "按任意键返回...";
    _getch();
}

// 剪切到剪切板 - 修改:允许剪切所有类型
void cutToClipboard(int nodeId, int parentId) {
    clipboard.action = CUT;
    clipboard.nodeId = nodeId;
    clipboard.originalParentId = parentId;
    
    clearConsole();
    std::cout << "已剪切: " << fil[nodeId].nam << std::endl;
    std::cout << "按任意键返回...";
    _getch();
}

// 从剪切板粘贴
bool pasteFromClipboard(int destDirId) {
    if (clipboard.action == NONE) {
        clearConsole();
        std::cout << "剪切板为空,无法粘贴!" << std::endl;
        std::cout << "按任意键返回...";
        _getch();
        return false;
    }
    
    // 检查是否粘贴到同一目录(剪切操作)
    if (clipboard.action == CUT && clipboard.originalParentId == destDirId) {
        clearConsole();
        std::cout << "不能将文件粘贴到同一目录!" << std::endl;
        std::cout << "按任意键返回...";
        _getch();
        return false;
    }
    
    // 检查目标目录是否已有同名同类型文件
    if (isNameExists(destDirId, fil[clipboard.nodeId].nam, fil[clipboard.nodeId].typ)) {
        clearConsole();
        std::cout << "目标目录中已存在同名文件: " << fil[clipboard.nodeId].nam << std::endl;
        std::cout << "按任意键返回...";
        _getch();
        return false;
    }
    
    bool result = false;
    
    if (clipboard.action == COPY) {
        // 复制操作:创建新文件并复制内容
        result = copyItem(clipboard.nodeId, destDirId);
    } 
    else if (clipboard.action == CUT) {
        // 剪切操作:移动文件
        int nodeId = clipboard.nodeId;
        
        // 从原目录移除
        std::vector<int>& originalChildren = fol[fil[clipboard.originalParentId].g].so;
        auto it = std::find(originalChildren.begin(), originalChildren.end(), nodeId);
        if (it != originalChildren.end()) {
            originalChildren.erase(it);
        }
        
        // 添加到新目录
        fil[nodeId].fa = destDirId;
        fol[fil[destDirId].g].so.push_back(nodeId);
        
        result = true;
        
        // 清空剪切板
        clearClipboard();
    }
    
    if (result) {
        clearConsole();
        std::cout << "已" << (clipboard.action == COPY ? "复制" : "移动") << ": " << fil[clipboard.nodeId].nam << std::endl;
        std::cout << "按任意键返回...";
        _getch();
    } else {
        clearConsole();
        std::cout << (clipboard.action == COPY ? "复制" : "移动") << "失败!" << std::endl;
        std::cout << "按任意键返回...";
        _getch();
    }
    
    return result;
}

// 清空剪切板
void clearClipboard() {
    clipboard.action = NONE;
    clipboard.nodeId = -1;
    clipboard.originalParentId = -1;
}

// 在文件/目录中导航
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;
            }
            case 'c':
            case 'C': {  // 复制到剪切板
                if (selectedItemIndex >= 1 && selectedItemIndex <= static_cast<int>(children.size())) {
                    int targetNodeId = children[selectedItemIndex - 1];
                    copyToClipboard(targetNodeId);
                }
                break;
            }
            case 'x':
            case 'X': {  // 剪切到剪切板
                if (selectedItemIndex >= 1 && selectedItemIndex <= static_cast<int>(children.size())) {
                    int targetNodeId = children[selectedItemIndex - 1];
                    cutToClipboard(targetNodeId, currentDirId);
                }
                break;
            }
            case 'v':
            case 'V': {  // 从剪切板粘贴
                pasteFromClipboard(currentDirId);
                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();
    
    // 初始化剪切板
    clipboard.action = NONE;
    clipboard.nodeId = -1;
    clipboard.originalParentId = -1;
}

int main() {
    init();
    int currentLocation = 1;

    while (true) {
        currentLocation = inFile(currentLocation);
    }

    return 0;
}
原文