-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathroman_numerals_to_decimal.c
121 lines (107 loc) · 3.02 KB
/
roman_numerals_to_decimal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* @file
* @brief Conversion of [roman numerals](https://en.wikipedia.org/wiki/Roman_numerals) to decimal
* @details Roman numerals are an ancient Roman numeral system consisting of the symbols I, V, X, L, C, D, and M
*
* @author [Focusucof](https://github.com/Focusucof)
*/
#include <assert.h> /// for assert
#include <stdio.h> /// for IO operations
#include <string.h> /// for strlen()
/**
* @brief Convert roman numeral symbol to a decimal value helper function
* @param symbol Roman numeral char
* @returns Integer of decimal value for given symbol
*/
int symbol(char symbol) {
int value = 0;
switch(symbol) {
case 'I':
value = 1;
break;
case 'V':
value = 5;
break;
case 'X':
value = 10;
break;
case 'L':
value = 50;
break;
case 'C':
value = 100;
break;
case 'D':
value = 500;
break;
case 'M':
value = 1000;
break;
}
return value;
}
/**
* @brief Converts roman numerals into a decimal number
* @param input Input roman numeral as a C-string
* @returns The converted number in decimal form
*/
int roman_to_decimal(char input[]) {
int result = 0; // result in decimal
for(int i = 0; i < strlen(input); i++) {
if(strlen(input) > i + 1) {
if(symbol(input[i]) >= symbol(input[i + 1])) {
result += symbol(input[i]); // add value to sum
} else {
result += symbol(input[i + 1]) - symbol(input[i]); // if the current symbol is smaller than the next (ex. IV), subtract it from the next symbol
i++; // skip over an extra symbol
}
} else {
result += symbol(input[i]); // add value to sum
}
}
return result;
}
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
// 1st test
char input[] = "MCMIV";
int expected = 1904;
int output = roman_to_decimal(input);
printf("TEST 1\n");
printf("Input: %s\n", input);
printf("Expected Output: %d\n", expected);
printf("Output: %d\n", output);
assert(output == expected);
printf("== TEST PASSED ==\n\n");
// 2nd test
char input2[] = "MMMDCCXXIV";
expected = 3724;
output = roman_to_decimal(input2);
printf("TEST 2\n");
printf("Input: %s\n", input2);
printf("Expected Output: %d\n", expected);
printf("Output: %d\n", output);
assert(output == expected);
printf("== TEST PASSED ==\n\n");
// 3rd test
char input3[] = "III";
expected = 3;
output = roman_to_decimal(input3);
printf("TEST 3\n");
printf("Input: %s\n", input3);
printf("Expected Output: %d\n", expected);
printf("Output: %d\n", output);
assert(output == expected);
printf("== TEST PASSED ==\n\n");
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}