0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Juliaで学ぶプログラミング入門 簡単なプログラムの製作(曜日を求める)

Last updated at Posted at 2022-12-04

練習 プログラムを作ってみよう

ここまでの内容をベースに曜日を求めるプログラムを作ってみましょう。

ツェラーの公式から曜日を求めてみましょう。

説明されていない関数について簡単に説明します。

参考:使用する組み込み算術関数
div(a,b):a/bの商
出力関数
println(a):aを標準(通常は画面)出力

# 曜日を求める関数を定義
function getDayOfWeek(year, month, day)
  # Zellerの公式によって、曜日を求める
  # 月の修正
  if month <= 2
    year -= 1
    month += 12
  end
  # 曜日を求める
  w = (year + div(year, 4) - div(year, 100) + div(year, 400) + div(13 * month + 8, 5) + day) % 7
  println("w:",w)  
  # 曜日を文字列で返す
  return ["日", "月", "火", "水", "木", "金", "土"][w+1]
end

# 曜日を求める
println(getDayOfWeek(2022, 12, 3)) # 2022年12月3日の曜日
println(getDayOfWeek(2022, 12, 4)) # 2022年12月4日の曜日
println(getDayOfWeek(2022, 12, 5)) # 2022年12月5日の曜日
println(getDayOfWeek(2022, 12, 6)) # 2022年12月6日の曜日
println(getDayOfWeek(2022, 12, 7)) # 2022年12月7日の曜日
println(getDayOfWeek(2022, 12, 8)) # 2022年12月8日の曜日
println(getDayOfWeek(2022, 12, 9)) # 2022年12月9日の曜日

結果は

土
日
月
火
水
木
金

プログラムでは、与えられた年月日から曜日を求めるために、Zellerの公式を使用しています。Zellerの公式とは、曜日を求めるための公式の一つで、閏年の補正をして割り出します。西暦年月日から曜日を求めることができます。

参考図書

1から始めるプログラミング

Juliaプログラミングクックブック

天才プログラマー タンメイが教える Julia超入門

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?