2012年8月18日土曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 2章(数値), 2.5(練習問題)、この本の著者の年齢を解いてみる。

その他参考書籍

この本の著者の年齢

コード(TextWrangler)

sample.rb

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

td = Time.new
birth = td - 10.25 * (10 ** 8)
puts "今現在の時刻: #{td}"
puts "生まれた時刻: #{birth}"
age =td.year - birth.year
age -= 1 if td.month < birth.month || 
  (td.month == birth.month && td.day < birth.day)

puts "著者の年齢: #{age}歳"
puts "閏年を考えずに割り算で求めた場合: #{1025 * (10 ** 6) / (365 * 24 * 60 * 60)}歳"

入出力結果(Terminal)

$ ./sample.rb
今現在の時刻: Sat Aug 18 16:29:40 +0900 2012
生まれた時刻: Mon Feb 25 06:16:20 +0900 1980
著者の年齢: 32歳
閏年を考えずに割り算で求めた場合: 32歳
$

0 コメント:

コメントを投稿