私はjavascriptが新しく、終了するように求めるまでユーザー入力を求め続けるためにコードを取得するのに問題があります。一度実行し、プロンプトをもう一度表示して終了します。どんな助けでも大歓迎です。ありがとう!私は後置へのインフィックスの実際の変換が現時点では動作していないことを知っています...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();
編集:私は記事のリンクを私の答えでより良いものに置き換えました。これははるかに理解しやすく、読まなくても大丈夫です;-) – rweisse