-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_integer_in_7_segments.c
115 lines (102 loc) · 3.81 KB
/
display_integer_in_7_segments.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* This program will output integer in seven-segment display format.
*/
#include <stdio.h>
#include <stdlib.h>
const char segments[10][3][3] = {
{{' ', '_', ' '},
{'|', ' ', '|'},
{'|', '_', '|'}}, // 0
{{' ', ' ', ' '},
{' ', ' ', '|'},
{' ', ' ', '|'}}, // 1
{{' ', '_', ' '},
{' ', '_', '|'},
{'|', '_', ' '}}, // 2
{{' ', '_', ' '},
{' ', '_', '|'},
{' ', '_', '|'}}, // 3
{{' ', ' ', ' '},
{'|', '_', '|'},
{' ', ' ', '|'}}, // 4
{{' ', '_', ' '},
{'|', '_', ' '},
{' ', '_', '|'}}, // 5
{{' ', '_', ' '},
{'|', '_', ' '},
{'|', '_', '|'}}, // 6
{{' ', '_', ' '},
{' ', ' ', '|'},
{' ', ' ', '|'}}, // 7
{{' ', '_', ' '},
{'|', '_', '|'},
{'|', '_', '|'}}, // 8
{{' ', '_', ' '},
{'|', '_', '|'},
{' ', '_', '|'}} // 9
};
// print positive input digit
void printPositiveDigit(int digit) {
// Loop through each row and column of the segments array
// i index for array in this array, so it is the row, j for index in i array, so it is the column
for (int i = 0; i < 3; i++) {
// print one space in the first column, to fit the position of negative sign
if (i == 1 || i == 0) {
printf(" ");
}
for (int j = 0; j < 3; j++) {
// Because the existence of an extra space to fit the negative sign ,
// without this if condition the third row of my result would not be in shape
// So I add this condition to print one more extra space at the beginning of third rows
if(i == 2 && j == 0){
printf(" %c", segments[digit][i][j]);
}
else{
printf("%c", segments[digit][i][j]);
}
}
// Move to the next row of the display
printf("\n");
}
}
// print positive input digit
// the algorithm are same as above, just change the left-middle independent column to '_'
void printNegativeDigit(int digit) {
// Convert digit to absolute value
int negativeInput = abs(digit);
for (int i = 0; i < 3; i++) {
// Print '-' character in left-middle independent column
if (i == 0) {
printf(" ");
}
if (i == 1) {
printf("_");
}
for (int j = 0; j < 3; j++) {
if(i == 2 && j == 0){
printf(" %c", segments[negativeInput][i][j]);
}
else{
printf("%c", segments[negativeInput][i][j]);
}
}
printf("\n");
}
}
int main() {
int input;
char state;
do {
printf("Please enter an integer from -9 to 9: ");
scanf("%d", &input);
// Check if the input is negative and call the appropriate function
if (input < 0) {
printNegativeDigit(input);
} else {
printPositiveDigit(input);
}
printf("You want to continue or stop? (Y/N): ");
scanf(" %c", &state);
} while (state == 'Y' || state == 'y');
return 0;
}