開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 12章(章全部で復習), 12.2(練習問題)ハッピーバースデー を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
ハッピーバースデー
コード(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
td = Time.new
age = 1
while Time.gm( b_year + age, b_month, b_day) <= td
puts "平手打ち(パシッ): #{age}歳"
age += 1
end
入出力結果(Terminal)
$ ./sample.rb 誕生年: 2000 誕生月: 3 誕生日: 10 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 平手打ち(パシッ): 13歳 $ ./sample.rb 誕生年: 2000 誕生月: 3 誕生日: 11 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 平手打ち(パシッ): 13歳 $ ./sample.rb 誕生年: 2000 誕生月: 3 誕生日: 12 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 $
ちなみにJavaScriptの場合。
コード(BBEdit)
var b_year = parseInt($('#b_year').val(), 10),
b_month = parseInt($('#b_month').val(), 10),
b_day = parseInt($('#b_day').val(), 10),
td = new Date(),
age = 1,
result = "",
d = new Date(b_year + age, b_month - 1, b_day);
while ( d <= td ) {
result += "平手打ち(パシッ): " + age + "歳\n";
age += 1;
d.setFullYear( b_year + age );
}
$('#pre2').text(result);
誕生日(西暦)
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("誕生日: "))
b = datetime.date(b_year, b_month, b_day)
td = datetime.date.today()
age = 1
d = b
while d.replace(year=b.year + age) <= td:
print("平手打ち(パシッ): {0}歳".format(age))
age += 1
入出力結果(Terminal)
$ ./sample.py 誕生年: 2000 誕生月: 3 誕生日: 10 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 平手打ち(パシッ): 13歳 $ ./sample.py 誕生年: 2000 誕生月: 3 誕生日: 11 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 平手打ち(パシッ): 13歳 $ ./sample.py 誕生年: 2000 誕生月: 3 誕生日: 12 平手打ち(パシッ): 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 $age = 1;
my $td = timelocal(0, 0, 0, (localtime)[3, 4, 5]);
while (timelocal(0, 0, 0, $b_day, $b_month - 1, $b_year + $age - 1900 ) <= $td ) {
print "平手打ち(パシッ): ${age}歳\n";
$age += 1;
}
入出力結果(Terminal)
$ ./sample.pl 誕生年: 2000 誕生月: 3 誕生日: 10 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 平手打ち(パシッ): 13歳 $ ./sample.pl 誕生年: 2000 誕生月: 3 誕生日: 11 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 平手打ち(パシッ): 13歳 $ ./sample.pl 誕生年: 2000 誕生月: 3 誕生日: 13 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 $
0 コメント:
コメントを投稿