aboutsummaryrefslogtreecommitdiff
path: root/challenge-200/paulo-custodio/cpp/ch-2.cpp
blob: d7ae53b7883940a82ccbe39f74c4b587087cbc5b (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Challenge 200

Task 2: Seven Segment 200
Submitted by: Ryan J Thompson
A seven segment display is an electronic component, usually used to display
digits. The segments are labeled 'a' through 'g' as shown:


Seven Segment


The encoding of each digit can thus be represented compactly as a truth table:

my @truth = qw<abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg>;
For example, $truth[1] = �bc�. The digit 1 would have segments �b� and �c� enabled.

Write a program that accepts any decimal number and draws that number as a
horizontal sequence of ASCII seven segment displays, similar to the following:


-------  -------  -------
      |  |     |  |     |
      |  |     |  |     |
-------
|        |     |  |     |
|        |     |  |     |
-------  -------  -------
To qualify as a seven segment display, each segment must be drawn (or not drawn)
according to your @truth table.

The number "200" was of course chosen to celebrate our 200th week!
*/

#include <iostream>
#include <string>

#define A 1
#define B 2
#define C 4
#define D 8
#define E 16
#define F 32
#define G 64

int truth[10] = {
    A|B|C|D|E|F, B|C, A|B|D|E|G, A|B|C|D|G, B|C|F|G,
    A|C|D|F|G, A|C|D|E|F|G, A|B|C, A|B|C|D|E|F|G, A|B|C|F|G
};

void draw_number(int n) {
    std::string s = std::to_string(n);

    // draw a
    for (size_t i = 0; i < s.size(); i++) {
        int digit = s[i] - '0';
        if ((truth[digit] & A)==A)
            std::cout << " ####   ";
        else
            std::cout << "        ";
    }
    std::cout << std::endl;
    // draw f, b
    for (int j = 0; j < 2; j++) {
        for (size_t i = 0; i < s.size(); i++) {
            int digit = s[i] - '0';
            if ((truth[digit] & F)==F) std::cout << "#"; else std::cout << " ";
            std::cout << "    ";
            if ((truth[digit] & B)==B) std::cout << "#"; else std::cout << " ";
            std::cout << "  ";
        }
        std::cout << std::endl;
    }
    // draw g
    for (size_t i = 0; i < s.size(); i++) {
        int digit = s[i] - '0';
        if ((truth[digit] & G)==G)
            std::cout << " ####   ";
        else
            std::cout << "        ";
    }
    std::cout << std::endl;
    // draw e, c
    for (int j = 0; j < 2; j++) {
        for (size_t i = 0; i < s.size(); i++) {
            int digit = s[i] - '0';
            if ((truth[digit] & E)==E) std::cout << "#"; else std::cout << " ";
            std::cout << "    ";
            if ((truth[digit] & C)==C) std::cout << "#"; else std::cout << " ";
            std::cout << "  ";
        }
        std::cout << std::endl;
    }
    // draw d
    for (size_t i = 0; i < s.size(); i++) {
        int digit = s[i] - '0';
        if ((truth[digit] & D)==D)
            std::cout << " ####   ";
        else
            std::cout << "        ";
    }
    std::cout << std::endl;
}

int main(int argc, char* argv[]) {
    argv++; argc--;
    if (argc == 0) {
        std::cerr << "usage: ch-2 num" << std::endl;
        exit(EXIT_FAILURE);
    }

    draw_number(atoi(argv[0]));
}