Problem Description
编写程序,输入一个整数,使用字符模拟七段显示器来输出该数。
计算器、电子手表和其它电子设备经常依靠七段显示器进行数值的输出。为了组成数字,这类设备需要“打开”7个显示段中的某些部分,同时“关闭”其它部分:
需要设置一个数组来保存显示0到9数字时需要“打开”和“关闭”的显示段。各显示段的编号如下:
Input Description
输入数据有多组,每组占一行,为一个不超过10位的正整数。
Output Description
对于每组输入数据,使用字符模拟七段显示器来输出该整数,每个数字之间留一个空格。
Sample Input
2020
Sample Output
_ _ _ _ _| | | _| | | |_ |_| |_ |_|
#include<stdio.h>
#include <string.h>
#define MAX_DIGIT 10
const int print_nu[10][9] = {
{7,0,7,5,7,1,4,3,2},
{7,7,7,7,7,1,7,7,2},
{7,0,7,7,6,1,4,3,7},
{7,0,7,7,6,1,7,3,2},
{7,7,7,5,6,1,7,7,2},
{7,0,7,5,6,7,7,3,2},
{7,0,7,5,6,7,4,3,2},
{7,0,7,7,7,1,7,7,2},
{7,0,7,5,6,1,4,3,2},
{7,0,7,5,6,1,7,3,2}
};
int nums[MAX_DIGIT];
void printf_char(int ch1);
void show_char(char *ch, int temp);
int main()
{
char ch[10];
int cnt = 0;
int temp = 0;
while (scanf("%s", &ch) != EOF)
{
temp = strlen(ch);
for (int a = 0; a < temp; a++)
{
if (cnt < 10 && ch[a] >= '0' && ch[a] <= '9' && ch[a] != '\n')
nums[a] = ch[a] - '0';
else
break;
}
show_char(ch, temp);
}
}
void show_char(char *ch, int temp)
{
int i = 0, j = 0;
for (i = 0; i < 3; i++)
{
for (int ten = 0; ten < temp; ten++)
{
for (j = i * 3; j < 3 * (i + 1); j++)
{
int n = nums[ten];
printf_char(print_nu[n][j]);
}
printf(" ");
}
printf("\n");
}
}
void printf_char(int ch1) {
switch (ch1) {
case 0:
printf("_");
break;
case 1:
printf("|");
break;
case 2:
printf("|");
break;
case 3:
printf("_");
break;
case 4:
printf("|");
break;
case 5:
printf("|");
break;
case 6:
printf("_");
break;
case 7:
printf(" ");
break;
}
}