以8x8字符显示字母

时间:2014-11-17 17:09:05

标签: c++

我正在修改此代码,以便它可以将输出显示为8x8字符。例如,如果我键入“gotcha”,则输出应为“GOTCHA”,如果输入数字,则输出将为空格字符。我不知道如何使用if,else语句为每个输入字母。现在我只使用if - else语句来表示字母“a”,“A”,“b”,“B”。还有一个,我可以void使用char_led display[]

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    int a, b;
    char c;
    char led_display[] =
    {
        0x3c, 0x24, 0x24, 0x7e, 0x62, 0x62, 0x62, 0x00,//A
        0x7c, 0x24, 0x24, 0x3e, 0x32, 0x32, 0x7e, 0x00,//B
        0x3e, 0x22, 0x20, 0x60, 0x60, 0x62, 0x7e, 0x00,//C
        0x7e, 0x22, 0x22, 0x32, 0x32, 0x32, 0x7e, 0x00,//D
        0x3e, 0x20, 0x20, 0x78, 0x60, 0x60, 0x7e, 0x00,//E
        0x3e, 0x20, 0x20, 0x78, 0x60, 0x60, 0x60, 0x00,//F
        0x3e, 0x22, 0x20, 0x6e, 0x62, 0x62, 0x7e, 0x00,//G
        0x24, 0x24, 0x24, 0x7e, 0x62, 0x62, 0x62, 0x00,//H
        0x3e, 0x08, 0x08, 0x18, 0x18, 0x18, 0x3E, 0x00,//I
        0x1c, 0x08, 0x08, 0x0C, 0x0C, 0x4C, 0x7c, 0x00,//J
        0x24, 0x24, 0x28, 0x70, 0x68, 0x68, 0x66, 0x00,//K
        0x20, 0x20, 0x20, 0x60, 0x60, 0x62, 0x7e, 0x00,//L
        0x36, 0x3e, 0x2a, 0x62, 0x62, 0x62, 0x62, 0x00,//M
        0x32, 0x2a, 0x2a, 0x6a, 0x6a, 0x66, 0x62, 0x00,//N
        0x3e, 0x22, 0x22, 0x62, 0x62, 0x62, 0x7e, 0x00,//O
        0x3e, 0x22, 0x22, 0x7e, 0x60, 0x60, 0x60, 0x00,//P
        0x3e, 0x22, 0x22, 0x62, 0x6a, 0x64, 0x7a, 0x00,//Q
        0x3e, 0x22, 0x22, 0x7e, 0x68, 0x64, 0x66, 0x00,//R
        0x3c, 0x24, 0x20, 0x3c, 0x0c, 0x4c, 0x7c, 0x00,//S
        0x3e, 0x08, 0x08, 0x18, 0x18, 0x18, 0x18, 0x00,//T
        0x22, 0x22, 0x22, 0x62, 0x62, 0x62, 0x7e, 0x00,//U
        0x22, 0x22, 0x22, 0x64, 0x68, 0x70, 0x60, 0x00,//V
        0x22, 0x22, 0x22, 0x6a, 0x6a, 0x7e, 0x76, 0x00,//W
        0x42, 0x24, 0x18, 0x3c, 0x64, 0x64, 0x66, 0x00,//X
        0x66, 0x24, 0x14, 0x0c, 0x0c, 0x18, 0x30, 0x00,//Y
        0x7e, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x7e, 0x00,//Z
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//1
    };

    char letter[10];
    cout << "Enter a string <max. 9 letter>: ";
    cin.getline(letter, 10);

    for (int i = 0; i < strlen(letter); i++)
    {
        char ch = letter[i];
        ch = toupper(letter[i]);
        cout << " " << ch;

        if ((strcmp(letter, "a") == 0) || (strcmp(letter, "A") == 0))
        {
            for (a = 0; a < 8; a++)
            {
                cout << endl;

                for (b = 0; b < 8; b++)
                {
                    c = led_display[a] >> (7 - b) & 1;

                    if (c == 0)
                    {
                        cout << " ";
                    }
                    else
                    {
                        cout << char{ 0xDB };
                    }
                }
            }
        }
        else if ((strcmp(letter, "b") == 0) || (strcmp(letter, "B") == 0))
        {
            for (a = 8; a < 16; a++)
            {
                cout << endl;

                for (b = 0; b < 8; b++)
                {
                    c = led_display[a] >> (7 - b) & 1;

                    if (c == 0)
                    {
                        cout << " ";
                    }
                    else
                    {
                        cout << char{ 0xDB };
                    }
                }
            }
        }
    }

    return 0;
}

2 个答案:

答案 0 :(得分:1)

首先,我认为您需要更改数据结构:

const unsigned int SEGMENTS_IN_DIGIT = 8;
struct Segment_Pattern
{
  uint8_t segments[SEGMENTS_IN_DIGIT];
};

一个简单的实现是一个查找表,或者在这种情况下是一个数组:

Segment_Pattern letter_patterns[] =
{
  {0x3c,0x24,0x24,0x7e,0x62,0x62,0x62,0x00}, // A
  // ...
};

您可以通过执行计算来查找字母:

char c = toupper(letters[i]);
unsigned int index = c - 'A';
Segment Pattern const * const p_pattern = &letter_patterns[index];

答案 1 :(得分:1)

我会将for循环的内部更改为这样的内容:

char ch = toupper(letter[i]);
cout << " " << ch;

if (ch >= 'A' && ch <= 'Z')
{
    const int intermediate = (ch - 'A') * 8;
    for (char* i = led_display + intermediate; i < led_display + intermediate + 8; ++i){
        cout << *i << ' ';
    }
}
else
{
    cout << "space";
}

请注意,您正在使用&#34; 8&#34;为了大踏步。

if语句将清除非字符条目。