{"id":2411,"date":"2017-03-18T00:01:04","date_gmt":"2017-03-18T07:01:04","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2411"},"modified":"2017-03-25T08:25:57","modified_gmt":"2017-03-25T15:25:57","slug":"from-roman-to-decimal","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2411","title":{"rendered":"From Roman to Decimal"},"content":{"rendered":"<p>Roman numerals are composed of letters, so it makes sense that their &#8220;values&#8221; are input and displayed as strings. To translate from that string into an integer, your program must convert each character into its corresponding decimal value. Sounds simple, right?<br \/>\n<!--more--><br \/>\nWell, it is rather simple! For each valid Roman numeral character, you increment an integer value by a given amount. Assume that <code>ch<\/code> is a character in a string and <code>rn<\/code> is an <em>int<\/em> variable initialized to zero:<\/p>\n<p><code>if( ch == 'I' ) rn += 1;<br \/>\nif( ch == 'V' ) rn += 5;<br \/>\nif( ch == 'X' ) rn += 10;<br \/>\nif( ch == 'L' ) rn += 50;<br \/>\nif( ch == 'C' ) rn += 100;<br \/>\nif( ch == 'D' ) rn += 500;<br \/>\nif( ch == 'M' ) rn += 1000;<\/code><\/p>\n<p>From my books, you learn that a series of single character comparisons translates into a <em>switch-case<\/em> statement. So the basis of a <em>roman2arabic()<\/em> function would be a giant <em>switch-case<\/em> structure that examines each character in a Roman numeral string and creates a decimal tally. Here is just such a function:<\/p>\n<pre class=\"screen\">\r\nint roman2arabic(char *roman)\r\n{\r\n    int value;\r\n    char *r;\r\n\r\n    r = roman;\r\n    value = 0;\r\n    while(*r != '\\0')\r\n    {\r\n        switch(*r)\r\n        {\r\n            case 'M':\r\n                value += 1000;\r\n                break;\r\n            case 'D':\r\n                value += 500;\r\n                break;\r\n            case 'C':\r\n                value += 100;\r\n                break;\r\n            case 'L':\r\n                value += 50;\r\n                break;\r\n            case 'X':\r\n                value += 10;\r\n                break;\r\n            case 'V':\r\n                value += 5;\r\n                break;\r\n            case 'I':\r\n                value += 1;\r\n                break;\r\n            \/* terminate on whitespace *\/\r\n            case '\\n':\r\n            case ' ':\r\n            case '\\t':\r\n                return(value);\r\n                break;\r\n            default:\r\n                \/* invalid character *\/\r\n                return(0);\r\n        }\r\n        r++;\r\n    }\r\n    return(value);\r\n}<\/pre>\n<p>The <em>roman2arabic()<\/em> function accepts a string, <code>roman<\/code>, as input. The <em>int<\/em> variable <code>value<\/code> holds the accumulated total, the return value. The variable <code>value<\/code> is initialized to zero and a pointer <code>r<\/code> is created for use inside the function.<\/p>\n<p>The <em>while<\/em> loop processes string <code>r<\/code>. For valid Roman numerals (which are assumed to be upper case only), variable <code>value<\/code> is increased. If the string contains any whitespace, <code>value<\/code> is returned immediately. If the string contains any other character, zero is returned, which is an error condition. (Remember, a Roman numeral zero doesn&#8217;t exist.)<\/p>\n<p>This function works for all valid strings of Roman numerals. The characters can also be in any order. I could modify the function to ensure that the format is proper, but this code is more about translation than syntax. The only thing that the function doesn&#8217;t handle are the abbreviations: CM, CD, XC, XL, IX, and IV. To deal with those abbreviations, the <em>case<\/em> statements for characters I, X, and C require additional processing, such as:<\/p>\n<pre class=\"screen\">\r\n            case 'C':\r\n                if( *(r+1) == 'M')\r\n                {\r\n                    value += 900;\r\n                    r++;\r\n                }\r\n                else if( *(r+1) == 'D')\r\n                {\r\n                    value += 400;\r\n                    r++;\r\n                }\r\n                else\r\n                {\r\n                    value += 100;\r\n                }\r\n                break;<\/pre>\n<p>For each character, such as <code>C<\/code> above, the following character in the string is checked, <code>*(r+1)<\/code>. Characters <code>M<\/code> and <code>D<\/code> (for value <code>C<\/code>) hold special meaning, so the <em>if-else if-else<\/em> structure properly increments variable <code>value<\/code> accordingly. And pointer <code>r<\/code> is incremented if the proper suffix character is found.<\/p>\n<p>Adding three <em>if-else if-else<\/em> structures to the <em>roman2arabic()<\/em> function increases the code&#8217;s length substantially. A <em>main()<\/em> function, is also required (duh): Its job is to read the string and then output the result. You can view the entire code <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/03\/0318a.c\">here<\/a>.<\/p>\n<p>Here is sample output:<\/p>\n<pre><code>Type a Roman Numeric value: MMCCCLXII\r\nThe value is: 2362<\/code><\/pre>\n<p>And:<\/p>\n<pre><code>Type a Roman Numeric value: MCMLXXIV\r\nThe value is: 1974<\/code><\/pre>\n<p>I also wrote a smaller version (89 lines versus 109), which uses a specific function to process the CM, CD, XC, XL, IX, and IV values. You can view that code <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/03\/0318b.c\">here<\/a>.<\/p>\n<p>Just as you can translate Roman numerals to decimal, you can also translate the other way. That topic is covered in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2424\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Translate text input as a Roman numeral into its decimal value. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2411\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2411","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2411"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2411\/revisions"}],"predecessor-version":[{"id":2443,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2411\/revisions\/2443"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}