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 - 秀丸マクロ

秀丸マクロです。
0と1で表示するシンプルなバイナリクロックです。

 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
$bin[0] = "";
$bin[1] = "";
$bin[2] = "";
insert "\n\n\n";

while (1) {
    #i = 0;
    while (#i < 3) {
        call GetBinTime #i;
        if ($bin[#i] != $$return) {
            $bin[#i] = $$return;
            moveto 0,#i;
            beginsel;
            golineend;
            endsel;
            insert $bin[#i];
            gofileend;
        }
        #i = #i+1;
    }
}
endmacro;

GetBinTime:
    refreshdatetime;
    if (##1 == 0) {
        call Dec2Bin val(hour);
    } else if (##1 == 1) {
        call Dec2Bin val(minute);
    } else {
        call Dec2Bin val(second);
    }
    while (strlen($$return) < 6) {
        $$return = " "+$$return;
    }
return $$return;

Dec2Bin:
    $$str = "";
    if (##1!=0) {
    while (##1!=1) {
        $$str = str(##1%2)+$$str;
        ##1 = ##1/2;
    }
    $$str = "1"+$$str;
}
return $$str;

Index

Feed

Other

Link

Pathtraq

loading...