Created
January 4, 2024 03:29
-
-
Save kujirahand/318e7773818ab03c416dde95726639fd to your computer and use it in GitHub Desktop.
Tcl/TkでFizzBuzz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# メインウィンドウの初期化 --- (*1) | |
wm title . "FizzBuzz" | |
wm geometry . 600x400 | |
# fizzbuzz関数を定義 --- (*2) | |
proc fizzbuzz { n } { | |
# 変数$nがFizzかBuzzかを判定 --- (*2) | |
set is_fizz [expr {($n % 3) == 0}] | |
set is_buzz [expr {($n % 5) == 0}] | |
# 判定結果に応じて結果を返す --- (*3) | |
if {$is_fizz && $is_buzz} { return "FizzBuzz" } | |
if {$is_fizz} { return "Fizz" } | |
if ($is_buzz) { return "Buzz" } | |
return $n | |
} | |
# リストボックスに表示するためのリストを作成 --- (*4) | |
set a_list {} | |
# fizzbuzz関数を使って1から100までの結果を取得 --- (*5) | |
for {set i 1} {$i <= 100} {incr i} { | |
set result [fizzbuzz $i] | |
# 画面に結果を表示 | |
puts $result | |
# リストに結果を追加 | |
lappend a_list $result | |
} | |
# リストボックスの作成 --- (*6) | |
listbox .list -listvariable a_list -width 600 -height 400 | |
pack .list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment