Vinícius Oliveira

I love coding.

Programming Challenges - LCD Display

Problem LCD Display

A friend of yours has just bought a new computer. Before this, the most powerful
machine he ever used was a pocket calculator. He is a little disappointed because he
liked the LCD display of his calculator more than the screen on his new computer! To
make him happy, write a program that prints numbers in LCD display style.

Input

The input file contains several lines, one for each number to be displayed. Each line
contains integers s and n, where n is the number to be displayed (0 ≤ n ≤ 99, 999, 999)
and s is the size in which it shall be displayed (1 ≤ s ≤ 10). The input will be terminated
by a line containing two zeros, which should not be processed.

Output

Print the numbers specified in the input file in an LCD display-style using s “-” signs
for the horizontal segments and s “|” signs for the vertical ones. Each digit occupies
exactly s + 2 columns and 2s + 3 rows. Be sure to fill all the white space occupied by
the digits with blanks, including the last digit. There must be exactly one column of
blanks between two digits.
Output a blank line after each number. You will find an example of each digit in the
sample output below.

Solution

(lcd_display.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <string.h>

char data_of_numbers[10][5][3];

void init_data_of_numbers();
void print_char_with_count (int, char);
void print_specific_char_for_number(int, char);
void print_char_formatted_lcd(int, char *, int);
void print_space_between_number(int, char *);

int main (){
  init_data_of_numbers();
  int s;
  char number[100];
  while (1){
      scanf (" %d %s",&s,number);
      if (s == 0 && number[0] == '0') break;
      for (int j = 0; j < 5; j++){
          if (j == 0 || j == 2 || j == 4){
              print_char_formatted_lcd(s,number, j);
              printf("\n");
          }else{
              for (int k = 0; k < s; k++){
                  print_char_formatted_lcd(s,number,j);
                 printf("\n");
              }
          }
      }
      printf("\n");
  }

  return 0;
}

void print_char_formatted_lcd(int count , char * number, int j){
  for (int i = 0; i < strlen(number); i++){
      int pos_number = number[i] - '0';
      for (int x = 0; x < 3; x++){
          char choose = data_of_numbers[pos_number][j][x];
          print_specific_char_for_number(count,choose);
      }
      print_space_between_number(i, number);
  }
}

void print_space_between_number(int i , char * number){
  if (i + 1 != strlen(number)){
      printf(" ");
  }
}

void print_specific_char_for_number(int count , char c){
  switch(c){
      case 'S':
          print_char_with_count(1,' ');
          break;
      case 'Y':
          print_char_with_count(count,' ');
          break;
      case '-':
          print_char_with_count(count,'-');
          break;
      case '|':
          print_char_with_count(1,'|');
          break;
  }
}

void print_char_with_count (int count , char c){
  for (int i = 0; i < count; i++){
      printf("%c",c);
  }
}
void init_data_of_numbers(){
  //0
  data_of_numbers[0][0][0] = 'S';data_of_numbers[0][0][1] = '-';data_of_numbers[0][0][2] = 'S';
  data_of_numbers[0][1][0] = '|';data_of_numbers[0][1][1] = 'Y';data_of_numbers[0][1][2] = '|';
  data_of_numbers[0][2][0] = 'S';data_of_numbers[0][2][1] = 'Y';data_of_numbers[0][2][2] = 'S';
  data_of_numbers[0][3][0] = '|';data_of_numbers[0][3][1] = 'Y';data_of_numbers[0][3][2] = '|';
  data_of_numbers[0][4][0] = 'S';data_of_numbers[0][4][1] = '-';data_of_numbers[0][4][2] = 'S';
  //end 0

  //1
  data_of_numbers[1][0][0] = 'S';data_of_numbers[1][0][1] = 'Y';data_of_numbers[1][0][2] = 'S';
  data_of_numbers[1][1][0] = 'S';data_of_numbers[1][1][1] = 'Y';data_of_numbers[1][1][2] = '|';
  data_of_numbers[1][2][0] = 'S';data_of_numbers[1][2][1] = 'Y';data_of_numbers[1][2][2] = 'S';
  data_of_numbers[1][3][0] = 'S';data_of_numbers[1][3][1] = 'Y';data_of_numbers[1][3][2] = '|';
  data_of_numbers[1][4][0] = 'S';data_of_numbers[1][4][1] = 'Y';data_of_numbers[1][4][2] = 'S';
  //end 1

  //2
  data_of_numbers[2][0][0] = 'S';data_of_numbers[2][0][1] = '-';data_of_numbers[2][0][2] = 'S';
  data_of_numbers[2][1][0] = 'S';data_of_numbers[2][1][1] = 'Y';data_of_numbers[2][1][2] = '|';
  data_of_numbers[2][2][0] = 'S';data_of_numbers[2][2][1] = '-';data_of_numbers[2][2][2] = 'S';
  data_of_numbers[2][3][0] = '|';data_of_numbers[2][3][1] = 'Y';data_of_numbers[2][3][2] = 'S';
  data_of_numbers[2][4][0] = 'S';data_of_numbers[2][4][1] = '-';data_of_numbers[2][4][2] = 'S';
  //end2

  //3
  data_of_numbers[3][0][0] = 'S';data_of_numbers[3][0][1] = '-';data_of_numbers[3][0][2] = 'S';
  data_of_numbers[3][1][0] = 'S';data_of_numbers[3][1][1] = 'Y';data_of_numbers[3][1][2] = '|';
  data_of_numbers[3][2][0] = 'S';data_of_numbers[3][2][1] = '-';data_of_numbers[3][2][2] = 'S';
  data_of_numbers[3][3][0] = 'S';data_of_numbers[3][3][1] = 'Y';data_of_numbers[3][3][2] = '|';
  data_of_numbers[3][4][0] = 'S';data_of_numbers[3][4][1] = '-';data_of_numbers[3][4][2] = 'S';
  //end3

  //4
  data_of_numbers[4][0][0] = 'S';data_of_numbers[4][0][1] = 'Y';data_of_numbers[4][0][2] = 'S';
  data_of_numbers[4][1][0] = '|';data_of_numbers[4][1][1] = 'Y';data_of_numbers[4][1][2] = '|';
  data_of_numbers[4][2][0] = 'S';data_of_numbers[4][2][1] = '-';data_of_numbers[4][2][2] = 'S';
  data_of_numbers[4][3][0] = 'S';data_of_numbers[4][3][1] = 'Y';data_of_numbers[4][3][2] = '|';
  data_of_numbers[4][4][0] = 'S';data_of_numbers[4][4][1] = 'Y';data_of_numbers[4][4][2] = 'S';
  //end4

  //5
  data_of_numbers[5][0][0] = 'S';data_of_numbers[5][0][1] = '-';data_of_numbers[5][0][2] = 'S';
  data_of_numbers[5][1][0] = '|';data_of_numbers[5][1][1] = 'Y';data_of_numbers[5][1][2] = 'S';
  data_of_numbers[5][2][0] = 'S';data_of_numbers[5][2][1] = '-';data_of_numbers[5][2][2] = 'S';
  data_of_numbers[5][3][0] = 'S';data_of_numbers[5][3][1] = 'Y';data_of_numbers[5][3][2] = '|';
  data_of_numbers[5][4][0] = 'S';data_of_numbers[5][4][1] = '-';data_of_numbers[5][4][2] = 'S';
  //end5

  //6
  data_of_numbers[6][0][0] = 'S';data_of_numbers[6][0][1] = '-';data_of_numbers[6][0][2] = 'S';
  data_of_numbers[6][1][0] = '|';data_of_numbers[6][1][1] = 'Y';data_of_numbers[6][1][2] = 'S';
  data_of_numbers[6][2][0] = 'S';data_of_numbers[6][2][1] = '-';data_of_numbers[6][2][2] = 'S';
  data_of_numbers[6][3][0] = '|';data_of_numbers[6][3][1] = 'Y';data_of_numbers[6][3][2] = '|';
  data_of_numbers[6][4][0] = 'S';data_of_numbers[6][4][1] = '-';data_of_numbers[6][4][2] = 'S';
  //end6

  //7
  data_of_numbers[7][0][0] = 'S';data_of_numbers[7][0][1] = '-';data_of_numbers[7][0][2] = 'S';
  data_of_numbers[7][1][0] = 'S';data_of_numbers[7][1][1] = 'Y';data_of_numbers[7][1][2] = '|';
  data_of_numbers[7][2][0] = 'S';data_of_numbers[7][2][1] = 'Y';data_of_numbers[7][2][2] = 'S';
  data_of_numbers[7][3][0] = 'S';data_of_numbers[7][3][1] = 'Y';data_of_numbers[7][3][2] = '|';
  data_of_numbers[7][4][0] = 'S';data_of_numbers[7][4][1] = 'Y';data_of_numbers[7][4][2] = 'S';
  //end7

  //8
  data_of_numbers[8][0][0] = 'S';data_of_numbers[8][0][1] = '-';data_of_numbers[8][0][2] = 'S';
  data_of_numbers[8][1][0] = '|';data_of_numbers[8][1][1] = 'Y';data_of_numbers[8][1][2] = '|';
  data_of_numbers[8][2][0] = 'S';data_of_numbers[8][2][1] = '-';data_of_numbers[8][2][2] = 'S';
  data_of_numbers[8][3][0] = '|';data_of_numbers[8][3][1] = 'Y';data_of_numbers[8][3][2] = '|';
  data_of_numbers[8][4][0] = 'S';data_of_numbers[8][4][1] = '-';data_of_numbers[8][4][2] = 'S';
  //end8

  //9
  data_of_numbers[9][0][0] = 'S';data_of_numbers[9][0][1] = '-';data_of_numbers[9][0][2] = 'S';
  data_of_numbers[9][1][0] = '|';data_of_numbers[9][1][1] = 'Y';data_of_numbers[9][1][2] = '|';
  data_of_numbers[9][2][0] = 'S';data_of_numbers[9][2][1] = '-';data_of_numbers[9][2][2] = 'S';
  data_of_numbers[9][3][0] = 'S';data_of_numbers[9][3][1] = 'Y';data_of_numbers[9][3][2] = '|';
  data_of_numbers[9][4][0] = 'S';data_of_numbers[9][4][1] = '-';data_of_numbers[9][4][2] = 'S';
  //end9
}