RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1588029
Accepted
Dima
Dima
Asked:2024-07-23 13:45:34 +0000 UTC2024-07-23 13:45:34 +0000 UTC 2024-07-23 13:45:34 +0000 UTC

如何在一个输入中编写一个计算器? [复制]

  • 772
这个问题已经在这里得到回答:
JS - 如何从字符串计算算术表达式的值 (3 个答案)
22 分钟前关闭。

大家好!

我有这个想法,但还没有达到实现的地步。我想在一个介绍性的行中制作一个计算器,屏幕截图中的一个示例。

这就是这个想法的样子

我开始写一些东西,但我面临的事实是我根本不明白如何在一行中处理符号和数字。起初看起来很简单,将其分散在数组中(从符号中筛选出数字),但即使在这里我也无法完成这个想法。这是一个例子(2 + 2),我很快就解决了所有问题,但困难是如果我突然想添加的不是 2 个数字,而是一个复杂的例子,(2 + 2 * 5 / 2)

这是到目前为止我们所得到的:

let btn = document.getElementById('button');
let inpt = document.getElementById('input');
let res = document.getElementById('result');

function math(e) {

  let arr = String(e).split(' ');

  arr.forEach((element, index) => {

    if (element == '-' || element == '+' || element == '/' || element == '*') {

      //res.innerHTML += element + ' (' + index + ') ';

      const arrSum = [element];
    } else {

      const arrNumber = [element];
      //res.innerHTML += element + ' (' + index + ') ';
    };


    //res.innerHTML += " (" + element + " | " + index + ") ";
  });


  //res.innerHTML = a;

};

btn.onclick = function() {

  if (inpt.value !== '') {

    //const i = String(inpt.value).split(' ');
    math(inpt.value);


  } else {

    res.innerHTML = 'Поле не может быть пустым';
  }
};

我请教知道的人帮忙。

javascript
  • 2 2 个回答
  • 59 Views

2 个回答

  • Voted
  1. Best Answer
    artur1214
    2024-07-23T15:00:30Z2024-07-23T15:00:30Z

    事实上,这是一个很好的算法问题,我个人建议你尝试自己解决。

    解决方案

    1)eval

    这是一个非常糟糕的选择,最好不要在浏览器中使用它,但是 JS 允许您使用以下方式在行内执行代码eval:

    eval('2+2*(74/(32+5))') // Выведет 6
    

    例如,eval您可以在此处阅读有关问题的信息

    对于那些知道什么eval不能直接使用的狡猾的人来说,有一个类似的方法new Function:

    const expression = '2+2*(74/(32+5))'
    const res = (new Function(`return ${expression}`))() // Вернет 6
    
    2)算法

    “正确”的算法解决方案可能如下所示:

    function evaluateExpression(expression) {
        function getPriority(operator) {
            // Приоритет операции
            switch (operator) {
                case '+':
                case '-':
                    return 1;
                case '*':
                case '/':
                    return 2;
                default:
                    return 0;
            }
        }
        function applyOperation(operands, operator) {
            //Выполнение одной простой операции
            const b = operands.pop();
            const a = operands.pop();
            switch (operator) {
                case '+':
                    operands.push(a + b);
                    break;
                case '-':
                    operands.push(a - b);
                    break;
                case '*':
                    operands.push(a * b);
                    break;
                case '/':
                    operands.push(a / b);
                    break;
            }
            //Раскомментируйте, чтобы видеть порядок выполнения
            //console.log(`Выполнение ${a} ${operator} ${b} = ${operands[operands.length-1]}`)
        }
        const operators = [];
        const operands = [];
        let i = 0;
    
        while (i < expression.length) {
            const char = expression[i];
            //Пропуск пробелов
            if (char === ' ') {
                i++;
                continue;
            }
    
            //Посимвольный сбор числа
            if (char >= '0' && char <= '9') {
                let buffer = '';
                while (i < expression.length && (expression[i] >= '0' && expression[i] <= '9' || expression[i] === '.')) {
                    buffer += expression[i++];
                }
                operands.push(parseFloat(buffer));
                continue;
            }
    
            //Обработка скобок
            if (char === '(') {
                operators.push(char);
            } else if (char === ')') {
                while (operators.length && operators[operators.length - 1] !== '(') {
                    applyOperation(operands, operators.pop());
                }
                operators.pop();
            } else if ("+-*/".includes(char)) {
                while (operators.length && getPriority(operators[operators.length - 1]) >= getPriority(char)) {
                    applyOperation(operands, operators.pop());
                }
                operators.push(char);
            }
            i++;
        }
        //Выполнение оставшихся операций
        while (operators.length) {
            applyOperation(operands, operators.pop());
        }
        return operands[0];
    }
    
    const expression = "2+2*(74/(32+5))";
    console.log(evaluateExpression(expression)); // Вывод: 6
    
    

    一个粗略的想法是:

    1. 我们来过一遍线
    2. 我们将操作数(数字)收集到数字堆栈中,将运算符(符号)收集到运算符数组中
    3. 我们进行操作

    让我们用一个例子来逐个字符地看它2+2*(74/(32+5)):

    operators = []
    operands = []
    
    1. Met 2- 添加到数字堆栈中
    operators = []
    operands = [2]
    
    1. 满足+,添加到运营商
    operators = ['+']
    operands = [2]
    
    1. 我们遇到了2,添加到操作数中:
    operators = ['+']
    operands = [2, 2]
    
    1. 我们相遇是*因为它的优先级高于前一个(+),我们将其添加到运算符中。
    operators = ['+', '*']
    operands = [2, 2]
    
    1. 我们遇到了一个括号(并将其添加到运算符中:
    operators = ['+', '*', '(']
    operands = [2, 2]
    
    1. 他们看到7,然后他们看到4,他们一起收到74,并将其添加到操作数中:
    operators = ['+', '*', '(']
    operands = [2, 2, 74]
    
    1. 我们看到/它的运算符比括号的运算符要高(在括号处0),所以我们将它添加到运算符中
    operators = ['+', '*', '(', '/']
    operands = [2, 2, 74]
    
    1. 我们看到了括号(,将其添加到运算符中:
    operators = ['+', '*', '(', '/', '(']
    operands = [2, 2, 74]
    
    1. 看到3、进一步2、收到32、添加到操作数
    operators = ['+', '*', '(', '/', '(']
    operands = [2, 2, 74, 32]
    
    1. Saw +,添加到运算符
    operators = ['+', '*', '(', '/', '(', '+']
    operands = [2, 2, 74, 32]
    
    1. 我们看到5并添加到操作数中:
    operators = ['+', '*', '(', '/', '(', '+']
    operands = [2, 2, 74, 32, 5]
    
    1. 我们看到了右括号,执行了直到左括号的操作:
    operators = ['+', '*', '(', '/', '(', '+']
    operands = [2, 2, 74, 32, 5]
    //Достали последние 2 числа из `operands`:
    operands = [2, 2, 74]; a = 32, b = 5;
    
    // Достали последний оператор из `operators`
    operators = ['+', '*', '(', '/', '(']; operator = '+'
    //32, 5, Оператор `+`, вычислили, получили 32+5 = 37.
    // Добавили 37 в конец оставшихся операндов, удалили открывающую скобку
    operators = ['+', '*', '(', '/']
    operands = [2, 2, 74, 37]
    
    1. 我们看到了另一个支架并以类似的方式进行了操作:
    operators = ['+', '*', '(', '/']
    operands = [2, 2, 74, 37]
    //Достали последние 2 числа из `operands`:
    operands = [2, 2]; a = 74, b = 37;
    
    // Достали последний оператор из `operators`
    operators = ['+', '*', '(']; operator = '/'
    //74, 37, Оператор `/`, вычислили, получили 74/37 = 2.
    // Добавили 2 в конец оставшихся операндов, удалили открывающую скобку
    operators = ['+', '*']
    operands = [2, 2, 2]
    
    1. 我们到达了该行的末尾并执行了剩余的操作:
    operators = ['+', '*']
    operands = [2, 2, 2]
    //Достали последние 2 числа из `operands`:
    operands = [2]; a = 2, b = 2'
    
    // Достали последний оператор из `operators`
    operators = ['+']; operator = '*'
    //2, 2, Оператор `*`, вычислили, получили 2*2 = 4.
    // Добавили 4 в конец оставшихся операндов
    operators = ['+']
    operands = [2, 4]
    
    //Достали последние 2 числа из `operands`:
    operands = []; a = 2; b = 4;
    // Достали последний оператор из `operators`
    operators = []; operator = '+'
    //2, 4, Оператор `+`, вычислили, получили 2+4 = 6.
    // Добавили 6 в конец оставшихся операндов
    
    1. 运算符数组中没有剩余任何内容,这意味着操作数中的最后一个数字就是我们的答案。
    operators = []
    operands = [6]
    

    6作为答案返回。

    我希望算法是清楚的。

    如果我们以不同的顺序看到操作,例如:

    3*6+4 它会是这样的:

    operators = []
    operands = []
    
    1. 添加了 3 个:
    operators = []
    operands = [3]
    
    1. 添加*:
    operators = ['*']
    operands = [3]
    
    1. 添加了 6 个:
    operators = ['*']
    operands = [3, 6]
    
    1. 我们见过面+。因为运算符+,优先级低于*,我们执行前面的操作,+直到它们的优先级变低(+下面只有括号):
    operators = ['*']
    operands = [3, 6]
    
    //Достали последние 2 числа из `operands`:
    operands = []; a = 3, b = 6'
    
    // Достали последний оператор из `operators`
    operators = []; operator = '*'
    // 3, 6, Оператор `*`, вычислили, получили 3*6 = 18.
    // Добавили 18 в конец оставшихся операндов
    
    operands = [18]
    operators = [];
    
    1. 我们遇到了+,添加到运算符的末尾:
    operands = [18]
    operators = ['+'];
    
    1. 我们见面4并添加到操作数中:
    operands = [18, 4]
    operators = ['+'];
    
    1. 我们已经到达了该行的末尾,让我们做剩下的事情:
    //Достали последние 2 числа из `operands`:
    operands = []; a = 18, b = 4'
    
    // Достали последний оператор из `operators`
    operators = []; operator = '+'
    // 18, 4, Оператор `+`, вычислили, получили 18+4 = 22.
    // Добавили 2 в конец оставшихся операндов
    operands = [22];
    operators = [];
    
    1. 运算符数组中没有剩余任何内容,这意味着操作数中的最后一个数字就是我们的答案。
    operators = []
    operands = [22]
    

    他们返回的答案是22。

    • 8
  2. Трипольский Пётр
    2024-07-23T14:19:35Z2024-07-23T14:19:35Z

    这是按如下方式完成的。我们正在寻找python 中的现成代码,将其上传到codeconvert.ai。、 、filter中的生成器有时转换不正确,但由于它们不在这里,我们可以认为自己很幸运reducemap

    听起来像是大学一年级的课程……

    const M_PI = 3.1415926535897932384626433832795;
    const F_G = 9.81;
    const H_LEET = 1337;
    
    const MAX_EXPR_LEN = 255;
    const MAX_TOKEN_LEN = 80;
    
    const CALC_END = -1;
    const CALC_L_BRACKET = -2;
    const CALC_R_BRACKET = -3;
    const CALC_NUMBER = -4;
    
    const OP_PLUS = 0;
    const OP_MINUS = 1;
    const OP_MULTIPLY = 2;
    const OP_DIVIDE = 3;
    const OP_PERCENT = 4;
    const OP_POWER = 5;
    const OP_UMINUS = 6;
    
    const OP_SIN = 7;
    const OP_COS = 8;
    const OP_TG = 9;
    const OP_CTG = 10;
    const OP_ARCSIN = 11;
    const OP_ARCCOS = 12;
    const OP_ARCTG = 13;
    const OP_ARCCTG = 14;
    const OP_SH = 15;
    const OP_CH = 16;
    const OP_TH = 17;
    const OP_CTH = 18;
    const OP_EXP = 19;
    const OP_LG = 20;
    const OP_LN = 21;
    const OP_SQRT = 22;
    const OP_IN = 23;
    const CALC_PI = 24;
    const CALC_G = 25;
    const CALC_LEET = 26;
    
    const TERMINATOR = "\x00";
    
    function strlen(list1) {
      let i = 0;
      while (list1[i] !== TERMINATOR) {
        i++;
      }
      return i;
    }
    
    function strcmp(list1, list2) {
      const total = Math.min(list1.length, list2.length);
      for (let i = 0; i < total; i++) {
        if (i > list1.length - 1) return false;
        if (i > list2.length - 1) return false;
        if (list1[i] !== list2[i]) return false;
      }
      return true;
    }
    
    function atof(text) {
      const str = text.slice(0, strlen(text)).join("");
      const integer = parseInt(str);
      const double = parseFloat(str);
      return integer === double ? integer : double;
    }
    
    class TCALCNode {
      constructor(_value = 0, _left = null, _right = null) {
        this.value = _value;
        this.left = _left;
        this.right = _right;
      }
    }
    
    class TCALC {
      constructor() {
        this.root = null;
        this.expr = Array(MAX_EXPR_LEN).fill(TERMINATOR);
        this.curToken = Array(MAX_TOKEN_LEN).fill(TERMINATOR);
        this.typToken = 0;
        this.pos = 0;
        this.result = 0;
      }
    
      static IsDelim(self) {
        for (const char of "+-*/%^()[]") {
          if (self.expr[self.pos] === char) {
            return true;
          }
        }
        return false;
      }
    
      static IsLetter(self) {
        const char = self.expr[self.pos];
        return (char >= "a" && char <= "z") || (char >= "A" && char <= "Z");
      }
    
      static IsDigit(self) {
        const char = self.expr[self.pos];
        return char >= "0" && char <= "9";
      }
    
      static IsPoint(self) {
        return self.expr[self.pos] === ".";
      }
    
      GetToken() {
        this.curToken[0] = TERMINATOR;
        while (this.expr[this.pos] === " ") {
          this.pos++;
        }
        if (this.expr[this.pos] === TERMINATOR) {
          this.curToken[0] = TERMINATOR;
          this.typToken = CALC_END;
          return true;
        } else if (TCALC.IsDelim(this)) {
          this.curToken[0] = this.expr[this.pos];
          this.pos++;
          this.curToken[1] = TERMINATOR;
          const tmp = this.curToken.slice(0, strlen(this.curToken)).join("");
          switch (tmp) {
            case "+":
              this.typToken = OP_PLUS;
              break;
            case "-":
              this.typToken = OP_MINUS;
              break;
            case "*":
              this.typToken = OP_MULTIPLY;
              break;
            case "/":
              this.typToken = OP_DIVIDE;
              break;
            case "%":
              this.typToken = OP_PERCENT;
              break;
            case "[":
            case "(":
              this.typToken = CALC_L_BRACKET;
              break;
            case "]":
            case ")":
              this.typToken = CALC_R_BRACKET;
              break;
          }
          return true;
        } else if (TCALC.IsLetter(this)) {
          let i = 0;
          while (TCALC.IsLetter(this)) {
            this.curToken[i] = this.expr[this.pos];
            this.pos++;
            i++;
          }
          this.curToken[i] = TERMINATOR;
          const len = strlen(this.curToken);
          for (let i = 0; i < len; i++) {
            if (this.curToken[i] >= "A" && this.curToken[i] <= "Z") {
              this.curToken[i] = String.fromCharCode(
                this.curToken[i].charCodeAt(0) +
                  "a".charCodeAt(0) -
                  "A".charCodeAt(0),
              );
            }
          }
          if (!strcmp(this.curToken, "leet".split(""))) {
            this.typToken = CALC_LEET;
          } else if (!strcmp(this.curToken, "g".split(""))) {
            this.typToken = CALC_G;
          } else if (!strcmp(this.curToken, "pi".split(""))) {
            this.typToken = CALC_PI;
          } else if (!strcmp(this.curToken, "sin".split(""))) {
            this.typToken = OP_SIN;
          } else if (!strcmp(this.curToken, "cos".split(""))) {
            this.typToken = OP_COS;
          } else if (!strcmp(this.curToken, "tg".split(""))) {
            this.typToken = OP_TG;
          } else if (!strcmp(this.curToken, "ctg".split(""))) {
            this.typToken = OP_CTG;
          } else if (!strcmp(this.curToken, "arcsin".split(""))) {
            this.typToken = OP_ARCSIN;
          } else if (!strcmp(this.curToken, "arccos".split(""))) {
            this.typToken = OP_ARCCOS;
          } else if (!strcmp(this.curToken, "sh".split(""))) {
            this.typToken = OP_SH;
          } else if (!strcmp(this.curToken, "ch".split(""))) {
            this.typToken = OP_CH;
          } else if (!strcmp(this.curToken, "th".split(""))) {
            this.typToken = OP_TH;
          } else if (!strcmp(this.curToken, "cth".split(""))) {
            this.typToken = OP_CTH;
          } else if (!strcmp(this.curToken, "exp".split(""))) {
            this.typToken = OP_EXP;
          } else if (!strcmp(this.curToken, "lg".split(""))) {
            this.typToken = OP_LG;
          } else if (!strcmp(this.curToken, "ln".split(""))) {
            this.typToken = OP_LN;
          } else if (!strcmp(this.curToken, "sqrt".split(""))) {
            this.typToken = OP_SQRT;
          } else {
            this.SendError(0);
          }
        } else if (TCALC.IsDigit(this) || TCALC.IsPoint(this)) {
          let i = 0;
          while (TCALC.IsDigit(this)) {
            this.curToken[i] = this.expr[this.pos];
            this.pos++;
            i++;
          }
          if (TCALC.IsPoint(this)) {
            this.curToken[i] = this.expr[this.pos];
            this.pos++;
            i++;
            while (TCALC.IsDigit(this)) {
              this.curToken[i] = this.expr[this.pos];
              this.pos++;
              i++;
            }
          }
          this.curToken[i] = TERMINATOR;
          this.typToken = CALC_NUMBER;
          return true;
        } else {
          this.curToken[0] = this.expr[this.pos];
          this.pos++;
          this.curToken[1] = TERMINATOR;
          this.SendError(1);
        }
        return false;
      }
    
      CreateNode(_value, _left, _right) {
        return new TCALCNode(_value, _left, _right);
      }
    
      Expr() {
        let temp = this.Expr1();
        while (true) {
          if (this.typToken === OP_PLUS) {
            this.GetToken();
            temp = this.CreateNode(OP_PLUS, temp, this.Expr1());
          } else if (this.typToken === OP_MINUS) {
            this.GetToken();
            temp = this.CreateNode(OP_MINUS, temp, this.Expr1());
          } else {
            break;
          }
        }
        return temp;
      }
    
      Expr1() {
        let temp = this.Expr2();
        while (true) {
          if (this.typToken === OP_MULTIPLY) {
            this.GetToken();
            temp = this.CreateNode(OP_MULTIPLY, temp, this.Expr2());
          } else if (this.typToken === OP_DIVIDE) {
            this.GetToken();
            temp = this.CreateNode(OP_DIVIDE, temp, this.Expr2());
          } else if (this.typToken === OP_PERCENT) {
            this.GetToken();
            temp = this.CreateNode(OP_PERCENT, temp, this.Expr2());
          } else {
            break;
          }
        }
        return temp;
      }
    
      Expr2() {
        let temp = null;
        if (this.typToken === OP_PLUS) {
          this.GetToken();
          temp = this.Expr3();
        } else if (this.typToken === OP_MINUS) {
          this.GetToken();
          temp = this.CreateNode(OP_UMINUS, this.Expr3(), null);
        } else {
          temp = this.Expr3();
        }
        return temp;
      }
    
      Expr3() {
        let temp = null;
        if (this.typToken >= OP_SIN && this.typToken <= OP_SQRT + 1) {
          temp = this.CreateNode(OP_SIN - OP_SIN + this.typToken, null, null);
          this.GetToken();
          if (this.typToken !== CALC_L_BRACKET) {
            this.SendError(4);
          }
          this.GetToken();
          temp.left = this.Expr();
          if (this.typToken !== CALC_R_BRACKET) {
            this.SendError(5);
          }
          this.GetToken();
        } else {
          temp = this.Expr4();
        }
        return temp;
      }
    
      Expr4() {
        let temp = null;
        if (this.typToken === CALC_NUMBER) {
          temp = this.CreateNode(atof(this.curToken), null, null);
          this.GetToken();
        } else if (this.typToken === CALC_PI) {
          temp = this.CreateNode(M_PI, null, null);
          this.GetToken();
        } else if (this.typToken === CALC_G) {
          temp = this.CreateNode(F_G, null, null);
          this.GetToken();
        } else if (this.typToken === CALC_L_BRACKET) {
          this.GetToken();
          temp = this.Expr();
          if (this.typToken !== CALC_R_BRACKET) {
            this.SendError(5);
          }
          this.GetToken();
        } else if (this.typToken === CALC_LEET) {
          temp = this.CreateNode(H_LEET, null, null);
          this.GetToken();
        } else {
          this.SendError(5);
        }
        return temp;
      }
    
      SendError(errNum) {
        if (this.curToken === TERMINATOR) {
          console.log("Пустое выражение");
        } else if (errNum === 2) {
          console.log("Внезапный конец выражения");
        } else if (errNum === 3) {
          console.log("Конец выражения ожидается");
        } else if (errNum === 4) {
          console.log("Пропущеннаи открывающая скобка");
        } else if (errNum === 5) {
          console.log("Пропущенна закрывающая скобка");
        } else {
          console.log("Неизвестная ошибка");
        }
        throw new Error("");
      }
    
      Compile(_expr) {
        this.pos = 0;
        this.expr = _expr.concat([TERMINATOR]);
        if (this.root !== null) {
          this.root = null;
        }
        this.GetToken();
        if (this.typToken === CALC_END) {
          this.SendError(2);
        }
        this.root = this.Expr();
        if (this.typToken !== CALC_END) {
          this.SendError(3);
        }
        return true;
      }
    
      GetResult() {
        return this.result;
      }
    
      Evaluate() {
        this.result = this.CalcTree(this.root);
      }
    
      CalcTree(tree) {
        if (tree.left === null && tree.right === null) {
          return tree.value;
        } else {
          const op = tree.value;
          switch (op) {
            case OP_PLUS:
              return this.CalcTree(tree.left) + this.CalcTree(tree.right);
            case OP_MINUS:
              return this.CalcTree(tree.left) - this.CalcTree(tree.right);
            case OP_MULTIPLY:
              return this.CalcTree(tree.left) * this.CalcTree(tree.right);
            case OP_DIVIDE:
              return this.CalcTree(tree.left) / this.CalcTree(tree.right);
            case OP_PERCENT:
              return this.CalcTree(tree.left) % this.CalcTree(tree.right);
            case OP_POWER:
              return Math.pow(this.CalcTree(tree.left), this.CalcTree(tree.right));
            case OP_UMINUS:
              return -this.CalcTree(tree.left);
            case OP_SIN:
              return Math.sin(this.CalcTree(tree.left));
            case OP_COS:
              return Math.cos(this.CalcTree(tree.left));
            case OP_TG:
              return Math.tan(this.CalcTree(tree.left));
            case OP_CTG:
              return 1.0 / Math.tan(this.CalcTree(tree.left));
            case OP_ARCSIN:
              return Math.asin(this.CalcTree(tree.left));
            case OP_ARCCOS:
              return Math.acos(this.CalcTree(tree.left));
            case OP_ARCTG:
              return Math.atan(this.CalcTree(tree.left));
            case OP_ARCCTG:
              return M_PI / 2.0 - Math.atan(this.CalcTree(tree.left));
            case OP_SH:
              const temp1 = this.CalcTree(tree.left);
              return (Math.exp(temp1) - Math.exp(-temp1)) / 2.0;
            case OP_CH:
              const temp2 = this.CalcTree(tree.left);
              return (Math.exp(temp2) + Math.exp(-temp2)) / 2.0;
            case OP_TH:
              const temp3 = this.CalcTree(tree.left);
              return (
                (Math.exp(temp3) - Math.exp(-temp3)) /
                (Math.exp(temp3) + Math.exp(-temp3))
              );
            case OP_CTH:
              const temp4 = this.CalcTree(tree.left);
              return (
                (Math.exp(temp4) + Math.exp(-temp4)) /
                (Math.exp(temp4) - Math.exp(-temp4))
              );
            case OP_EXP:
              return Math.exp(this.CalcTree(tree.left));
            case OP_LG:
              return Math.log10(this.CalcTree(tree.left));
            case OP_LN:
              return Math.log(this.CalcTree(tree.left));
            case OP_SQRT:
              return Math.sqrt(this.CalcTree(tree.left));
            case OP_IN:
              return 1;
          }
        }
        return 0;
      }
    }
    
    const CALC = new TCALC();
    
    const readline = require("readline").createInterface({
      input: process.stdin,
      output: process.stdout,
    });
    
    function prompt() {
      readline.question(">> ", (input) => {
        try {
          CALC.Compile(Array.from(input));
          CALC.Evaluate();
          console.log("Ответ:    ", CALC.GetResult());
          console.log("");
        } catch (e) {
          readline.close();
        }
        prompt();
      });
    }
    
    prompt();
    
    

    截屏

    • 3

相关问题

  • 第二个 Instagram 按钮的 CSS 属性

  • 由于模糊,内容不可见

  • 弹出队列。消息显示不正确

  • 是否可以在 for 循环中插入提示?

  • 如何将 JSON 请求中的信息输出到数据表 Vuetify vue.js?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5