2017-09-23 82 views
0

我是新来的JavaScript和有一些问题让我的代码,直到他们要求退出时不断询问用户输入。它运行一次,再次打印提示,然后退出。任何帮助将不胜感激。谢谢!我知道infix到postfix的实际转换目前不起作用...Node.js Javascript程序正在退出而无需等待用户的输入

//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(); 

回答

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(); 
0

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

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

+0

编辑:我用我的答案更好的文章替换了文章的链接。这一个更容易理解,你不必阅读这么多;-) – rweisse

相关问题