简体   繁体   中英

End process of stdin in nodejs

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
  input_stdin += data;
});

process.stdin.on('end', function () {
  input_stdin_array = input_stdin.split("\n");
  main();
});

// Reads complete line from STDIN
function readLine() {
  return input_stdin_array[input_currentline++];
}

function main() {
  var i = 4
  var d = 4.0
  var s = "HackerRank "

  var i2 = parseInt(input_stdin_array[0]);
  var d2 = parseFloat(input_stdin_array[1]);
  var s2 = input_stdin_array[2];

  var t1 = i + i2;
  var t2 = parseFloat(d + d2).toFixed(1);
  var s3 = s + s2;

  process.stdout.write(t1.toString() + "\n");
  process.stdout.write(t2.toString() + "\n");
  process.stdout.write(s3.toString());
}

How can I end the stdin process to show the output? as below code? If you know how to end the process from stdin, you will get output. my question: How could I end the process from stdin?

在命令行上,按CTRL+D发送EOF这将触发process.stdinend事件

process.stdin.resume();
process.stdin.setEncoding("ascii");

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on("data", function (data) {
  input_stdin += data;
  if (input_stdin.includes("sum")) {
    input_stdin_array = input_stdin.split("\n");
    input_stdin_array.pop();
    main();
    process.exit();
  }
});



function readLine() {
  return input_stdin_array[input_currentline++];
}

function solveMeFirst(a, b) {
  return a + b;
}

function main() {
  var a = parseInt(readLine());
  var b = parseInt(readLine());

  var res = solveMeFirst(a, b);
  console.log(res);
}



try this it will help you, not to use on end enter image description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM