gpt4 book ai didi

c++ - 以 8x8 字符显示字母

转载 作者:行者123 更新时间:2023-11-28 02:37:37 39 4
gpt4 key购买 nike

我想修改这段代码,以便它可以将输出显示为 8x8 个字符。例如,如果我输入“gotcha”,输出应该是“GOTCHA”,如果我输入数字,输出将是空格字符。我不知道如何对每个输入字母使用 if,else 语句。现在我只对字母“a”、“A”、“b”、“B”使用 if-else 语句。还有一个问题,我可以将 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;
}

最佳答案

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

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

关于c++ - 以 8x8 字符显示字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26977992/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com