A Solution For The "LCD Display" Problem
Join the DZone community and get the full member experience.
Join For FreeA solution for the "LCD Display" problem.
Problem description:
http://acm.uva.es/p/v7/706.html
Author: Joana Matos Fonseca da Trindade
Date: 2008.03.09
/*
* Solution for the "LCD Display" problem.
* UVa ID: 706
*/
#include
#include
#include
#define MAX_SIZE 9
int main (int argc, const char * argv[]) {
/* number of vertical or horizontal segments in each digit */
int s;
/* the number to print */
char digitString[MAX_SIZE];
/*
* LED representation for each number, according to
* the following convention:
*
* -0-
* | |
* 2 1
* | |
* -3-
* | |
* 5 4
* | |
* -6-
*
*/
const char conversionTable[10][7] = {
/* 0 1 2 3 4 5 6 */
/* 0 */ '-','|','|',' ','|','|','-',
/* 1 */ ' ','|',' ',' ','|',' ',' ',
/* 2 */ '-','|',' ','-',' ','|','-',
/* 3 */ '-','|',' ','-','|',' ','-',
/* 4 */ ' ','|','|','-','|',' ',' ',
/* 5 */ '-',' ','|','-','|',' ','-',
/* 6 */ '-',' ','|','-','|','|','-',
/* 7 */ '-','|',' ',' ','|',' ',' ',
/* 8 */ '-','|','|','-','|','|','-',
/* 9 */ '-','|','|','-','|',' ','-',
};
/* iterators */
int i, j, k;
while(scanf("%d %s", &s, &digitString) != EOF) {
/* 0, ends the program */
if (!s) {
return 0;
}
int n = strlen(digitString);
int digit;
for (i = 0; i < 2*s+3; i++) {
for (j = 0; j < n; j ++) {
digit = digitString[j] - '0';
/* upper, middle and lower parts */
if ((i % (s + 1)) == 0) {
printf(" ");
for (k = 0; k < s; k++) {
printf("%c", conversionTable[digit][(i / (s + 1)) * 3]);
}
printf(" ");
}
/* between upper and middle parts */
if (i > 0 && i < (s + 1)) {
printf("%c", conversionTable[digit][2]);
for (k = 0; k < s; k++) {
printf(" ");
}
printf("%c", conversionTable[digit][1]);
}
/* between middle and lower parts */
if (i > (s + 1) && i < (2*s + 2)) {
printf("%c", conversionTable[digit][5]);
for (k = 0; k < s; k++) {
printf(" ");
}
printf("%c", conversionTable[digit][4]);
}
/* if not the last number */
if (j != n-1)
printf(" ");
}
printf("\n");
}
printf("\n");
}
return 0;
}
Opinions expressed by DZone contributors are their own.
Comments