Skip to content

Commit e6029c9

Browse files
committed
Finished with octal to decimal
1 parent 9de7ec1 commit e6029c9

File tree

1 file changed

+62
-14
lines changed

1 file changed

+62
-14
lines changed

For loop/51. Octal to decimal.c

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,92 @@ The equivalent Decimal Number : 485
1414
+ 3. Function to count amount of digits
1515
+ 4. Write function to iterate over digits and return sum of powers
1616
+ 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
2020
*/
2121

2222
#include <stdio.h>
2323

24-
enum boolean_for_validation {
24+
enum boolean_for_input {
2525
TRUE = 1,
2626
FALSE = 0
2727
};
2828

29+
enum switch_for_negative_octal {
30+
IS_NEGATIVE = -1,
31+
IS_POSITIVE = 1
32+
};
33+
2934
int input_octal(int *octal);
3035
void print_invalid_input();
3136
int power(int base, int exponent);
3237
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();
3442

3543
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 {
4153
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);
4492
}
4593

46-
int find_decimal(int octal, int number_of_digits) {
94+
int find_decimal(int octal, int number_of_digits, int sign) {
4795
int decimal = 0, temp_octal = octal, power_of_ten;
4896
for (int i = number_of_digits; i > 0; --i) {
4997
power_of_ten = power(10, number_of_digits - 1);
5098
decimal += temp_octal / power_of_ten * power(8, number_of_digits - 1);
5199
temp_octal -= temp_octal / power_of_ten * power_of_ten;
52100
--number_of_digits;
53101
}
54-
return decimal;
102+
return decimal * sign;
55103
}
56104

57105
int find_number_of_digits(int octal) {

0 commit comments

Comments
 (0)