@@ -9,38 +9,124 @@ The equivalent Decimal Number is : 84
9
9
10
10
+ 1. Take input. Max range of int is 2,147,483,647, so I'm going to take the maximum binary number of
11
11
31 characters long. 1111111111111111111111111111111 == 2,147,483,647
12
- 1.5 Invalid input
13
- 2. Figure out how to use math library to find the decimal number
14
- 3. Convert to decimal
15
- 4. Output
16
- 5. Test
17
- 6. Cpplint test
18
- 7. Add and push
12
+ + 1.1 Remember how to pass an array from a function to a funtion and return an array from a function -
13
+ Here we should use a pointer to that array and initialize it in the main funciton
14
+ + 1.5 Invalid input
15
+ + 2. Figure out how to use math library to find the decimal number:
16
+ if char == 1 then 2^lenght-1 where i goes from lenght to zero
17
+ if zero -> go next
18
+ + 3. Convert to decimal
19
+ + 4. Output
20
+ + 5. Test
21
+ + 6. Cpplint test
22
+ + 7. Add and push
19
23
*/
20
24
21
25
#include <stdio.h>
22
26
#include <math.h>
27
+ #define NMAX 31
23
28
24
- char input_binary ();
29
+ // To return an array from a function we should use a pointer
30
+ int input_binary_array (char * binary_array );
31
+ void print_invalid_input ();
32
+ int convert_binary_to_decimal (char * binary_array , int lenght );
33
+ void print_decimal_result (char * binary_array , int lenght , int decimal );
34
+ void test (char * test_binary_array , int lenght , int expected_result , int test_number );
35
+ void print_test_binary_number (char * binary_array , int lenght , int test_number );
25
36
26
37
int main () {
27
- char input_array = input_binary ();
28
- printf ("%c" , input_array );
38
+ // Regular run
39
+ char binary_array [NMAX ];
40
+ int lenght = input_binary_array (binary_array );
41
+ if (lenght >= 1 ) {
42
+ int decimal = convert_binary_to_decimal (binary_array , lenght );
43
+ print_decimal_result (binary_array , lenght , decimal );
44
+ } else {
45
+ print_invalid_input ();
46
+ }
47
+ // Test runs
48
+ // Regular value test
49
+ char test1_binary_array [7 ] = {'1' , '0' , '1' , '0' , '1' , '0' , '0' };
50
+ test (test1_binary_array , 7 , 84 , 1 );
51
+ // All ones test
52
+ char test2_binary_array [7 ] = {'1' , '1' , '1' , '1' , '1' , '1' , '1' };
53
+ test (test2_binary_array , 7 , 127 , 2 );
54
+ // All zeros test
55
+ char test3_binary_array [7 ] = {'0' , '0' , '0' , '0' , '0' , '0' , '0' };
56
+ test (test3_binary_array , 7 , 0 , 3 );
57
+ // Min test
58
+ char test4_binary_array [1 ] = {'0' };
59
+ test (test4_binary_array , 1 , 0 , 4 );
60
+ // Max int range test
61
+ char test5_binary_array [31 ] = {'1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' ,
62
+ '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' , '1' ,
63
+ '1' , '1' , '1' , '1' , '1' , '1' , '1' };
64
+ test (test5_binary_array , 31 , 2147483647 , 5 );
65
+ // 1 - 0 - 1 - 0 - 1 test
66
+ char test6_binary_array [7 ] = {'1' , '0' , '1' , '0' , '1' , '0' , '1' };
67
+ test (test6_binary_array , 7 , 85 , 6 );
68
+ // 1 - 1 - 1 - 0 - 0 - 0 test
69
+ char test7_binary_array [12 ] = {'1' , '1' , '1' , '0' , '0' , '0' , '1' , '1' , '1' , '0' , '0' , '0' };
70
+ test (test7_binary_array , 12 , 3640 , 7 );
29
71
return 0 ;
30
72
}
31
73
32
- char input_binary () {
33
- int lenght = 31 ;
34
- char input_string_array [lenght ];
35
- for (int i = 0 ; i <= lenght ; i ++ ) {
36
- scanf ("%1c" , & input_string_array [i ]);
37
- if (input_string_array [i ] == '\n' ) {
38
- lenght = i - 1 ;
74
+ void test (char * test_binary_array , int lenght , int expected_result , int test_number ) {
75
+ int actual_result = convert_binary_to_decimal (test_binary_array , lenght );
76
+ print_test_binary_number (test_binary_array , lenght , test_number );
77
+ if (actual_result == expected_result ) {
78
+ printf ("\nTest #%d: expected = %d, actual = %d \nSuccess" ,
79
+ test_number , expected_result , actual_result );
80
+ } else {
81
+ printf ("\nTest #%d: expected = %d, actual = %d \nFailed" ,
82
+ test_number , expected_result , actual_result );
83
+ }
84
+ printf ("\n" );
85
+ }
86
+
87
+ void print_test_binary_number (char * binary_array , int lenght , int test_number ) {
88
+ printf ("\nThe test #%d Binary Number: " , test_number );
89
+ for (int i = 0 ; i < lenght ; ++ i ) {
90
+ printf ("%c" , binary_array [i ]);
91
+ }
92
+ }
93
+
94
+ void print_decimal_result (char * binary_array , int lenght , int decimal ) {
95
+ printf ("\nThe Binary Number: " );
96
+ for (int i = 0 ; i <= lenght ; ++ i ) {
97
+ printf ("%c" , binary_array [i ]);
98
+ }
99
+ printf ("\nThe equivalent Decimal Number is: %d\n" , decimal );
100
+ }
101
+
102
+ int convert_binary_to_decimal (char * binary_array , int lenght ) {
103
+ int result = 0 , exponent = lenght - 1 ;
104
+ for (int i = 0 ; i <= lenght - 1 ; ++ i ) {
105
+ if (binary_array [i ] == '1' ) {
106
+ result += (int )pow (2 , exponent );
107
+ }
108
+ -- exponent ;
109
+ }
110
+ return result ;
111
+ }
112
+
113
+ void print_invalid_input () {
114
+ printf ("n/a" );
115
+ }
116
+
117
+ int input_binary_array (char * binary_array ) {
118
+ int lenght = NMAX ;
119
+ for (int i = 0 ; i <= lenght ; ++ i ) {
120
+ scanf ("%1c" , & binary_array [i ]);
121
+ if (binary_array [i ] == '\n' ) {
122
+ // Used to stop the loop when it reaches newline
123
+ lenght = i ;
39
124
break ;
40
125
}
126
+ if (binary_array [i ] != '0' && binary_array [i ] != '1' ) {
127
+ // Used to sort out invalid input
128
+ lenght = -1 ;
129
+ }
41
130
}
42
- // for (int i = 0; i <= lenght; i++) {
43
- // printf("%c", input_string_array[i]);
44
- // }
45
- return * input_string_array ;
131
+ return lenght ;
46
132
}
0 commit comments