Node.js Javascript程序退出而不等待用户输入

时间:2017-09-23 17:53:47

标签: javascript node.js

我是javascript的新手,并且遇到一些问题让我的代码继续询问用户输入,直到他们要求退出。它运行一次,再次打印提示然后退出。任何帮助将不胜感激。谢谢!我知道中缀到postfix的实际转换目前还不能正常工作......

//RPN
var readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
var presedence = {'POW': 0, '*': 1, '/': 1, '%': 1, '+': 2, '-': 2};
var operators = ['P', '*', '/', '%', '+', '-'];
var digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

var convertInfix = function (infixQ) {
  //PEMDAS
  var opS = [];
  var postfixQ = [];
  var t;
  while (infixQ.length != 0) {
    t = infixQ.shift();
    console.log(t)
    console.log(typeof t)
    if (digits.indexOf(t) > -1) {
      console.log('push number')
      postfixQ.push(t);
    }
    else if (opS.length === 0 || t == '(') {
      console.log('push operator')
      opS.push(t);
    }
    else if (t == ')') {
      while (opS[0] != '(') {
        opS.Push(t);
      }
    }
    else {
      while (opS.length != 0 && opS[0] != '(') {
        if (presedence[t] > presedence[opS[0]]) {
          break;
        }
        postfixQ.push(opS[0]);
        opS.pop();
        console.log

      }
    }
  }
  while (opS.length != 0) {
    postfixQ.push(opS[0]);
    opS.pop();
  }
  console.log(postfixQ)
};

var main = function () {
  rl.question('Enter an infix expression or "quit" to exit the     program:', (rawInfix) => {
    if (rawInfix == 'quit') {
      console.log('Exiting')
      return;
    } else {
      var infixQ = [];
      for (var i = 0; i < rawInfix.length; i++) {
        if (digits.indexOf(raxInfix[i]) > -1 ||
          operators.indexOf(rawInfix[i]) > -1 ||
          rawInfix[i] == ' ') {
          infixQ.push(rawInfix[i])
        } else if (rawInfix[i] != 'W' || rawInfix[i] != 'O') {
          console.log('Unexpected Character in input: ', rawInfix)
          main()
        }
      }
      postfixQ = convertInfix(infixQ)
      main()
      // evaluatePostFix(postfixQ)
    }
    rl.close();
  });
}
main();

2 个答案:

答案 0 :(得分:0)

你走了。注释掉了尚未运行的代码。如果用户没有输入'quit',则递归调用main。如果他们这样做,请关闭readline。

//RPN
 var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var presedence = {'POW':0,'*':1,'/':1,'%':1,'+':2,'-':2 };
var operators = ['P','*','/','%','+','-'];
var digits = ['0','1','2','3','4','5','6','7','8','9'];

var convertInfix= function(infixQ){
  //PEMDAS
   var opS =[];
   var postfixQ =[];
   var t;
   while (infixQ.length != 0){
 t = infixQ.shift();
 console.log(t)
 console.log(typeof t)
 if (digits.indexOf(t)>-1){
     console.log('push number')
     postfixQ.push(t);
   }
 else if (opS.length === 0 || t=='('){
     console.log('push operator')
     opS.push(t);
   }
 else if (t ==')'){
   while (opS[0] !='('){
      opS.Push(t);
    }
  }
  else {
     while(opS.length != 0 && opS[0] != '('){
     if (presedence[t]>presedence[opS[0]]){
       break;
      }
     postfixQ.push(opS[0]);
     opS.pop();
     console.log

   }
 }
 }while(opS.length != 0){
 postfixQ.push(opS[0]);
 opS.pop();
 }
 console.log(postfixQ)
};

var main =function(){
  rl.question('Enter an infix expression or "quit" to exit the     
program:',(rawInfix) =>{
  if (rawInfix == 'quit'){
    console.log('Exiting')
    rl.close();
    return;
  }else{
      console.log(rawInfix);
      main();
// var infixQ= [];
// for(var i=0;i<rawInfix.length;i++){
//   if (digits.indexOf(raxInfix[i])>-1 ||
//       operators.indexOf(rawInfix[i])>-1 ||
//       rawInfix[i]==' '){
//     infixQ.push(rawInfix[i])
//   }else if (rawInfix[i]!='W' || rawInfix[i]!='O'){
//     console.log('Unexpected Character in input: ',rawInfix)
//     main()
//   }
// }
// postfixQ = convertInfix(infixQ)
// main()
// // evaluatePostFix(postfixQ)
  }

});
}
   main();

答案 1 :(得分:0)

首先你应该阅读一些关于Node.js的文章。 Node.js的一个特殊功能是异步处理I / O操作:Node.js - Callbacks Concept

您的程序退出是因为提示输入在代码中处理为asynchrone。如果您想在提示中做一些输入,我建议使用现有的节点包。这个过去对我很有用:prompt