I would like to share with you a simple program that I wrote PHP as my programming language I called this program Roman Numeral To Decimal Converter in PHP. What this program will do is to ask the user to enter roman numeral value format and then the user can select convert to decimal button to convert the roman numeral value to its decimal value equivalent. The code is very short and simple I also added a function if the user give invalid value such as words, letters or even special character our program will not hang or malfunction in such situation encounter.
If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com. People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360.
Sample Output of Our Program
Program Listing
<html>
<title>Roman Numeral To Decimal Converter</title>
<style>
body {
font-size:20px;
font-family:arial;
color:blue;
}
</style>
<?php
error_reporting(0);
$values = $_POST['value'];
if(isset($_POST['check'])) {
if ($values==" ") {
echo "<script>";
echo "alert('It Cannot Be Empty!!! Please enter a number.');";
echo "</script>";
$values=" ";
$results=" ";
}
if ($values != " ") {
$natural_roman = array(1000 => 'M', 500 => 'D', 100 => 'C', 50 => 'L', 10 => 'X', 5 => 'V', 1 => 'I');
function convert_to_decimal($numeral)
{
global $natural_roman;
$numeral = str_replace(
array('CM', 'CD', 'XC', 'XL', 'IX', 'IV'),
array('DCCCC', 'CCCC', 'LXXXX', 'XXXX', 'VIIII', 'IIII'),
trim($numeral)
);
for ($total = 0, $n = 0; $n < strlen($numeral); $n++)
{
if ($numeral[$n] == 'I' && $n <> strlen($numeral)-1 && $numeral[$n+1] <> 'I')
$number = array_search($numeral[++$n], $natural_roman)-1;
else
$number = array_search($numeral[$n], $natural_roman);
$total += $number;
}
return $total;
}
$results .= "==== REPORT ==== ";
$results .= "<br><br>";
$results .= "The roman numeral " .$values." it's Decimal Equivalent is "
.convert_to_decimal($values).".";
}
}
if(isset($_POST['clear'])) {
$values=" ";
$results= " ";
}
?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
<br> <h1 align="center">Roman Numeral To Decimal Converter</h2>
<br>
<form action="" method="post">
Enter Roman Numeral : <input type="text" name="value" value="<?php echo $values; ?>" autofocus size=10/>
<br><br>
<input type="submit" name="check" value="Convert To Decimal"
title="Click here to convert roman numeral to decimal equivalent."/>
<input type="submit" name="clear" value="Clear"
title="Click here to clear text box and values on the screen"/>
</form>
<br>
<?php
echo $results;
?>
</body>
</html>
No comments:
Post a Comment