開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 12章(新しいクラスのオブジェクト), 12.2(練習問題)ハッピーバースデー を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
ハッピーバースデー
コード(TextWrangler)
sample.rb
#!/usr/bin/env ruby1.9
# -*- coding: utf-8 -*-
print "生まれた年: "
birth_year = gets.chomp.to_i
print "生まれた月: "
birth_month = gets.chomp.to_i
print "生まれた日: "
birth_day = gets.chomp.to_i
birth_date = Time.gm(birth_year, birth_month, birth_day)
td = Time.new
age = 1
while Time.gm(birth_year + age, birth_month, birth_day) <= td
puts "平手打ち(パシッ): #{age}歳"
age += 1
end
puts "誕生日: #{birth_date}"
puts "今日: #{td}"kamimuras
入出力結果(Terminal)
$ ./sample.rb 生まれた年: 2000 生まれた月: 11 生まれた日: 20 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 誕生日: 2000-11-20 00:00:00 UTC 今日: 2012-11-21 18:04:56 +0900 $ ./sample.rb 生まれた年: 2000 生まれた月: 11 生まれた日: 21 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 誕生日: 2000-11-21 00:00:00 UTC 今日: 2012-11-21 18:05:00 +0900 $ ./sample.rb 生まれた年: 2000 生まれた月: 11 生まれた日: 22 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 誕生日: 2000-11-22 00:00:00 UTC 今日: 2012-11-21 18:05:06 +0900 $
ちなみにJavaScriptの場合。
コード(TextWrangler)
var birth_year = parseInt($('#t0').val());
var birth_month = parseInt($('#t1').val()) - 1
var birth_day = parseInt($('#t2').val());
var birth_date = new Date(birth_year, birth_month, birth_day);
var date = birth_date;
var td = new Date();
var age = 1;
var result = "";
while(date.setYear(birth_year + age) <=td){
result += "平手打ち(パシッ): " + age + "歳\n";
age++;
}
result += "誕生日: " + birth_date + "\n今日: " + td;
$('#pre0').text(result);
pythonの場合。
sample.py
コード(TextWrangler)
#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-
import datetime
print("生まれた年を入力: ", end="")
birth_year = int(input())
print("生まれた月を入力: ", end="")
birth_month = int(input())
print("生まれた日を入力: ", end="")
birth_day = int(input())
birth_date = datetime.date(birth_year, birth_month, birth_day)
td = datetime.date.today()
age = 1
while birth_date.replace(year = birth_year + age) <= td:
print("平手打ち(パシッ): {0}歳".format(age))
age += 1
print("誕生日: {0}".format(birth_date))
print("今日の日付: {0}".format(td))
入出力結果(Terminal)
$ ./sample.py 生まれた年を入力: 2000 生まれた月を入力: 11 生まれた日を入力: 20 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 誕生日: 2000-11-20 今日の日付: 2012-11-21 $ ./sample.py 生まれた年を入力: 2000 生まれた月を入力: 11 生まれた日を入力: 21 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 平手打ち(パシッ): 12歳 誕生日: 2000-11-21 今日の日付: 2012-11-21 $ ./sample.py 生まれた年を入力: 2000 生まれた月を入力: 11 生まれた日を入力: 22 平手打ち(パシッ): 1歳 平手打ち(パシッ): 2歳 平手打ち(パシッ): 3歳 平手打ち(パシッ): 4歳 平手打ち(パシッ): 5歳 平手打ち(パシッ): 6歳 平手打ち(パシッ): 7歳 平手打ち(パシッ): 8歳 平手打ち(パシッ): 9歳 平手打ち(パシッ): 10歳 平手打ち(パシッ): 11歳 誕生日: 2000-11-22 今日の日付: 2012-11-21 $
0 コメント:
コメントを投稿