Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define MAX_DIGITS 10
- // array determining which segments are "on" or "off"
- const 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, 0, 0, 1, 1} };
- /*
- location array to store characters('blank', '|', '_') to create the seven
- segment display
- __
- | __ |
- | __ |
- blocks[0][1] = '_'; == Segment 0
- blocks[1][2] = '|'; == Segment 1
- blocks[2][2] = '|'; == Segment 2
- blocks[2][1] = '_'; == Segment 3
- blocks[2][0] = '|'; == Segment 4
- blocks[1][0] = '|'; == Segment 5
- blocks[1][1] = '_'; == Segment 6
- */
- int locations[7][2] = { {0, 1},
- {1, 2},
- {2, 2},
- {2, 1},
- {2, 0},
- {1, 0},
- {1, 1} };
- /*
- The character that goes into digits array(| or _)
- */
- char block;
- char digits[4][4 * MAX_DIGITS];
- void clear_digits_array(void);
- void process_digit(int digit, int position);
- void print_digits_array(void);
- int main()
- {
- int num;
- char ch;
- char block;
- int count = 0;
- int position = 0;
- clear_digits_array();
- while ((ch = getchar()) != '\n' && count < 10) {
- if (ch >= '0' && ch <= '9') {
- num = ch - '0';
- count++;
- }
- process_digit(num, position);
- position +=4;
- }
- printf("count = %d\n", count);
- printf("num = %d\n", num);
- printf("location[0][0] = %d, location[0][1] = %d\n", locations[0][0], locations[0][1]);
- print_digits_array();
- return 0;
- }
- // clear_digits_array will storeblank characters into all elements of digits array
- void clear_digits_array(void)
- {
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 4 * MAX_DIGITS; j++)
- digits[i][j] = ' ';
- }
- }
- // process_digit will store the seven-segnment representation of digit into a specified
- // position in the digits array (positions range from 0 to MAX_DIGITS - 1)
- void process_digit(int digit, int position)
- {
- for (int j = 0; j < 7; j++) {
- if (segments[digit][j]) {
- if ( j % 3 == 0) {
- block = '_';
- } else {
- block = '|';
- }
- digits[locations[j][0]][locations[j][1] + position] = block;
- }
- }
- }
- // print_digits_array will display the rows of the digits array, each on a single line,
- // producing output
- void print_digits_array(void)
- {
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 4 * MAX_DIGITS; j++)
- printf("%c", digits[i][j]);
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement