C语言模拟输出七段显示译码器
受http://jyywiki.cn/pages/OS/2022/demos/seven-seg.py
的启发,结合数电课设的需求,写出了以下代码,非常丑陋的C语言实现。
人生苦短,我用Python
/*+++++++++++++++++++++++++++++++++
@Author: ll
+++++++++++++++++++++++++++++++++*/
#define DEV
//#define DEBUG
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INF 0x3f3f3f3f
#define EPS 1e-6
const int MAX_N = 10000 + 5;
#define SEVENSEG \
"\033[2J\033[1;1f\n\
AAAAAAAAA\n\
FF BB\n\
FF BB\n\
FF BB\n\
FF BB\n\
GGGGGGGGGG\n\
EE CC\n\
EE CC\n\
EE CC\n\
EE CC\n\
DDDDDDDDD"
int A, B, C, D, E, F, G;
#define LIGHT "\033[40;31m|\033[0m"
const int font[10] = {0x7E, 0x30, 0x6D, 0x79, 0x33,
0x5B, 0x5F, 0x70, 0x7F, 0x7B};
int main(int argc, char* argv[]) {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int num;
scanf("%d", &num);
num = font[num];
G = num & 1;
num >>= 1;
F = num & 1;
num >>= 1;
E = num & 1;
num >>= 1;
D = num & 1;
num >>= 1;
C = num & 1;
num >>= 1;
B = num & 1;
num >>= 1;
A = num & 1;
num >>= 1;
A++;
B++;
C++;
D++;
E++;
F++;
G++;
#ifdef DEBUG
printf("%d %d %d %d %d %d %d\n", A, B, C, D, E, F, G);
#endif
char ch[MAX_N] = SEVENSEG;
for (int i = 0; i < strlen(ch); i++) {
switch (ch[i]) {
case 'A':
ch[i] = A;
break;
case 'B':
ch[i] = B;
break;
case 'C':
ch[i] = C;
break;
case 'D':
ch[i] = D;
break;
case 'E':
ch[i] = E;
break;
case 'F':
ch[i] = F;
break;
case 'G':
ch[i] = G;
break;
default:
break;
}
}
#ifdef DEV
for (int i = 0; i < strlen(ch); i++) {
if (ch[i] == 2) {
printf(LIGHT);
} else {
printf("%c", ch[i]);
}
}
#endif
system("pause");
return 0;
}
License:
CC BY 4.0