同調実験室 - ARGIUS.net

TOP PAGE INDEX Number / 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

03:四則計算

機能

  1. 数値1入力を促す文言 + > を表示
  2. 標準入力から数値1を取得
  3. 数値2入力を促す文言 + > を表示
  4. 標準入力から数値2を取得
  5. 取得した数値の和、差、積、商を表示

※整数以外の入力、ゼロ除算などのエラーチェックは考慮していません。

実行結果

$ ./test
#1 >12
#2 >5
12 + 5 = 17
12 - 5 = 7
12 * 5 = 60
12 / 5 = 2

$

ソース

C

#include <stdio.h>

int main()
{
  int a = 0, b = 0;
  
  printf("#1 > ");
  scanf("%d",&a);
  
  printf("#2 > ");
  scanf("%d",&b);
  
  printf("%d + %d = %d\n", a, b,  a + b);
  printf("%d - %d = %d\n", a, b,  a - b);
  printf("%d * %d = %d\n", a, b,  a * b);
  printf("%d / %d = %d\n", a, b,  a / b);
  
  return 0;
}

C++

#include <iostream>

int main()
{
  int a = 0, b = 0;
  
  std::cout << "#1 > ";
  std::cin >> a;
  
  std::cout << "#2 > ";
  std::cin >> b;
  
  std::cout << a << " + " << b << " = " << a + b << std::endl;
  std::cout << a << " - " << b << " = " << a - b << std::endl;
  std::cout << a << " * " << b << " = " << a * b << std::endl;
  std::cout << a << " / " << b << " = " << a / b << std::endl;
  
  return 0;
}

Java

import java.io.*;

class Main {
    
    public static void main(String[] args) {
        
        try {
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            
            System.out.print("#1 >");
            int a = Integer.parseInt(reader.readLine());
            System.out.print("#2 >");
            int b = Integer.parseInt(reader.readLine());
            
            System.out.println( a + " + " + b + " = " + (a + b) );
            System.out.println( a + " - " + b + " = " + (a - b) );
            System.out.println( a + " * " + b + " = " + (a * b) );
            System.out.println( a + " / " + b + " = " + (a / b) );
            
        } catch (Exception e) {
            // IOException
            // NumberFormatException
            // ArithmeticException
            e.printStackTrace();
        }
        
    }
    
}

Perl

print"#1 >";
chomp(my $a = <STDIN>);
print"#2 >";
chomp(my $b = <STDIN>);

printf"%d + %d = %d\n", $a, $b, $a + $b;
printf"%d - %d = %d\n", $a, $b, $a - $b;
printf"%d * %d = %d\n", $a, $b, $a * $b;
printf"%d / %d = %d\n", $a, $b, int($a / $b);

Ruby

print "#1 >"
a = gets.to_i
print "#2 >"
b = gets.to_i

puts "#{a} + #{b} = #{a + b}"
puts "#{a} - #{b} = #{a - b}"
puts "#{a} * #{b} = #{a * b}"
puts "#{a} / #{b} = #{a / b}"

最終更新日 : 2005.05.28

TOP PAGE INDEX Number / 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18