C++ 以8x8个字符显示字母

C++ 以8x8个字符显示字母,c++,C++,我希望修改这段代码,以便它可以将输出显示为8x8个字符。例如,如果我键入“gotcha”,则输出应为“gotcha”,如果我键入数字,则输出将为空格字符。我不知道如何对每个输入字母使用if,else语句。现在我只对字母“a”、“a”、“b”、“b”使用if-else语句。还有一点,我可以使用void进行char\u-led显示屏[] #include <iostream> #include <cstring> using namespace std; int main(

我希望修改这段代码,以便它可以将输出显示为8x8个字符。例如,如果我键入“gotcha”,则输出应为“gotcha”,如果我键入数字,则输出将为空格字符。我不知道如何对每个输入字母使用if,else语句。现在我只对字母“a”、“a”、“b”、“b”使用
if
-
else
语句。还有一点,我可以使用
void
进行
char\u-led显示屏[]

#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;
}
#包括
#包括
使用名称空间std;
int main()
{
INTA,b;
字符c;
字符led_显示[]=
{
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,0x00,//1
};
字符[10];

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

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];

我会将
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";
}
charch=toupper(字母[i]);

cout要获得与硬件相关的问题(如LED段)的帮助,您可以通过添加
embedded
标签获得更多的受众。