Guest User

Untitled

a guest
Nov 8th, 2019
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. #define MAX_DIGITS 10
  4.  
  5. // array determining which segments are "on" or "off"
  6. const int segments[10][7] = { { 1, 1, 1, 1, 1, 1, 0},
  7. { 0, 1, 1, 0, 0, 0, 0},
  8. { 1, 1, 0, 1, 1, 0, 1},
  9. { 1, 1, 1, 1, 0, 0, 1},
  10. { 0, 1, 1, 0, 0, 1, 1},
  11. { 1, 0, 1, 1, 0, 1, 1},
  12. { 1, 0, 1, 1, 1, 1, 1},
  13. { 1, 1, 1, 0, 0, 0, 0},
  14. { 1, 1, 1, 1, 1, 1, 1},
  15. { 1, 1, 1, 0, 0, 1, 1} };
  16.  
  17. /*
  18. location array to store characters('blank', '|', '_') to create the seven
  19. segment display
  20.  
  21. __
  22. | __ |
  23. | __ |
  24.  
  25. blocks[0][1] = '_'; == Segment 0
  26. blocks[1][2] = '|'; == Segment 1
  27. blocks[2][2] = '|'; == Segment 2
  28. blocks[2][1] = '_'; == Segment 3
  29. blocks[2][0] = '|'; == Segment 4
  30. blocks[1][0] = '|'; == Segment 5
  31. blocks[1][1] = '_'; == Segment 6
  32.  
  33. */
  34. int locations[7][2] = { {0, 1},
  35. {1, 2},
  36. {2, 2},
  37. {2, 1},
  38. {2, 0},
  39. {1, 0},
  40. {1, 1} };
  41.  
  42. /*
  43. The character that goes into digits array(| or _)
  44. */
  45. char block;
  46.  
  47. char digits[4][4 * MAX_DIGITS];
  48.  
  49. void clear_digits_array(void);
  50. void process_digit(int digit, int position);
  51. void print_digits_array(void);
  52. int main()
  53. {
  54. int num;
  55. char ch;
  56. char block;
  57. int count = 0;
  58. int position = 0;
  59.  
  60. clear_digits_array();
  61.  
  62. while ((ch = getchar()) != '\n' && count < 10) {
  63. if (ch >= '0' && ch <= '9') {
  64. num = ch - '0';
  65. count++;
  66. }
  67. process_digit(num, position);
  68. position +=4;
  69. }
  70. printf("count = %d\n", count);
  71. printf("num = %d\n", num);
  72. printf("location[0][0] = %d, location[0][1] = %d\n", locations[0][0], locations[0][1]);
  73.  
  74. print_digits_array();
  75.  
  76. return 0;
  77. }
  78.  
  79. // clear_digits_array will storeblank characters into all elements of digits array
  80. void clear_digits_array(void)
  81. {
  82. for(int i = 0; i < 4; i++){
  83. for(int j = 0; j < 4 * MAX_DIGITS; j++)
  84. digits[i][j] = ' ';
  85. }
  86. }
  87.  
  88. // process_digit will store the seven-segnment representation of digit into a specified
  89. // position in the digits array (positions range from 0 to MAX_DIGITS - 1)
  90. void process_digit(int digit, int position)
  91. {
  92. for (int j = 0; j < 7; j++) {
  93. if (segments[digit][j]) {
  94. if ( j % 3 == 0) {
  95. block = '_';
  96. } else {
  97. block = '|';
  98. }
  99. digits[locations[j][0]][locations[j][1] + position] = block;
  100. }
  101. }
  102.  
  103. }
  104.  
  105. // print_digits_array will display the rows of the digits array, each on a single line,
  106. // producing output
  107. void print_digits_array(void)
  108. {
  109. for(int i = 0; i < 4; i++){
  110. for(int j = 0; j < 4 * MAX_DIGITS; j++)
  111. printf("%c", digits[i][j]);
  112. printf("\n");
  113. }
  114. }
RAW Paste Data