@@ -14,6 +14,7 @@ The equivalent Decimal Number : 485
14
14
+ 3. Function to count amount of digits
15
15
+ 4. Write function to iterate over digits and return sum of powers
16
16
+ 5. Invalid input
17
+ + 5.5 Fix "using digit 0 - 7"
17
18
+ 6. Test
18
19
+ 7. Cpplint test
19
20
+ 8. Add and push
@@ -36,7 +37,7 @@ void print_invalid_input();
36
37
int power (int base , int exponent );
37
38
int find_number_of_digits (int octal );
38
39
int find_decimal (int octal , int number_of_digits , int sign );
39
- void output_decimal (int decimal );
40
+ void output_decimal (int octal , int decimal );
40
41
void test (int octal , int expected_result , int test_number );
41
42
void run_tests ();
42
43
@@ -48,7 +49,7 @@ int main() {
48
49
octal *= IS_NEGATIVE ;
49
50
sign = IS_NEGATIVE ;
50
51
}
51
- output_decimal (find_decimal (octal , find_number_of_digits (octal ), sign ));
52
+ output_decimal (octal , find_decimal (octal , find_number_of_digits (octal ), sign ));
52
53
} else {
53
54
print_invalid_input ();
54
55
}
@@ -87,8 +88,9 @@ void test(int octal, int expected_result, int test_number) {
87
88
}
88
89
}
89
90
90
- void output_decimal (int decimal ) {
91
- printf ("%d\n" , decimal );
91
+ void output_decimal (int octal , int decimal ) {
92
+ printf ("The Octal Number: %d" , octal );
93
+ printf ("The equivalent Decimal Number: %d\n" , decimal );
92
94
}
93
95
94
96
int find_decimal (int octal , int number_of_digits , int sign ) {
@@ -123,13 +125,30 @@ int power(int base, int exponent) {
123
125
}
124
126
125
127
void print_invalid_input () {
126
- printf ("n/a" );
128
+ printf ("n/a\n" );
129
+ }
130
+
131
+ int is_valid_octal (int octal , int number_of_digits ) {
132
+ int digit = 0 , temp_octal = octal , power_of_ten , is_valid = TRUE;
133
+ for (int i = number_of_digits ; i > 0 ; -- i ) {
134
+ power_of_ten = power (10 , number_of_digits - 1 );
135
+ digit = temp_octal / power_of_ten * power (8 , number_of_digits - 1 );
136
+ temp_octal -= temp_octal / power_of_ten * power_of_ten ;
137
+ -- number_of_digits ;
138
+ if (digit >= 8 && digit <= 9 ) {
139
+ is_valid = FALSE;
140
+ break ;
141
+ }
142
+ }
143
+ return is_valid ;
127
144
}
128
145
129
146
int input_octal (int * octal ) {
147
+ printf ("Input an octal number (using digit 0 - 7):\n" );
130
148
int is_valid = TRUE;
131
149
char endline = '\0' ;
132
- if (!scanf ("%d%c" , octal , & endline ) || endline != '\n' ) {
150
+ if (!scanf ("%d%c" , octal , & endline ) || endline != '\n' ||
151
+ is_valid_octal (* octal , find_number_of_digits (* octal ) == FALSE)) {
133
152
is_valid = FALSE;
134
153
}
135
154
return is_valid ;
0 commit comments