challenge バイナリクロック

 時刻を二進数相当の表現で出力する時計アプリケーションを書いてください。
 20:18の場合,例えば以下の様な出力をするイメージです。

出力例:
 ■□■□□
□■□□■□
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#! c:\ruby\bin\ruby.exe -Ks

String.class_eval do |string|
    def words
        self.split(//)
    end
    def fix_width(width, padding)
        (self.words.size > width) ? self : (padding * (width - self.words.size) + self)
    end
end

Fixnum.class_eval do |fixnum|
    alias :to_s_orig :to_s
    def to_s(base, width)
        binary = self.to_s_orig(base).fix_width(width, "0")
    end
end

class BinaryClock
    attr_accessor :now
    def initialize
        self.now = Time.now
    end
    def print
        output(self.now.hour.to_s(2, 5))
        output(self.now.min.to_s(2, 6))
    end
private
    def output(binary)
        puts binary.words.map { |f| f == "0" ? "□" : "■" }.join.fix_width(6, " ")
    end
end

BinaryClock.new.print

Posted feedbacks - HSP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#uselib "user32.dll"
#func   PostMessage   "PostMessageA"   int,int,int,sptr
#cfunc  GetWindowLong "GetWindowLongA" int,int
#func   SetWindowLong "SetWindowLongA" int,int,int

#undef  BITNUM
#define global ctype BITNUM(%1) (1 << (%1))

#define WIN_HEIGHT 30
#define CELLSIZE 10
#define CX_MARGIN 1
#define CY_MARGIN 1

    cx = 60 + CX_MARGIN * 2
    cy = WIN_HEIGHT + CY_MARGIN * 2
    bgscr IDW_MAIN, cx, cy, 2
    SetWindowLong hwnd, -20, ( GetWindowLong(hwnd, -20) | 0x80 )
    
    gsel IDW_MAIN, 2
    onclick gosub *OnMove
    onkey   gosub *OnKeyProc
    
    goto *mainlp
    
*mainlp
    gosub *LCalcData
    gosub *LRedraw
    await 10
    goto *mainlp
    
*LCalcData
    nTime = gettime(4), gettime(5), gettime(6)
    return
    
*LRedraw
    redraw 2
    
    color 192, 192, 192 : boxf
    color
    px1 = CX_MARGIN - 1
    py1 = CY_MARGIN - 1
    px2 = ginfo(12) - CX_MARGIN
    py2 = ginfo(13) - CY_MARGIN
    boxf px1, py1, px2, py2
    
    foreach nTime
        h = (192 / length(nTime)) * cnt
        for i, 0, 6
            if ( nTime(cnt) & BITNUM(5 - i) ) {
                s = 255 - ((255 / 10) * i)
                v = 255 - ((255 / 10) * i)
                hsvcolor h, s, v
            } else {
                color 255, 255, 255
            }
            
            px1 = CX_MARGIN + (  i * CELLSIZE)
            py1 = CY_MARGIN + (cnt * CELLSIZE)
            px2 = CX_MARGIN + ((  i + 1) * CELLSIZE) - 2
            py2 = CY_MARGIN + ((cnt + 1) * CELLSIZE) - 2
            boxf px1, py1, px2, py2
        next
    loop
    
    redraw 1
    return
    
*OnMove
    if ( wparam == 1 ) {
        sendmsg hwnd, 0x00A1, 2, 0
    }
    return
    
*OnKeyProc
    if ( iparam == 27 ) {        // [Esc]
        gosub *LQuit
    }
    return
    
*LQuit
    PostMessage hwnd, 0x0010, 0, 0
    return

Index

Feed

Other

Link

Pathtraq

loading...