javascript nodejs 中的 C scanf 等效项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14238187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:07:06  来源:igfitidea点击:

C scanf equivalent in nodejs

javascriptnode.js

提问by Alexander T.

If I want to read from the input stream in CI write scanf., Is there equivalent method in NodeJSto do the same?

如果我想从C 中的输入流中读取我写的scanf.,NodeJS 中是否有等效的方法来做同样的事情?

For example, here's the code in C

例如,这里是C 中的代码

int n,
    m,
    i;

scanf("%d", &n);

for (i = 0; i < n; i++) {
    scanf("%d", &m);

    ............

}

Here's where I'm starting from in Node... TODOindicates where I'm stuck:

这是我从 Node 开始的地方...... TODO表明我被卡住的地方:

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

process.stdin.on("data", function (input) {
    var n = +input;
    for (var i = 0; i < n; i++) {
        // TODO
    }
});

回答by user7116

For starters, calling scanfand the dataevent for a readable stream in NodeJS are not equivalent. In the NodeJS example, you will need to parse the chunk of the input you've received.

对于初学者来说,NodeJS 中可读流的调用scanfdata事件并不等效。在 NodeJS 示例中,您需要解析收到的输入块。

You can examine how these chunks are sent to you by replacing the body of your on method with a simple:

您可以通过用简单的方法替换 on 方法的主体来检查这些块是如何发送给您的:

process.stdout.write('onData: ' + input + '\n');

Given how inputthen contains your data you'll need to use some method to extract the string of interest and then use parseInt. Perhaps a naive approach to your problem, assuming 1 integer per input:

鉴于如何input包含您的数据,您需要使用某种方法来提取感兴趣的字符串,然后使用parseInt. 假设每个 1 个整数,也许是解决问题的幼稚方法input

var n = 0;
var m = 0;
var state = 0;
process.stdin.on('data', function (input) {
    switch (state)
    {
    case 0:
        // we're reading 'n'
        n = parseInt(input.trim(), 10);
        state++;
        break;

    default:
        // we're reading 'm'
        m = parseInt(input.trim(), 10);

        if (state++ == n)
        {
            // we've read every 'm'
            process.exit();
        }
        break;
    }
});

I'm not a terribly large fan of this means of getting data to your NodeJS event loop, you should instead look to command line arguments, configuration/input files, or some other means.

我不太喜欢这种将数据传送到 NodeJS 事件循环的方法,您应该查看命令行参数、配置/输入文件或其他一些方法。

回答by Jorge Bucaran

Check sget.

检查sget

var sget = require('./sget');

var width = sget('Width?'),
    height = sget('Height?'),
    area = width * height;

console.log('Area is', area);

回答by Jeremy J Starcher

This function will do what you asked for:

此功能将执行您的要求:

function readNums(s) {
   // Split it up into numbers and spaces
   var array = s.split(/(\d+)/);

   // Keep just the numbers
   array = array.filter(function(i) {return "" + +i == i});

   // Convert back to a number
   array = array.map(function(i) {return +i});

   // How many elements should there have been?
   var maxLen = array.shift();

   if (array.length < maxLen) {
     throw "Not enough enough numbers";
   } else {
     array.length = maxLen;
   }


   return array; 
}

console.log(readNums("4 10 20 30 40 50 60 70"));

Result:

结果:

[10, 20, 30, 40]