Skip to content

Commit 0d5ed5e

Browse files
committed
Invalid input done. Need to figure out how to fix error with number that are almost at the end of int range
1 parent ec9566a commit 0d5ed5e

File tree

1 file changed

+81
-12
lines changed

1 file changed

+81
-12
lines changed

For loop/50. Decimal to octal.c

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The Octal of 79 is 117.
1919
2 Get the integer quotient for the next iteration.
2020
3 Get the remainder for the octal digit.
2121
4 Repeat the steps until the quotient is equal to 0.
22-
3. Invalid input
22+
+ 3. Invalid input
2323
+ 4. Convert:
2424
+ Function to count how many iterations of /8 there will be
2525
+ Power function
@@ -33,21 +33,76 @@ The Octal of 79 is 117.
3333

3434
#include <stdio.h>
3535

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);
3747
int number_of_eights(int decimal_number);
3848
int power(int base, int exponent);
39-
int convert(int decimal_number, int division_by_eight);
49+
int convert(int decimal_number);
4050
int reverse_number(int converted_number, int division_by_eight);
4151
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();
4255

4356
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();
4866
return 0;
4967
}
5068

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+
51106
void print_octal(int decimal_number, int result) {
52107
printf("The Octal of %d is %d.\n", decimal_number, result);
53108
}
@@ -78,7 +133,13 @@ I will probably need a function to count how many iterations I will need to conv
78133
I'll need that function once again when I will flip the number
79134
*/
80135

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);
82143
int result = 0, quotient, remainder, temp_decimal = decimal_number,
83144
divider = 8, power_counter = division_by_eight;
84145
for (int i = 0; i <= division_by_eight; ++i) {
@@ -93,6 +154,9 @@ int convert(int decimal_number, int division_by_eight) {
93154
temp_decimal = quotient;
94155
}
95156
result = reverse_number(result, division_by_eight);
157+
if (is_negative == TRUE) {
158+
result *= -1;
159+
}
96160
return result;
97161
}
98162

@@ -122,8 +186,13 @@ int number_of_eights(int decimal_number) {
122186
return counter;
123187
}
124188

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;
129198
}

0 commit comments

Comments
 (0)