Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
roman_numerals_to_decimal.c File Reference

Conversion of roman numerals to decimal. More...

#include <assert.h>
#include <stdio.h>
#include <string.h>
Include dependency graph for roman_numerals_to_decimal.c:

Functions

int symbol (char symbol)
 for assert More...
 
int roman_to_decimal (char input[])
 Converts roman numerals into a decimal number. More...
 
static void test ()
 Self-test implementations. More...
 
int main ()
 Main function. More...
 

Detailed Description

Conversion of roman numerals to decimal.

Roman numerals are an ancient Roman numeral system consisting of the symbols I, V, X, L, C, D, and M

Author
Focusucof

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
118 {
119 test(); // run self-test implementations
120 return 0;
121}
static void test()
Self-test implementations.
Definition: roman_numerals_to_decimal.c:73
Here is the call graph for this function:

◆ roman_to_decimal()

int roman_to_decimal ( char  input[])

Converts roman numerals into a decimal number.

Parameters
inputInput roman numeral as a C-string
Returns
The converted number in decimal form
51 {
52 int result = 0; // result in decimal
53
54 for(int i = 0; i < strlen(input); i++) {
55 if(strlen(input) > i + 1) {
56 if(symbol(input[i]) >= symbol(input[i + 1])) {
57 result += symbol(input[i]); // add value to sum
58 } else {
59 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
60 i++; // skip over an extra symbol
61 }
62 } else {
63 result += symbol(input[i]); // add value to sum
64 }
65 }
66 return result;
67}
int symbol(char symbol)
for assert
Definition: roman_numerals_to_decimal.c:18
Here is the call graph for this function:

◆ symbol()

int symbol ( char  symbol)

for assert

for IO operations for strlen()

Convert roman numeral symbol to a decimal value helper function

Parameters
symbolRoman numeral char
Returns
Integer of decimal value for given symbol
18 {
19 int value = 0;
20 switch(symbol) {
21 case 'I':
22 value = 1;
23 break;
24 case 'V':
25 value = 5;
26 break;
27 case 'X':
28 value = 10;
29 break;
30 case 'L':
31 value = 50;
32 break;
33 case 'C':
34 value = 100;
35 break;
36 case 'D':
37 value = 500;
38 break;
39 case 'M':
40 value = 1000;
41 break;
42 }
43 return value;
44}
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
73 {
74 // 1st test
75 char input[] = "MCMIV";
76 int expected = 1904;
77
78 int output = roman_to_decimal(input);
79
80 printf("TEST 1\n");
81 printf("Input: %s\n", input);
82 printf("Expected Output: %d\n", expected);
83 printf("Output: %d\n", output);
84 assert(output == expected);
85 printf("== TEST PASSED ==\n\n");
86
87 // 2nd test
88 char input2[] = "MMMDCCXXIV";
89 expected = 3724;
90
91 output = roman_to_decimal(input2);
92
93 printf("TEST 2\n");
94 printf("Input: %s\n", input2);
95 printf("Expected Output: %d\n", expected);
96 printf("Output: %d\n", output);
97 assert(output == expected);
98 printf("== TEST PASSED ==\n\n");
99
100 // 3rd test
101 char input3[] = "III";
102 expected = 3;
103
104 output = roman_to_decimal(input3);
105
106 printf("TEST 3\n");
107 printf("Input: %s\n", input3);
108 printf("Expected Output: %d\n", expected);
109 printf("Output: %d\n", output);
110 assert(output == expected);
111 printf("== TEST PASSED ==\n\n");
112}
int roman_to_decimal(char input[])
Converts roman numerals into a decimal number.
Definition: roman_numerals_to_decimal.c:51
Here is the call graph for this function: