taeyounkim LOG

Program to display "SEVEN SEGMENT DISPLAY" 본문

Career&Study/Coding Practice

Program to display "SEVEN SEGMENT DISPLAY"

taeyounkim 2021. 7. 6. 08:18
728x90

#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");
    }
}

 

 

//예시 결과 화면

728x90