Skip to content

developerhost/coding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

標準入力

下の例では、外部ファイル(input.txt)に入力を書いています。

# command promptの場合
node ./dist/index.js < input.txt

# powershellの場合
cat input.txt | node ./dist/index.js

index.ts

import * as readline from "readline";

const stream = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let inputs = "";
stream.on("line", (line) => {
  inputs += line;
  inputs += "\n";
});
stream.on("close", () => {
  // ここで処理をする。
});

テンプレート

// readline モジュールを使って標準入力を受け取るためのインターフェースを作成
import { createInterface } from "readline";
// ファイルシステムモジュールを使用してファイル操作や標準入力を処理
import * as fs from "fs";

// 入力全体を格納する文字列変数
let inputs = "";
// 入力を分割して格納する配列
let inputArray: string[];
// 現在処理中の入力配列のインデックス
let currentIndex = 0;

// 出力結果をバッファに蓄積するための文字列
let outputBuffer = "";

// 入力配列から次の値を取得し、インデックスを進める関数
function next() {
  return inputArray[currentIndex++];
}

// 入力配列から次の値を数値に変換して取得する関数
function nextNum() {
  return +next(); // `+`演算子で文字列を数値に変換
}

// 入力配列から次の値をBigInt型に変換して取得する関数
function nextBigInt() {
  return BigInt(next());
}

// 指定された長さの配列を入力から取得する関数(文字列)
function nexts(length: number) {
  const arr = [];
  for (let i = 0; i < length; ++i) arr[i] = next(); // 配列に値を格納
  return arr;
}

// 指定された長さの配列を入力から取得する関数(数値)
function nextNums(length: number) {
  const arr = [];
  for (let i = 0; i < length; ++i) arr[i] = nextNum();
  return arr;
}

// 指定された長さの配列を入力から取得する関数(BigInt)
function nextBigInts(length: number) {
  const arr = [];
  for (let i = 0; i < length; ++i) arr[i] = nextBigInt();
  return arr;
}

// 出力バッファに文字列や数値を追加する関数(print)
// 配列の場合は指定された区切り文字で結合
function print(out: string | number | bigint): void;
function print<T>(out: Array<T>, separator: string): void;
function print<T>(
  out: string | number | bigint | Array<T>,
  separator?: string
) {
  if (Array.isArray(out)) {
    outputBuffer += out.join(separator); // 配列を指定された区切り文字で結合
  } else {
    outputBuffer += out; // 単一の値をそのまま追加
  }
}

// 出力バッファに文字列や数値を追加し、改行を挿入する関数(println)
function println(out: string | number | bigint): void;
function println<T>(out: Array<T>, separator: string): void;
function println<T>(
  out: string | number | bigint | Array<T>,
  separator?: string
) {
  if (Array.isArray(out)) {
    print(out, separator || ""); // 配列を指定された区切り文字で結合して出力
  } else {
    print(out); // 単一の値をそのまま出力
  }
  print("\n"); // 改行を追加
}

// バッファに蓄積された出力を一括で標準出力に書き出す関数
function flush() {
  console.log(outputBuffer); // 標準出力にバッファの内容を表示
}

// デバッグ環境がWindowsの場合に特別な処理を行う
if (process.env.OS == "Windows_NT") {
  const stream = createInterface({
    input: process.stdin, // 標準入力を指定
    output: process.stdout, // 標準出力を指定
  });
  // 標準入力のデータを1行ずつ読み込むイベントリスナー
  stream.on("line", (line) => {
    inputs += line; // 行データを蓄積
    inputs += "\n";
  });
  // 入力の読み取りが完了した際の処理
  stream.on("close", () => {
    inputArray = inputs.split(/\s/); // 空白文字で入力を分割して配列化
    main(); // メイン処理を呼び出し
    flush(); // 出力バッファを出力
  });
} else {
  // Unix系環境では標準入力をファイルとして直接読み込む
  inputs = fs.readFileSync("/dev/stdin", "utf8");
  inputArray = inputs.split(/\s/); // 空白文字で入力を分割して配列化
  main(); // メイン処理を呼び出し
  flush(); // 出力バッファを出力
}

// メイン処理を定義する関数
function main() {
  // この中に解決したいロジックを記述する
}

使用例

function main() {
  const [a, b, c] = nextNums(3);
  const s = next();
  println(`${a + b + c} ${s}`);
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published