mode = -1; $this->lastmode = -1; $this->rollingvalue = 0; } function ToRoman($MyNumber) { $m = $MyNumber; $romanstr = ""; while ($m >= 1000) { $romanstr .= "M"; $m -= 1000; } if ($m >= 900) { $romanstr .= "CM"; $m -= 900; } if ($m >= 500) { $romanstr .= "D"; $m -= 500; } if ($m >= 400) { $romanstr .= "CD"; $m -= 400; } while ($m >= 100) { $romanstr .= "C"; $m -= 100; } if ($m >= 90) { $romanstr .= "XC"; $m -= 90; } if ($m >= 50) { $romanstr .= "L"; $m -= 50; } if ($m >= 40) { $romanstr .= "XL"; $m -= 40; } while ($m >= 10) { $romanstr .= "X"; $m -= 10; } if ($m >= 9) { $romanstr .= "IX"; $m -= 9; } if ($m >= 5) { $romanstr .= "V"; $m -= 5; } if ($m == 4) { $romanstr .= "IV"; $m -= 4; } while ($m > 0) { $romanstr .= "I"; $m -= 1; } // echo "

".$romanstr."= ToRoman(".$MyNumber.")

"; return $romanstr; } function NumeralToNumber($numeral) { // $retval = 0; switch ($numeral) { case 'I' : return 1; break; case 'V' : return 5; break; case 'X' : return 10; break; case 'L' : return 50; break; case 'C' : return 100; break; case 'D' : return 500; break; case 'M' : return 1000; break; default : return 0; } // echo "

".$retval."= NumeralToNumber(".$numeral.")

"; // return $retval; } function ToDecimal($Str) { $romanstrlen = strlen($Str); // echo "

Found ".$romanstrlen." characters to process

"; $lastvalue = 0; $totalvalue = 0; /* Run through the string of numerals in reverse order if the current digit is greater or equal to the last, add it if the current digit is less than the last, subtract it This handles the CM, IX, pairs, I realise that you could get some illegal pairs here... IM, XM, etc */ for ($ii = ($romanstrlen - 1) ; $ii >= 0; $ii--) { $number = $this->NumeralToNumber(substr($Str, $ii, 1)); // echo "

Proccessed:".substr($Str, $ii, 1)." to get :".$number."

"; if ($number < $lastvalue) { $totalvalue -= $number; } else { $totalvalue += $number; } $lastvalue = $number; } // echo "

".$totalvalue."= ToDecimal(".$Str.")

"; return $totalvalue; } var $mode; // stores the current calculating mode (+ - * / ) var $lastmode; // stores the last calculating mode var $rollingvalue; // stores the running total var $roman1; // stores the string value being typed in } session_start(); /* ************************************************************************************************* */ /* Roman Calculator in PHP */ /* This code hopefully presents the operation of a roman numeral calculator */ /* */ /* */ /* History: */ /* AM 11/09/2002 Altered to account for PHP 4.2.x differences in session handling */ /* AM 11/24/2004 Altered output to use text rather than input boxes */ /* Corrected title tag output */ /* AM 12/07/2004 Change to correct php tags */ /* AM 12/10/2004 Changes to make register_globals off work */ /* */ /* ************************************************************************************************* */ /* ************************************************************************************************* */ /* ************************************************************************************************* */ /* ************************************************************************************************* */ /* ************************************************************************************************* */ // There should be a roman calculator object if we did this before if (isset($_SESSION['rc'])) { $rc = $_SESSION['rc']; $calcvalue = $rc->roman1; } else { // If we have no pre-existing calculator object then we are // here for the first time, so create one $rc = new romancalculator; $_SESSION['rc'] = $rc; $calcvalue = ""; } // Add the input value to the current (ongoing) value string if (isset($_REQUEST['I'])) { $rc->roman1 = $calcvalue."I"; //echo "

value = I

"; } else if (isset($_REQUEST['V'])) { $rc->roman1 = $calcvalue."V"; //echo "

value = V

"; } else if (isset($_REQUEST['X'])) { $rc->roman1 = $calcvalue."X"; //echo "

value = X

"; } else if (isset($_REQUEST['L'])) { $rc->roman1 = $calcvalue."L"; //echo "

value = L

"; } else if (isset($_REQUEST['D'])) { $rc->roman1 = $calcvalue."D"; //echo "

value = D

"; } else if (isset($_REQUEST['C'])) { $rc->roman1 = $calcvalue."C"; //echo "

value = C

"; } else if (isset($_REQUEST['M'])) { $rc->roman1 = $calcvalue."M"; //echo "

value = M

"; } else if (isset($_REQUEST['clear'])) { // remove the last calc inputs $rc->roman1 = ""; $rc->mode = $rc->lastmode; } else if (isset($_REQUEST['reset'])) { // reset the entire calculating engine $rc->roman1 = ""; $rc->rollingvalue = 0; $rc->mode = -1; $rc->lastmode = -1; } else { // Do calc's // Act on last mode // Then set mode to whatever we need to // If there is no data ignore input if ($rc->rollingvalue == 0 && trim($rc->roman1) == "") { // ignore } else { if (! trim($rc->roman1) == "") { // Now do a calc based on the current mode // do the calculation? switch ($rc->mode) { case 0: // do an add $rc->rollingvalue += $rc->ToDecimal($rc->roman1); break; case 1: // do a minus $rc->rollingvalue -= $rc->ToDecimal($rc->roman1); break; case 2: // do a multiply $rc->rollingvalue *= $rc->ToDecimal($rc->roman1); break; case 3: // do a divide $rc->rollingvalue = round($rc->rollingvalue / $rc->ToDecimal($rc->roman1)); break; case -1: // some sort of error $rc->rollingvalue = $rc->ToDecimal($rc->roman1); break; default: break; } $rc->roman1 = ""; } } $rc->lastmode = $rc->mode; if (isset($_REQUEST['plus'])) { $rc->mode = 0; } if (isset($_REQUEST['minus'])) { $rc->mode = 1; } if (isset($_REQUEST['multiply'])) { $rc->mode = 2; } if (isset($_REQUEST['divide'])) { $rc->mode = 3; } } /* ************************************************************************************************* */ /* ************************************************************************************************* */ // save the currect roman calc values $_SESSION['rc'] = $rc; ?> <?php echo htmlentities("<^l<vl^tor"); ?>

SPQR Calculator

Home Page

Enter Result
roman1."\">"; echo "{$rc->roman1}\n"; } else { echo " "; } echo "rollingvalue."\" >"; echo "roman1."\" >"; ?> ToRoman($rc->rollingvalue); if (trim(strlen($resultvalue) == 0)) { $resultvalue = " "; } echo "".$resultvalue."\n"; ?>
   

This is a link to a code listing