taeyounkim LOG
Program to display "SEVEN SEGMENT DISPLAY" 본문
Program to display "SEVEN SEGMENT DISPLAY"
taeyounkim 2021. 7. 6. 08:18#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DIGITS 10
int digit[MAX_DIGITS];
int segments[10][7] =
{{1,1,1,1,1,1,0}, {0,1,1,0,0,0,0}, {1,1,0,1,1,0,1}, {1,1,1,1,0,0,1}, {0,1,1,0,0,1,1}, {1,0,1,1,0,1,1},
{1,0,1,1,1,1,1}, {1,1,1,0,0,0,0}, {1,1,1,1,1,1,1}, {1,1,1,1,0,1,1}};
char digits[4][MAX_DIGITS * 4];
void clear_digits_array(void);
void process_digit(int digit, int position);
void print_digit_array(void);
int main(void)
{
int num = 0;
int ch;
clear_digits_array();
printf("Enter a number: ");
while((ch = getchar()) != '\n'){
digit[num] = ch - '0'; /*converts 'char' into 'int'*/
num++;
}
for (int i=0; i<num; i++){
process_digit(digit[i], i);
}
print_digit_array();
return 0;
}
/*The following function will store blank characters into all elements of the digits array*/
void clear_digits_array(void)
{
for(int i=0; i<4; i++){
for(int j=0; j < MAX_DIGITS*4; j++){
digits[i][j] = ' ';
}
}
}
/*The following function will store the seven-segment representation of digit
into a specific position in the digits array*/
void process_digit(int digit, int position)
{
int i;
for(i=0; i<7; i++){
if(segments[digit][i]==1){
switch(i){
case 0: digits[0][1+position*4]='_'; break;
case 1: digits[1][2+position*4]='|'; break;
case 2: digits[2][2+position*4]='|'; break;
case 3: digits[2][1+position*4]='_'; break;
case 4: digits[2][0+position*4]='|'; break;
case 5: digits[1][0+position*4]='|'; break;
case 6: digits[1][1+position*4]='_'; break;
}
}
}
}
/*The following function will display the rows of the digits array*/
void print_digit_array(void)
{
int i, j;
for(i=0; i<4; i++){
for(j=0; j<MAX_DIGITS*4; j++){
printf("%c",digits[i][j]);
}
printf("\n");
}
}
//예시 결과 화면
'Career&Study > Coding Practice' 카테고리의 다른 글
pointer practice 2 (program to find the largest and smallest elements in an array) (0) | 2021.07.07 |
---|---|
pointer practice (decomposing fraction program) (0) | 2021.07.07 |
Guessing a Number game codes (0) | 2021.07.05 |
Game of Craps code, 그리고 stackoverflow.com (0) | 2021.07.02 |
function that returns the k(th) digit(from the right) in n(a positive integer) (0) | 2021.06.30 |