所以我试图使用三维数组打印出七段显示器:
我偶然发现了这篇文章,解释了七段显示器的阵列结构: http://cboard.cprogramming.com/c-programming/94140-seven-segment-display.html
这就是我想要打印出来的内容:
char array[9][10][20]
但我似乎无法弄清楚如何将其打印出来。此外,我很难理解如何打印七段数字中的一个(例如只是'5')?
我认为我需要三个for循环,但我不确定如何实现第三个循环。我发现的任何例子都只有2d,我可以得到,但最后一个维度让我感到困惑。
非常感谢任何帮助!
答案 0 :(得分:4)
不要过度思考它。这是一个例子:
#include <stdio.h>
char array[9][10][20] =
{
{" ### "," "," ### "," ### "," "," ### "," ### "," ### "," ### "," ### "},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{" "," "," ### "," ### "," ### "," ### "," ### "," "," ### "," ### "},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{" ### "," "," ### "," ### "," "," ### "," ### "," "," ### "," ### "},
};
int main(void) {
int ii, jj, kk;
// printing '3':
for(ii = 0; ii<9; ii++) {
printf("%s\n", array[ii][3]);
}
}
输出:
###
#
#
#
###
#
#
#
###
为什么这样做?
首先,让我们看看用于创建段的数组。这是一个9x10x20的字符数组。
事实上,虽然最后一个维度设置为20
,但它完全由长度为6
的字符串组成(5+1
用于终止'\0'
)。要打印出单个数字,您只想打印属于该数字的字符串 - 上面的例子对数字3
进行了打印。
如果要打印更长的数字字符串会变得更加棘手,因为您必须以每行为基础遍历每个数字。您可以通过以下方式使用数字12345
:
#include <stdio.h>
#include <string.h>
char array[9][10][20] =
{
{" ### "," "," ### "," ### "," "," ### "," ### "," ### "," ### "," ### "},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{" "," "," ### "," ### "," ### "," ### "," ### "," "," ### "," ### "},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{" ### "," "," ### "," ### "," "," ### "," ### "," "," ### "," ### "},
};
int main(void) {
int ii, jj, kk;
// printing '3':
for(ii = 0; ii<9; ii++) {
printf("%s\n", array[ii][3]);
}
// print '12345'
int numToPrint = 12345;
char numBuf[20];
sprintf(numBuf, "%d", numToPrint);
for(ii=0; ii<9; ii++) {
for(jj=0; jj < strlen(numBuf); jj++) {
printf("%s ", array[ii][numBuf[jj]-'0']);
}
printf("\n");
}
}
第二部分的输出:
### ### ###
# # # # # #
# # # # # #
# # # # # #
### ### ### ###
# # # # #
# # # # #
# # # # #
### ### ###
注意 - 我使用sprintf
函数将整数转换为字符串 - 然后我可以通过查看字符串中的每个字符并减去'0'
来找到每个相应的数字。在“真实”代码中,您当然会检查您是否没有超出数组边界,并且您可能希望包含一个小数点的字符串以及+
和-
符号来制作一些东西更灵活 - 但我希望你能从中得到这个想法。
答案 1 :(得分:0)
我不确定你想要什么,但这是一种显示想要号码的方法。
int numberToDisplay = 5;
for (int i = 0; i < 9; i++)
fprintf(stdout, "%s\n", array[i][numberToDisplay];
答案 2 :(得分:0)
在最高级别,我们有:
array =
{
{" ### "," "," ### "," ### "," "," ### "," ### "," ### "," ### "," ### "},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{" "," "," ### "," ### "," ### "," ### "," ### "," "," ### "," ### "},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{" ### "," "," ### "," ### "," "," ### "," ### "," "," ### "," ### "},
};
此数组有9个元素,对应于SSD数字ASCII艺术的行。例如:
array[2] =
{
"# #", /* 3rd row of digit '0' */
" #", /* 3rd row of digit '1' */
" #", /* 3rd row of digit '2' */
" #", /* 3rd row of digit '3' */
"# #", /* 3rd row of digit '4' */
"# ", /* 3rd row of digit '5' */
"# ", /* 3rd row of digit '6' */
" #", /* 3rd row of digit '7' */
"# #", /* 3rd row of digit '8' */
"# #", /* 3rd row of digit '9' */
};
索引此数组将选择要从中打印第3行的数字。因此,要打印多个数字,我们需要以下内容:
char array[9][10][20] =
{
{" ### "," "," ### "," ### "," "," ### "," ### "," ### "," ### "," ### "},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{"# #"," #"," #"," #","# #","# ","# "," #","# #","# #"},
{" "," "," ### "," ### "," ### "," ### "," ### "," "," ### "," ### "},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{"# #"," #","# "," #"," #"," #","# #"," #","# #"," #"},
{" ### "," "," ### "," ### "," "," ### "," ### "," "," ### "," ### "},
};
int main(void) {
int i, j;
char number[] = "345";
for (i = 0; i < 9; i++) { /* For each row...*/
for (j = 0; number[j] != '\0'; j++) { /* Print the nth row of each digit */
printf("%s ", array[i][number[j] - '0']);
}
putchar('\n');
}
return 0;
}