@@ -14,44 +14,92 @@ 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
- 6. Test
18
- 7. Cpplint test
19
- 8. Add and push
17
+ + 6. Test
18
+ + 7. Cpplint test
19
+ + 8. Add and push
20
20
*/
21
21
22
22
#include <stdio.h>
23
23
24
- enum boolean_for_validation {
24
+ enum boolean_for_input {
25
25
TRUE = 1 ,
26
26
FALSE = 0
27
27
};
28
28
29
+ enum switch_for_negative_octal {
30
+ IS_NEGATIVE = -1 ,
31
+ IS_POSITIVE = 1
32
+ };
33
+
29
34
int input_octal (int * octal );
30
35
void print_invalid_input ();
31
36
int power (int base , int exponent );
32
37
int find_number_of_digits (int octal );
33
- int find_decimal (int octal , int number_of_digits );
38
+ int find_decimal (int octal , int number_of_digits , int sign );
39
+ void output_decimal (int decimal );
40
+ void test (int octal , int expected_result , int test_number );
41
+ void run_tests ();
34
42
35
43
int main () {
36
- int octal = 0 ;
37
- if (input_octal (& octal )) {
38
- int number_of_digits = find_number_of_digits (octal );
39
- printf ("%d" , find_decimal (octal , number_of_digits ));
40
- } else {
44
+ int octal = 0 ;
45
+ if (input_octal (& octal )) {
46
+ int sign = IS_POSITIVE ;
47
+ if (octal < 0 ) {
48
+ octal *= IS_NEGATIVE ;
49
+ sign = IS_NEGATIVE ;
50
+ }
51
+ output_decimal (find_decimal (octal , find_number_of_digits (octal ), sign ));
52
+ } else {
41
53
print_invalid_input ();
42
- }
43
- return 0 ;
54
+ }
55
+ run_tests ();
56
+ return 0 ;
57
+ }
58
+
59
+ void run_tests () {
60
+ // Normal values tests
61
+ test (745 , 485 , 1 );
62
+ test (745456 , 248622 , 2 );
63
+ test (15 , 13 , 3 );
64
+ // Big number tests
65
+ test (523053070 , 88888888 , 4 );
66
+ // Near end of int range test
67
+ test (2122206200 , 290000000 , 5 );
68
+ // Zero test
69
+ test (0 , 0 , 6 );
70
+ // Negative number test
71
+ test (-34567 , -14711 , 7 );
72
+ test (-345 , -229 , 8 );
73
+ }
74
+
75
+ void test (int octal , int expected_result , int test_number ) {
76
+ int sign = IS_POSITIVE ;
77
+ if (octal < 0 ) {
78
+ octal *= IS_NEGATIVE ;
79
+ sign = IS_NEGATIVE ;
80
+ }
81
+ int actual_result = find_decimal (octal , find_number_of_digits (octal ), sign );
82
+ if (actual_result == expected_result ) {
83
+ printf ("Test #%d: Success\n" , test_number );
84
+ } else {
85
+ printf ("Test #%d: Failed. Octal = %d, expected_result = %d, actual_result = %d\n" ,
86
+ test_number , octal , expected_result , actual_result );
87
+ }
88
+ }
89
+
90
+ void output_decimal (int decimal ) {
91
+ printf ("%d\n" , decimal );
44
92
}
45
93
46
- int find_decimal (int octal , int number_of_digits ) {
94
+ int find_decimal (int octal , int number_of_digits , int sign ) {
47
95
int decimal = 0 , temp_octal = octal , power_of_ten ;
48
96
for (int i = number_of_digits ; i > 0 ; -- i ) {
49
97
power_of_ten = power (10 , number_of_digits - 1 );
50
98
decimal += temp_octal / power_of_ten * power (8 , number_of_digits - 1 );
51
99
temp_octal -= temp_octal / power_of_ten * power_of_ten ;
52
100
-- number_of_digits ;
53
101
}
54
- return decimal ;
102
+ return decimal * sign ;
55
103
}
56
104
57
105
int find_number_of_digits (int octal ) {
0 commit comments