<?php
$page_name = "Converting From Roman Numerals To Decimal<br />Web Script";
$menu_type = "programming";
$menu_name = "Read Roman Numerals";
$blog_num = 41;
$page_rate = 10;
$page_tags = array("erudition");
$page_date = "October 13, 2011";
include_once("./files/head.php");

$to_from = $_POST['to_from'];
$dec_num = ($to_from == 0) ? $_POST['dec_num'] : 0;
$roman_num = ($to_from == 0) ? to_roman_num($dec_num) : $_POST['roman_num'];
$dec_num = ($to_from == 1) ? from_roman_num($roman_num) : $dec_num;

echo "<p>Ok, part 2, converting back to a real number for roman numerals (<a href=\"./roman_numerals.php" . $admin_link . "\">part 1 is here</a>)...First of all, here are the functions to play with:</p>\n";

echo "<form enctype=\"multipart/form-data\" id=\"convert_to_roman\" action=\"./roman_numerals_2.php" . $admin_link . "\" method=\"POST\">\n";
  echo "<input type=\"hidden\" name=\"to_from\" id=\"to_from\" value=\"0\" />";
  echo "Decimal Number: <input type=\"text\" name=\"dec_num\" id=\"dec_num\" value=\"" . $dec_num . "\" size=\"6\"/>";
  echo "<input type=\"submit\" value=\"Submit\" />\n";
  echo "<br />Roman Number: " . $roman_num;
echo "</form>\n";
echo "<br />\n";
echo "<form enctype=\"multipart/form-data\" id=\"convert_to_dec\" action=\"./roman_numerals_2.php" . $admin_link . "\" method=\"POST\">\n";
  echo "<input type=\"hidden\" name=\"to_from\" id=\"to_from\" value=\"1\" />";
  echo "Roman Number: <input type=\"text\" name=\"roman_num\" id=\"roman_num\" value=\"" . $roman_num . "\" size=\"6\"/>";
  echo "<input type=\"submit\" value=\"Submit\" />\n";
  echo "<br />Decimal Number: " . $dec_num;
echo "</form>\n";
echo "</p>\n";

echo "<h3>Reverse Engineering</h3>\n";
echo "<p>I very quickly realized that the likes of 4 (IV) and 9 (IX) were once again going to bugger us up.  So we will hunt those down first in the array (after we get rid of all the stupidly huge numbers).</p>\n";
echo "<div class=\"code_block\">\n<code>\n";
$disp_code_001 = "function from_roman_num(\$temp) { \n  \$values = array(\"M6+\" => 1000000000, \"M5+\" => 100000000, \"M4+\" => 10000000,\n                  \"M3+\" => 1000000, \"M2+\" => 100000, \"M1+\" => 10000,\n                  \"CM\" => 900, \"M\" => 1000, \"CD\" => 400, \"D\" => 500,\n                  \"XC\" => 90, \"C\" => 100, \"XL\" => 40, \"L\" => 50, \"IX\" => 9,\n                  \"X\" => 10, \"IV\" => 4, \"V\" => 5, \"I\" => 1);";
highlight_string("<?php\n" . $disp_code_001 . "\n?>");
echo "</code>\n</div>\n";

echo "<p>Alright, we need to parse the string of letters based on the above array.  We also need to figure out how long the number is.  We also need to be able to check both 1 digit and 2 digit numbers.  We also need to make sure everything is in capitals.  I also hate it when people use single letters for variables, it doesn't cost you anything to use 3 or 5 letters and have other people be able to follow...</p>\n";
echo "<p>I'm using a bunch of sub string and string length hunts here.  When a number is found, we will onto the total and then remove it from the number.  Turns out my first guess was close, but not very.  So here is the one that does work:</p>\n";
echo "<div class=\"code_block\">\n<code>\n";
$disp_code_002 = "\n  strtoupper(\$temp); \n  \$temp_len = strlen(\$temp); \n  for (\$i=0; \$i <= \$temp_len; $i++) { \n    foreach (\$values as \$output => \$number) { \n      \$out_len = strlen(\$output); \n      if (substr(\$temp, \$i, \$out_len) == \$output) { \n        \$rebuild += \$number; \n        \$temp = substr(\$temp, 0, \$i) . str_repeat(\" \", \$out_len) . substr(\$temp, \$i + \$out_len); \n		\$i += \$out_len - 1; \n	  } \n    } \n  } \n  return \$rebuild; \n}";
highlight_string("<?php\n" . $disp_code_001 . $disp_code_002 . "\n?>");
echo "</code>\n</div>\n";

include_once("./files/close.php");
?>