LoginSignup
10
13

More than 5 years have passed since last update.

各プログラミング言語の標準入出力サンプル(ややマイナーなもの用)

Last updated at Posted at 2015-12-28

CodeIQの問題を解くとき、標準入出力のサンプルとして、メジャーな言語であれば、
各プログラミング言語の標準入出力サンプルを見ているんだけれども、ここに載っていないのもあるので、参考用に。

よく探してみたら、下記のページに標準入出力サンプル乗っていた。
http://ideone.com/samples
そのままだとCodeIQのコンパイラで動かないので、 加工して記載することに。
※よく見たら42が入力されるまで繰り返すという処理だった。
42って銀座ヒッチハイクガイド究極の疑問の答え
ことなのだろうか。

あとこちらも参考に
標準入力から読んで、標準出力に書く
言語別標準入出力
【言語別】複数行の標準入力→int型配列 の高速化(Tips)

  • bash

while read line
do
    echo $line
done

  • C

#include <stdio.h>

int main(void){
   char str[1024];
   while( fgets(str, sizeof(str), stdin) != NULL ){
     printf("%s", str);
   }
   return 0;
}

  • C#

using System;
public class Example
{
   public static void Main()
   {
      string line;
      do { 
         line = Console.ReadLine();
         if (line != null) 
            Console.WriteLine(line);
      } while (line != null);   
   }
}

  • Clojure

(doseq [line (line-seq (java.io.BufferedReader. *in*))] 
    (println line))



(use '[clojure.java.io])

(doseq [line (line-seq (reader *in*))]
  (println line))

  • D言語

import std.stdio;
import std.string;

void main()
{
    string line;
    while ((line = readln()) !is null) {
        writeln(chomp(line));
    }
}

  • Erlang

-module(prog).
-export([main/0]).

main() ->
    loop().
loop() ->
    case io:fread( "","~d" ) of
        eof ->
            true;
        {ok, X} ->
            [Y] = X,
            if
                Y == "" ->
                    true;
                true ->
                    io:fwrite( "~B\n",X ),
                    loop()
            end
    end.
  • F#

seq { while true do yield stdin.ReadLine () }
|> Seq.takeWhile ((<>) null)
|> Seq.iter (printfn "%s")


module Example

let mutable line = System.Console.ReadLine()
while line <> null do
    System.Console.WriteLine line
    line <- System.Console.ReadLine()


module Example

let sysin = new System.IO.StreamReader(System.Console.OpenStandardInput())
let s = sysin.ReadToEnd()
sysin.Close()

s.Split [|','|]
|> Array.iter (fun arg -> printfn "%s" arg)

  • Go

package main

import (
    "fmt"
)

func main() {
    var line string
    for { 
        c,_:=fmt.Scanln(&line)
        if c == 0 { break }
        fmt.Println(line)
    }
}

  • Groovy
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
def line
while((line = br.readLine()) != null) {
    println(line)
}

  • Java

import java.io.*;
class Main {
    public static void main(String[] args) throws IOException {
        int c;
        while ((c = System.in.read()) != -1)
            System.out.println(c);
        }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = 
                new BufferedReader(new InputStreamReader(System.in));
        String line;

        while ((line = br.readLine())!=null) {
            System.out.println(line);            
        }
    }
}
  • JavaScript(node.js)

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
    var lines = chunk.toString().split('\n');
    var N = lines.length;
    for(var i=0; i<lines.length; i++) {
        console.log(lines[i]);
    }
});

  • Lua


while true do
    local line = io.read()
    if line == nil then return end
    print(line)
end


  • Nemerle

class Example
{
  public static Main () : void
  {
    mutable line;
    do {
    line = System.Console.ReadLine();
    System.Console.WriteLine(line);
    } while (line != null) 
  }
}

CSharp Similarities and Differences

  • Perl

while(defined(my $line=<STDIN>)){
    print $line
}


while (<>) {
    print $_;
}

  • Perl6

while ($_ = $*IN.get) { say $_ }

  • PHP

<?php
while($line=fgets(STDIN))
{
      echo $line;
}
?>

  • Python

import fileinput
for line in fileinput.input():
  print line,

  • Python3

import fileinput
for line in fileinput.input():
  print(line,end='')


line = input()
while len(line) > 0:
    print(line)
    line = input()
  • R

con = file(description="stdin",open="r")
while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {
       cat(line)
}
close(con)

  • Ruby

while line = STDIN.gets
  print line
end


lines = $stdin.read.split(?\n)
lines.each{|s| puts s}

$<.map{|line| puts line }

  • Scala

object Main extends App {
    var line: String = readLine
    while(line != null) {
        println(line)
        line = readLine
    };
}



object Main extends App {
    scala.io.Source.stdin.getLines.foreach(println)
}


  • VB.Net

Module Example
    Public Sub Main()
        Dim line As String  
        Do
            line = Console.ReadLine()
            Console.WriteLine(line)
        Loop While line IsNot Nothing
    End Sub 
End Module

10
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
13