@@ -19,7 +19,7 @@ The Octal of 79 is 117.
19
19
2 Get the integer quotient for the next iteration.
20
20
3 Get the remainder for the octal digit.
21
21
4 Repeat the steps until the quotient is equal to 0.
22
- 3. Invalid input
22
+ + 3. Invalid input
23
23
+ 4. Convert:
24
24
+ Function to count how many iterations of /8 there will be
25
25
+ Power function
@@ -33,21 +33,76 @@ The Octal of 79 is 117.
33
33
34
34
#include <stdio.h>
35
35
36
- int input_decimal_number ();
36
+ enum input_state {
37
+ FAILURE = 0 ,
38
+ SUCCESS = 1
39
+ };
40
+
41
+ enum is_negative {
42
+ TRUE = 1 ,
43
+ FALSE = 0
44
+ };
45
+
46
+ int input_decimal_number (int * decimal_number );
37
47
int number_of_eights (int decimal_number );
38
48
int power (int base , int exponent );
39
- int convert (int decimal_number , int division_by_eight );
49
+ int convert (int decimal_number );
40
50
int reverse_number (int converted_number , int division_by_eight );
41
51
void print_octal (int decimal_number , int result );
52
+ void print_invalid_input ();
53
+ void test (int number , int expected , int test_number );
54
+ void run_tests ();
42
55
43
56
int main () {
44
- int decimal_number = input_decimal_number ();
45
- int division_by_eight = number_of_eights (decimal_number );
46
- int converted = convert (decimal_number , division_by_eight );
47
- print_octal (decimal_number , converted );
57
+ // int decimal_number = 0;
58
+ // int input_state = input_decimal_number(&decimal_number);
59
+ // if (input_state == SUCCESS) {
60
+ // int converted = convert(decimal_number);
61
+ // print_octal(decimal_number, converted);
62
+ // } else {
63
+ // print_invalid_input();
64
+ // }
65
+ run_tests ();
48
66
return 0 ;
49
67
}
50
68
69
+ void run_tests () {
70
+ /*
71
+ // Normal value tests
72
+ test(79, 117, 1);
73
+ test(1, 1, 2);
74
+ test(15, 17, 3);
75
+ test(555, 1053, 4);
76
+ test(8888, 21270, 5);
77
+ // Negative number test
78
+ test(-79, -117, 6);
79
+ test(-1, -1, 7);
80
+ test(-15, -17, 8);
81
+ test(-555, -1053, 9);
82
+ test(-8888, -21270, 10);
83
+ // Zero test
84
+ test(0, 0, 11);
85
+ // Big number test
86
+ test(47483647, 265105377, 12);
87
+ */
88
+ // Near end of int range
89
+ test (294903430 , 2144757206 , 13 );
90
+ }
91
+
92
+ void test (int number , int expected , int test_number ) {
93
+ int actual = convert (number );
94
+ if (actual == expected ) {
95
+ printf ("Test #%d passed\n" , test_number );
96
+ } else {
97
+ printf ("Test #%d failed. Number = %d, expected = %d, actual = %d\n" ,
98
+ test_number , number , expected , actual );
99
+ }
100
+ }
101
+
102
+ void print_invalid_input () {
103
+ printf ("n/a" );
104
+ }
105
+
51
106
void print_octal (int decimal_number , int result ) {
52
107
printf ("The Octal of %d is %d.\n" , decimal_number , result );
53
108
}
@@ -78,7 +133,13 @@ I will probably need a function to count how many iterations I will need to conv
78
133
I'll need that function once again when I will flip the number
79
134
*/
80
135
81
- int convert (int decimal_number , int division_by_eight ) {
136
+ int convert (int decimal_number ) {
137
+ int is_negative = FALSE;
138
+ if (decimal_number < 0 ) {
139
+ decimal_number *= -1 ;
140
+ is_negative = TRUE;
141
+ }
142
+ int division_by_eight = number_of_eights (decimal_number );
82
143
int result = 0 , quotient , remainder , temp_decimal = decimal_number ,
83
144
divider = 8 , power_counter = division_by_eight ;
84
145
for (int i = 0 ; i <= division_by_eight ; ++ i ) {
@@ -93,6 +154,9 @@ int convert(int decimal_number, int division_by_eight) {
93
154
temp_decimal = quotient ;
94
155
}
95
156
result = reverse_number (result , division_by_eight );
157
+ if (is_negative == TRUE) {
158
+ result *= -1 ;
159
+ }
96
160
return result ;
97
161
}
98
162
@@ -122,8 +186,13 @@ int number_of_eights(int decimal_number) {
122
186
return counter ;
123
187
}
124
188
125
- int input_decimal_number () {
126
- int decimal_number ;
127
- scanf ("%d" , & decimal_number );
128
- return decimal_number ;
189
+ int input_decimal_number (int * decimal_number ) {
190
+ int input_state = FAILURE ;
191
+ char endline = '\0' ;
192
+ if (!scanf ("%d%c" , decimal_number , & endline ) || endline != '\n' ) {
193
+ input_state = FAILURE ;
194
+ } else {
195
+ input_state = SUCCESS ;
196
+ }
197
+ return input_state ;
129
198
}
0 commit comments