검색결과 리스트
글
문제4 LCD디스플레이
알고리즘.데이터구조
2011. 2. 18. 11:40
LCD 디스플레이 형태로 숫자를 출력하되 크기 입력 s에 의해 출력하는 숫자의 크기를 조절할 수 있다.
이 때 각 숫자는 s+2개의 열과 2s+3 개의 행 크기를 차지하게 된다. 행문자는 ' | ', 열 ' -' 를 사용.
각 자리 수 사이에는 1칸씩 공백을 둔다.
입력 : s n (크기 s, 출력할 정수 n)
출력 : LCD 디스플레이 형태로 n을 출력
문제해결
두 가지 방법으로 해결법을 찾아 보았다. 첫번째는 0 ~ 9 까지 각 수를 인자 s에 따라 출력하는
일반적인 방법이다. 두번 째는 LCD 패널을 7가지 구성요소로 분리하고, 각 요소를 7개의 함수로
만들고, 0~9중 해당하는 수가 입력된 경우에만 해당 요소를 출력하는 방법이다.
/*
입력 : s(크기) , n(출력할 수)
출력 : LCD Display 형태로 n를 출력
조건 : s+2 열, 2s+3 행을 사용
*/
#include <stdio.h>
#include <stdlib.h>
#define ROW(s) (2*s + 3)
#define COL(s) (s+2)
#define ROW_CHAR '@'
#define COL_CHAR '@'
#define SPACE ' '
void f0(int n, int s);
void f1(int n, int s);
void f2(int n, int s);
void f3(int n, int s);
void f4(int n, int s);
void f5(int n, int s);
void f6(int n, int s);
void lcd_display(int s, char *buf);
int main (int argc, char const* argv[])
{
int s;
char buf[10];
scanf("%d %s", &s, buf);
lcd_display(s, buf);
return 0;
}
void lcd_display(int s, char *buf) {
int i;
char *p = buf;
if (s == 0 && atoi(buf) == 0) {
return ;
}
p = buf;
while (*p != '\n' && *p != '\0') {
f0(*p-'0', s);
p++;
}
putchar('\n');
for (i = 0; i < (ROW(s)-1)/2; i++) {
p = buf;
while (*p != '\n' && *p != '\0') {
f1(*p-'0', s);
p++;
}
putchar('\n');
}
p = buf;
while (*p != '\n' && *p != '\0') {
f3(*p-'0', s);
p++;
}
putchar('\n');
for (i = 0; i < (ROW(s)-1)/2; i++) {
p = buf;
while (*p != '\n' && *p != '\0') {
f4(*p-'0', s);
p++;
}
putchar('\n');
}
p = buf;
while (*p != '\n' && *p != '\0') {
f6(*p-'0', s);
p++;
}
putchar('\n');
}
// 가장 첫행
void f0(int n, int s) {
int i;
char ch = COL_CHAR;
if (n == 1 || n == 4){
ch = SPACE;
}
for (i = 0; i < COL(s)-1; i++) {
putchar(ch);
}
putchar(SPACE);
}
void f1(int n, int s) {
int i;
char ch = ROW_CHAR;
if (n == 1 || n == 2 || n == 3 || n == 7) {
ch = SPACE;
}
putchar(ch);
for (i = 0; i < COL(s)-3; i++) {
putchar(SPACE);
}
f2(n, s);
}
void f2(int n, int s) {
char ch = ROW_CHAR;
if (n == 5 || n == 6){
ch = ' ';
}
putchar(ch);
putchar(SPACE);
}
// 중간 행
void f3(int n, int s) {
int i;
char ch = COL_CHAR;
if (n == 0 || n == 1 || n == 7){
ch = ' ';
}
for (i = 0; i < COL(s)-1; i++) {
putchar(ch);
}
putchar(SPACE);
}
void f4(int n, int s) {
int i;
char ch = ROW_CHAR;
if (n == 1 || n == 3 || n == 4 || n == 5 || n == 7 || n == 9){
ch = SPACE;
}
putchar(ch);
for (i = 0; i < COL(s)-3; i++) {
putchar(SPACE);
}
f5(n, s);
}
void f5(int n, int s) {
char ch = ROW_CHAR;
if (n == 2){
ch = ' ';
}
putchar(ch);
putchar(' ');
}
// 끝행
void f6(int n, int s) {
int i;
char ch = COL_CHAR;
if (n == 1 || n == 4 || n == 7){
ch = SPACE;
}
for (i = 0; i < COL(s)-1; i++) {
putchar(ch);
}
putchar(SPACE);
}
'알고리즘.데이터구조' 카테고리의 다른 글
| HMAC-SHA1 (0) | 2011.03.11 |
|---|---|
| 문제5 그래픽편집기 (0) | 2011.02.18 |
| 문제4 LCD디스플레이 (0) | 2011.02.18 |
| 문제3 여행 (0) | 2011.02.18 |
| 문제2 지뢰찾기 (0) | 2011.02.18 |
| 문제1 3n+1문제 (0) | 2011.02.18 |
lcd_display.c