2012年12月27日木曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 12章(新しいクラスのオブジェクト), 12.2(練習問題の続き)ハッピーバースデー を解いてみる。

その他参考書籍

ハッピーバースデー

コード(BBEdit)

sample.rb

#!/usr/bin/env ruby1.9
# -*- coding: utf-8 -*-

print "誕生年: "
b_year = gets.chomp.to_i
print "誕生月: "
b_month = gets.chomp.to_i
print "誕生日: "
b_day = gets.chomp.to_i

age = 1
td = Time.new
while Time.gm(b_year + age, b_month, b_day) <= td
  puts "平手打ち(パシッ) #{age}歳"
  age += 1
end

入出力結果(Terminal)

$ ./sample.rb
誕生年: 2000
誕生月: 12
誕生日: 26
平手打ち(パシッ) 1歳
平手打ち(パシッ) 2歳
平手打ち(パシッ) 3歳
平手打ち(パシッ) 4歳
平手打ち(パシッ) 5歳
平手打ち(パシッ) 6歳
平手打ち(パシッ) 7歳
平手打ち(パシッ) 8歳
平手打ち(パシッ) 9歳
平手打ち(パシッ) 10歳
平手打ち(パシッ) 11歳
平手打ち(パシッ) 12歳
$ ./sample.rb
誕生年: 2000
誕生月: 12
誕生日: 27
平手打ち(パシッ) 1歳
平手打ち(パシッ) 2歳
平手打ち(パシッ) 3歳
平手打ち(パシッ) 4歳
平手打ち(パシッ) 5歳
平手打ち(パシッ) 6歳
平手打ち(パシッ) 7歳
平手打ち(パシッ) 8歳
平手打ち(パシッ) 9歳
平手打ち(パシッ) 10歳
平手打ち(パシッ) 11歳
平手打ち(パシッ) 12歳
$ ./sample.rb
誕生年: 2000
誕生月: 12
誕生日: 28
平手打ち(パシッ) 1歳
平手打ち(パシッ) 2歳
平手打ち(パシッ) 3歳
平手打ち(パシッ) 4歳
平手打ち(パシッ) 5歳
平手打ち(パシッ) 6歳
平手打ち(パシッ) 7歳
平手打ち(パシッ) 8歳
平手打ち(パシッ) 9歳
平手打ち(パシッ) 10歳
平手打ち(パシッ) 11歳

ちなみにJavaScriptの場合。

コード(BBEdit)

var b_year = parseInt($('#b_year').val());
var b_month = parseInt($('#b_month').val());
var b_day = parseInt($('#b_day').val());
var birth = new Date(b_year, b_month - 1, b_day);
var td = new Date();
var i = 1;
birth.setYear(birth.getYear() + 1 + 1900);
while(birth <= td){
  $('#pre0').append("平手打ち(パシッ) " + i + "\n");
  birth.setYear(birth.getYear() + 1 + 1900);
  i++;
}


誕生 




pythonの場合。

sample.py

コード(BBEdit)

#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-

import datetime

b_year = int(input("誕生年: "))
b_month = int(input("誕生月: "))
b_day = int(input("誕生日: "))

birth = datetime.date(b_year, b_month, b_day)
td = datetime.date.today()
i = 1
birth = birth.replace(year=birth.year + 1)
while birth <= td:
    print("平手打ち(パシッ) {0}".format(i))
    birth = birth.replace(year=birth.year + 1)
    i += 1

入出力結果(Terminal)

$ ./sample.py
誕生年: 2000
誕生月: 12
誕生日: 27
平手打ち(パシッ) 1
平手打ち(パシッ) 2
平手打ち(パシッ) 3
平手打ち(パシッ) 4
平手打ち(パシッ) 5
平手打ち(パシッ) 6
平手打ち(パシッ) 7
平手打ち(パシッ) 8
平手打ち(パシッ) 9
平手打ち(パシッ) 10
平手打ち(パシッ) 11
平手打ち(パシッ) 12
$ ./sample.py
誕生年: 2000
誕生月: 12
誕生日: 28
平手打ち(パシッ) 1
平手打ち(パシッ) 2
平手打ち(パシッ) 3
平手打ち(パシッ) 4
平手打ち(パシッ) 5
平手打ち(パシッ) 6
平手打ち(パシッ) 7
平手打ち(パシッ) 8
平手打ち(パシッ) 9
平手打ち(パシッ) 10
平手打ち(パシッ) 11
$ ./sample.py
誕生年: 2000
誕生月: 12
誕生日: 26 
平手打ち(パシッ) 1
平手打ち(パシッ) 2
平手打ち(パシッ) 3
平手打ち(パシッ) 4
平手打ち(パシッ) 5
平手打ち(パシッ) 6
平手打ち(パシッ) 7
平手打ち(パシッ) 8
平手打ち(パシッ) 9
平手打ち(パシッ) 10
平手打ち(パシッ) 11
平手打ち(パシッ) 12
$

perlの場合。

sample.pl

コード(BBEdit)

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
use Time::Local;

print "誕生年: ";
chomp(my $b_year = <STDIN>);
print "誕生月: ";
chomp(my $b_month = <STDIN>);
print "誕生日: ";
chomp(my $b_day = <STDIN>);
my($day, $month, $year) = (gmtime)[3, 4, 5];
my $td = timegm(0,0,0,$day, $b_month - 1, $year);
my $age = 1;
while(timegm(0,0,0,$b_day,$b_month - 1, $b_year + $age - 1900) <= $td){
  print "平手打ち(パシッ) ${age}歳\n";
  $age++;
}

入出力結果(Terminal)

$ ./sample.pl
誕生年: 2000
誕生月: 12
誕生日: 26
平手打ち(パシッ) 1歳
平手打ち(パシッ) 2歳
平手打ち(パシッ) 3歳
平手打ち(パシッ) 4歳
平手打ち(パシッ) 5歳
平手打ち(パシッ) 6歳
平手打ち(パシッ) 7歳
平手打ち(パシッ) 8歳
平手打ち(パシッ) 9歳
平手打ち(パシッ) 10歳
平手打ち(パシッ) 11歳
平手打ち(パシッ) 12歳
$ ./sample.pl
誕生年: 2000
誕生月: 12
誕生日: 27
平手打ち(パシッ) 1歳
平手打ち(パシッ) 2歳
平手打ち(パシッ) 3歳
平手打ち(パシッ) 4歳
平手打ち(パシッ) 5歳
平手打ち(パシッ) 6歳
平手打ち(パシッ) 7歳
平手打ち(パシッ) 8歳
平手打ち(パシッ) 9歳
平手打ち(パシッ) 10歳
平手打ち(パシッ) 11歳
平手打ち(パシッ) 12歳
$ ./sample.pl
誕生年: 2000
誕生月: 12
誕生日: 28
平手打ち(パシッ) 1歳
平手打ち(パシッ) 2歳
平手打ち(パシッ) 3歳
平手打ち(パシッ) 4歳
平手打ち(パシッ) 5歳
平手打ち(パシッ) 6歳
平手打ち(パシッ) 7歳
平手打ち(パシッ) 8歳
平手打ち(パシッ) 9歳
平手打ち(パシッ) 10歳
平手打ち(パシッ) 11歳
$

0 コメント:

コメントを投稿