编辑代码

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
class TreeNode {
  constructor(curDicName, father) {
    this.curDicName = curDicName;
    this.father = father;
    this.children = {};
  }
}
class Tree {
  constructor() {
    this.root = new TreeNode("/", null);
    this.cur = this.root;
  }
  // 创建子目录
  mkdir(dicName) {
    if (!this.cur.children[dicName])
      this.cur.children[dicName] = new TreeNode(dicName + "/", this.cur);
  }
  // 跳转到子目录
  cd(dicName) {
    if (dicName == "..") {
      if (this.cur.father) this.cur = this.cur.father;
    } else {
      if (this.cur.children[dicName]) this.cur = this.cur.children[dicName];
    }
  }
  // 列出当前目录下所有子目录
  pwd() {
    const arr = [];
    let cur = this.cur;
    while (cur) {
      // 不停寻找父目录直到找到根目录
      arr.push(cur.curDicName);
      cur = cur.father;
    }
    return arr.reverse().join("");
  }
}
void (async function () {
  const tree = new Tree();
  let lastPwd = "";
  while (true) {
    const command = await readline();
    if (command == "") break;
    const [cmd, ...arges] = command.split(" ");
    switch (cmd) {
      // case "mkdir":
      case "pwd": {
        if (arges.length > 0) continue;
        lastPwd = tree.pwd();
        break;
      }
      case "mkdir": {
        if (arges.length !== 1) continue;
        const mkdirVal = arges[0];
        tree.mkdir(mkdirVal);
        lastPwd = "/";
        break;
      }
      case "cd": {
        if (arges.length !== 1) continue;
        const cdVal = arges[0];
        tree.cd(cdVal);
        lastPwd = "/";
        break;
      }
    }
  }
  console.log(lastPwd);
})();