F
Felipe Ribeiro
Hi everybody,
I wrote a small program that simulates the effect of a seven-segment
display and would like to know if I can do anything to improve it.
Here's the code:
====================================
#include <stdio.h>
#define MAX_DIGITS 10
#define HEIGHT_DIGIT 3
/* External variables */
/*
* 'segments' stores the seven-segment representaion of each number.
* The representation is done in the following way:
*
* 1__2__3
* | |
* 4 6
* |__5__|
* | |
* 7 9
* |__8__|
*
* '*' determines that a segment is necessary in a given position
while
* ' ' detrmines that no segment should be printed and a blank is
* printed instead.
*/
const char segments[10][9] =
{{' ', '*', ' ', '*', ' ', '*', '*', '*', '*'},
{' ', ' ', ' ', ' ', ' ', '*', ' ', ' ', '*'},
{' ', '*', ' ', ' ', '*', '*', '*', '*', ' '},
{' ', '*', ' ', ' ', '*', '*', ' ', '*', '*'},
{' ', ' ', ' ', '*', '*', '*', ' ', ' ', '*'},
{' ', '*', ' ', '*', '*', ' ', ' ', '*', '*'},
{' ', '*', ' ', '*', '*', ' ', '*', '*', '*'},
{' ', '*', ' ', ' ', ' ', '*', ' ', ' ', '*'},
{' ', '*', ' ', '*', '*', '*', '*', '*', '*'},
{' ', '*', ' ', '*', '*', '*', ' ', '*', '*'}};
/*
* 'digits' will store only the seven-segment representation of the
* digits entered by the user. Each digit is 3 characters high and 3
* characters wide.
*/
char digits[HEIGHT_DIGIT][MAX_DIGITS * 4];
/* Prototypes */
void process_digit(int digit, int position);
void print_digits_array(void);
/*
* Calls 'process_digit' and 'print_digits_array' repeatedly.
*/
int main(void)
{
char digit_ch;
int digit, i = 0;
printf("Enter a number: ");
do {
/*
* The user may enter a non-numerical character but it's gonna
* be ignored.
*/
digit_ch = getchar();
digit = digit_ch - '0';
if (0 <= digit && digit <= 9) {
process_digit(digit, i);
i++;
}
} while (i < MAX_DIGITS && digit_ch != '\n');
print_digits_array();
return 0;
}
/*
* Treats the digit entered by the user. 'digits' will receive the
data
* stored in 'segments' according to the number entered by the user.
* For instance; if the number one is entered, then all the
information
* in row one of the 'segments' array will be passed to the 'digits'
* array.
*/
void process_digit(int digit, int position)
{
int start, end;
int i, j = 0;
start = position * 4;
end = start + 3;
for (i = 0; i < HEIGHT_DIGIT; i++)
for (start = position * 4; start < end; start++) {
digits[start] = segments[digit][j];
j++;
}
}
/*
* Surprisingly, it prints the 'digits' array.
*/
void print_digits_array(void)
{
int i, j;
for (i = 0; i < HEIGHT_DIGIT; i++) {
for (j = 0; j < MAX_DIGITS * 4; j++)
if (digits[j] == '*' && j % 2 == 0)
printf("|");
else if (digits[j] == '*')
printf("_");
else
printf(" ");
printf("\n");
}
}
====================================
I don't know if I chose the best way to do it. Problably not, since
I'm just learning.
My segments array specially causes me some doubts. Each row stores
information about a digit and it does so using 9 positions instead of
7. I wasn't able to figure out a way to solve the problem using just 7
segments.
I'd appreciate any advice.
Thank in advance.
Felipe
I wrote a small program that simulates the effect of a seven-segment
display and would like to know if I can do anything to improve it.
Here's the code:
====================================
#include <stdio.h>
#define MAX_DIGITS 10
#define HEIGHT_DIGIT 3
/* External variables */
/*
* 'segments' stores the seven-segment representaion of each number.
* The representation is done in the following way:
*
* 1__2__3
* | |
* 4 6
* |__5__|
* | |
* 7 9
* |__8__|
*
* '*' determines that a segment is necessary in a given position
while
* ' ' detrmines that no segment should be printed and a blank is
* printed instead.
*/
const char segments[10][9] =
{{' ', '*', ' ', '*', ' ', '*', '*', '*', '*'},
{' ', ' ', ' ', ' ', ' ', '*', ' ', ' ', '*'},
{' ', '*', ' ', ' ', '*', '*', '*', '*', ' '},
{' ', '*', ' ', ' ', '*', '*', ' ', '*', '*'},
{' ', ' ', ' ', '*', '*', '*', ' ', ' ', '*'},
{' ', '*', ' ', '*', '*', ' ', ' ', '*', '*'},
{' ', '*', ' ', '*', '*', ' ', '*', '*', '*'},
{' ', '*', ' ', ' ', ' ', '*', ' ', ' ', '*'},
{' ', '*', ' ', '*', '*', '*', '*', '*', '*'},
{' ', '*', ' ', '*', '*', '*', ' ', '*', '*'}};
/*
* 'digits' will store only the seven-segment representation of the
* digits entered by the user. Each digit is 3 characters high and 3
* characters wide.
*/
char digits[HEIGHT_DIGIT][MAX_DIGITS * 4];
/* Prototypes */
void process_digit(int digit, int position);
void print_digits_array(void);
/*
* Calls 'process_digit' and 'print_digits_array' repeatedly.
*/
int main(void)
{
char digit_ch;
int digit, i = 0;
printf("Enter a number: ");
do {
/*
* The user may enter a non-numerical character but it's gonna
* be ignored.
*/
digit_ch = getchar();
digit = digit_ch - '0';
if (0 <= digit && digit <= 9) {
process_digit(digit, i);
i++;
}
} while (i < MAX_DIGITS && digit_ch != '\n');
print_digits_array();
return 0;
}
/*
* Treats the digit entered by the user. 'digits' will receive the
data
* stored in 'segments' according to the number entered by the user.
* For instance; if the number one is entered, then all the
information
* in row one of the 'segments' array will be passed to the 'digits'
* array.
*/
void process_digit(int digit, int position)
{
int start, end;
int i, j = 0;
start = position * 4;
end = start + 3;
for (i = 0; i < HEIGHT_DIGIT; i++)
for (start = position * 4; start < end; start++) {
digits[start] = segments[digit][j];
j++;
}
}
/*
* Surprisingly, it prints the 'digits' array.
*/
void print_digits_array(void)
{
int i, j;
for (i = 0; i < HEIGHT_DIGIT; i++) {
for (j = 0; j < MAX_DIGITS * 4; j++)
if (digits[j] == '*' && j % 2 == 0)
printf("|");
else if (digits[j] == '*')
printf("_");
else
printf(" ");
printf("\n");
}
}
====================================
I don't know if I chose the best way to do it. Problably not, since
I'm just learning.
My segments array specially causes me some doubts. Each row stores
information about a digit and it does so using 9 positions instead of
7. I wasn't able to figure out a way to solve the problem using just 7
segments.
I'd appreciate any advice.
Thank in advance.
Felipe