Чтение и запись в stdin или stdout

Код решения для задачи С: чтение и запись в stdin или stdout.

Выберите название компилятора (языка программирования):

Free Basic 1.04
DIM a as long
DIM b as long
DIM c as long
input a
input b
c=a+b
print c
Скопировано
GNU c 4.9
#include <stdio.h>

int main()
{
     int one = 0;
     int two = 0;
     
     if (scanf ("%d", &one) != 1) {
          printf("error1\n");
          return 1;
     }
     
     if (scanf ("%d", &two) != 1) {
          printf("error2\n");
          return 1;
     }
     printf("%d\n", one + two);
     return 0;
}
Скопировано
Mono C# 5.2.0
using System;

public class Test
{
     static void Main()
     {
          string[] array = Console.ReadLine().Split(' ');
          int a = int.Parse(array[0]);
          int b = int.Parse(array[1]); ;
          Console.WriteLine(a + b);
     }
}
Скопировано
GNU C++ 4.9
#include <iostream>
#include<vector>
int main(){
int A,B;
std::vector<int> numbs;
std::cin>>A>>B;
numbs.push_back(A);
numbs.push_back(B);
int C=0;
C=numbs[0]+numbs[1];
std::cout<<C;
return 0;
}
Скопировано
GNU C++ 11 4.9
#include <iostream>

using namespace std;

int main()
{
int a, b;
cin >> a;
cin >> b;
cout << a + b;
}
Скопировано
GNU C++ 17 7.3
#include <iostream>
using namespace std;
int main(){
long int a, b, c;
cin >> a >> b;
cout << a+b;
}
Скопировано
Delphi 2.4.4
var 
a,b:longint;
begin
read(a,b);
write(a+b);
end.
Скопировано
gc go
package main

import (
"fmt"
)

func main() {
var num1 int64
var num2 int64
fmt.Scan(&num1, &num2)
fmt.Print(num1 + num2)
}
Скопировано
gcc go
package main

import (
"fmt"
"os"
)

func main() {
var first, second int64
fmt.Fscan(os.Stdin, &first, &second)

fmt.Println(first + second)
}
Скопировано
Haskell 7.10.2
main = print . sum . map read . words =<< getLine
Скопировано
Oracle Java 7
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main 
{
    public static void main(String[] args) throws IOException {
          BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
          String input = reader.readLine().trim();
          String[] strings = input.split(" ");
          
          System.out.println(Long.parseLong(strings[0]) + Long.parseLong(strings[1]));
    }
}
Скопировано
Oracle Java 8
import java.io.*;
public class Main
{

     public static void main(String[] args) throws Exception
     {
     int x=0;
          
          BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); 
          String vvod=br.readLine();
          String[] tok=vvod.split(" ");
          for (int i=0; i<tok.length;i++) {
            x+=Integer.parseInt(tok[i]);
            
          }
      System.out.println(x);
      }
}
Скопировано
Kotlin 1.3.50 (JRE 1.8.0)
val scan = java.util.Scanner(System.`in`)
fun main(args: Array<String>){
      val a = scan.nextInt()
      val b = scan.nextInt()
      print(a+b)
}
Скопировано
OpenJDK Java 11 x64
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{

public static void main(String[] args){
  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
     try {
          String s = bufferedReader.readLine();
     bufferedReader.close();
          String[] s2 = s.split(" ");
          int x1 = Integer.parseInt(s2[0]);
          int x2 = Integer.parseInt(s2[1]);
          System.out.println(x1+x2);
     } catch (IOException e) {
          e.printStackTrace();
     }}
}
Скопировано
Node JS 8.16
sum = (data) => {
     let mas = data.toString().split(' ');
     return +mas[0] + +mas[1];
};
          
let cnt, res;
process.stdin.on('data', data => {
     res = sum(data);
     process.stdout.write(res + '');
     process.exit();
});
Скопировано
Node JS 0.10.28
process.openStdin().on ('data', function(line) {
     const xs = String(line).match(/^\s*(\d+)\s+(\d+)\s*/)
     console.log(xs ? Number(xs[1]) + Number(xs[2]) : null);
     process.exit();
});
Скопировано
ocaml 4.02.3
Scanf.scanf "%d %d" (fun a b -> Printf.printf "%d\n" (a + b))
Скопировано
Swift 4.1.1
let array = readLine()?.split(separator: Character(" ")).map(String.init).map(Int.init)
if array != nil, array?.first != nil, array?.last != nil , array?.first! != nil, array?.last! != nil{
     print(array!.first!! + array!.last!!)
}
Скопировано
Free pascal 2.4.4
program AB;
var a, b : longint;

begin
     read(a);
     read(b);
     write(a+b);
end.
Скопировано
PascalABC.NET 3.5.1
var a,b,c : integer;
begin
readln(a,b);
c:=a+b;
writeln(c);
end.
Скопировано
Perl 5.14
#!/usr/bin/perl

chomp(my $v = <>);
my($l,$r) = split /\s+/, $v;
print $l+$r;
Скопировано
php 5.3.10
<?php

$text = trim(fgets(STDIN));
$abc = explode(" ", $text);
$a = $abc[0];
$b = $abc[1];

function summ($a,$b){
    return $a+$b;
}

fwrite(STDOUT, summ($a,$b));

?>
Скопировано
php 7.3.5
<?php

$input = fgets(STDIN);
$args = explode(' ', $input);

$result = $args[0] + $args[1];

fputs(STDOUT, $result);
Скопировано
pypy 4
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
import operator as op
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
     from __builtin__ import xrange as range
     from future_builtins import ascii, filter, hex, map, oct, zip

BUFSIZE = 8192


class FastIO(IOBase):
      newlines = 0
      
      def __init__(self, file):
          self._fd = file.fileno()
          self.buffer = BytesIO()
          self.writable = 'x' in file.mode or 'r' not in file.mode
          self.write = self.buffer.write if self.writable else None
          
      def read(self):
          while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
               break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
          self.newlines = 0
          return self.buffer.read()
          
      def readline(self):
          while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
          self.newlines -= 1
          return self.buffer.readline()
          
      def flush(self):
          if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
          
          
class IOWrapper(IOBase):
      def __init__(self, file):
          self.buffer = FastIO(file)
          self.flush = self.buffer.flush
          self.writable = self.buffer.writable
          self.write = lambda s: self.buffer.write(s.encode('ascii'))
          self.read = lambda: self.buffer.read().decode('ascii')
          self.readline = lambda: self.buffer.readline().decode('ascii')
          
          
def print(*args, **kwargs):
      """Prints the values to a stream, or to sys.stdout by default."""
      sep, file = kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout)
      at_start = True
      for x in args:
          if not at_start:
            file.write(sep)
          file.write(str(x))
          at_start = False
        file.write(kwargs.pop('end', '\n'))
        if kwargs.pop('flush', False):
            file.flush()
          
          
if sys.version_info[0] < 3:
      sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
      sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
          
input = lambda: sys.stdin.readline().rstrip('\r\n')
          
          
def main():
      print(sum(map(int, input().split())))
          
          
if __name__ == '__main__':
      main()
Скопировано
Python 2.7
import sys
a,b = map(int,sys.stdin.readline().split())
print(a+b)
Скопировано
Python 3.6
print(sum(map(int, input().split())))
Скопировано
R
input<-file('stdin', 'r')
a <- readLines(input, n=1)
a <- as.numeric(unlist(strsplit(a, " ")))
cat(sum(a))
Скопировано
Ruby 1.9.3
input = gets
sum = 0
input.split(' ').each {|number| sum+=number.to_i}
puts sum
Скопировано
Ruby 2.2.3
a, b = gets.split(' ')

puts a.to_i + b.to_i
Скопировано
rust 1.2
use std::fs::File;
use std::io::{Write, BufReader, BufRead};

use std::io;

fn main(){
     let mut input = String::new();
     match io::stdin().read_line(&mut input) {
          Ok(_n) => {
            let vec: Vec<&str> = input.split_whitespace().collect();
            let a = vec[0].parse::<i32>().unwrap_or(0).to_owned();
            let b = vec[1].parse::<i32>().unwrap_or(0).to_owned();
            println!("{}", a+b);
          }
          Err(error) => println!("error: {}", error),
     }
}
Скопировано
Scala 2.9.1
object miall {
     def main(args: Array[String]): Unit = {
       val str = Console.in.readLine()
       val sum = str match {
          case "" => 0
          case _ => str.split(" ") match{
            case Array(a, b) => a.toInt+b.toInt
            case _ => 0
          }
       }
       print(sum)
     }
}
Скопировано
GNU bash 4.2.24
#!/bin/bash

read str
sum=0
for i in $str
do
  sum=$((sum + i))
done
echo $sum
Скопировано