Question

In: Computer Science

Using PHP, design a function that given a Roman numeral (Wikipedia link.), in input, is able...

Using PHP, design a function that given a Roman numeral (Wikipedia link.), in input, is able to compute the modern Hindu–Arabic numeral (wikipedia link), system representation (aka, 0123456789).

For example:

Input Output
VI 6
IV 4
MCMXC 1990
IX 9
  • Be sure to add all the necessary checks and at least one testing function.
  • Try to split the logic in more small functions.

Solutions

Expert Solution

romantonumber.php:

<!DOCTYPE html>
<html>
<head>
   <title>Roman to Hindu-Arabic Number</title>
   <style>
   table{
       width:50%;
       border-collapse:collapse;
       text-align:center;
   }
</style>
</head>
<body>
<center>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" id="roman_input" name="roman_input" placeholder="Enter Roman Number">
<input type="submit" value="Convert"/>
</form>
<br/>
<?php

$roman_numbers = array('M' => 1000,'CM' => 900,'D' => 500,'CD' => 400,'C' => 100,'XC' => 90,'L' => 50,'XL' => 40,'X' => 10,'IX' => 9,'V' => 5,'IV' => 4,'I' => 1);
if(!isset($_POST['roman_input']))
   return 0;
else{
$roman_input = strtoupper($_POST['roman_input']);
$output = 0;

foreach ($roman_numbers as $key => $value) {
while (strpos($roman_input, $key) === 0) {
$output += $value;
$roman_input = substr($roman_input, strlen($key));
}
}

//output
echo "<table border='1'>";
echo "<tr><th>Input</th><th>Output</th></tr>";
echo "<tr><td>".strtoupper($_POST['roman_input'])."</td><td>".$output."</td></tr>";
echo "</table>";
}
?>
</center>
</body>
</html>

Output Screenshots:


Related Solutions

Using PHP, design a function that given a Roman numeral (Links to an external site.)Links to...
Using PHP, design a function that given a Roman numeral (Links to an external site.)Links to an external site. in input, is able to compute the modern Hindu–Arabic numeral (Links to an external site.)Links to an external site. system representation (aka, 0123456789). For example: Input Output VI 6 IV 4 MCMXC 1990 IX 9 Be sure to add all the necessary checks and at least one testing function. Try to split the logic in more small functions.
Write a simple java program to list roman numeral for a given range of numbers. Roman...
Write a simple java program to list roman numeral for a given range of numbers. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M. I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 Roman numerals are usually written largest to smallest from left to right. But a number like 4 is not written as IIII. It is written as IV. Because...
Write a recursive function that converts Old Roman Numeral to base 10 values.
C++ program Write a recursive function that converts Old Roman Numeral to base 10 values. Print out the Old Roman Number and it base 10 equivalents. You will read all the digits of a Roman number into an array of characters. You will process each Roman digit in the array using ONLY POINTERS. Putting a ‘\0’ at the end of the input characters allows you to print the string easier. Use the following input to test your function:    DVIII, MMMDCCCCLXXXXIIII,...
PHP You will be using the object oriented features of PHP to design a music album...
PHP You will be using the object oriented features of PHP to design a music album processing page. First you will build a form page called addAlbum.php. This form will contain text fields for album title, artist, publisher (Sony, BMI, etc.) and genre. Add two more fields of your choice. You will post this form to the file process.php. If all the fields have values, we will create a new Album object and print the details of the object. You...
Design a four-link mechanism when the motions of the input and the output links are governed...
Design a four-link mechanism when the motions of the input and the output links are governed by a function y = x2 and x varies from 0 to 2 with an interval of 1. Assume to vary from 50° to 150° and from 80° to 160°.
- Design and implement a function with no input parameters. The function keeps receiving a number...
- Design and implement a function with no input parameters. The function keeps receiving a number from input (user) and adds the numbers together. The application keeps doing it until the user enter 0. Then the application will stop and print the total sum and average of the numbers the user had entered.
Design an ER Diagram for the given problem. Link the tables that are related and implement...
Design an ER Diagram for the given problem. Link the tables that are related and implement your design using MySQL. Insert at least a minimum of 5 records. Ensure that the data to be added are related to other tables. Post here the screenshot of the following: Entity Relationship Diagram with attributes, PK, and FK. Use the "describe" command to view the structure of the tables. Use the "select" command to view the contents of the tables. Company ABC has...
Create a database and design an ER diagram for the given question. Must link the related...
Create a database and design an ER diagram for the given question. Must link the related tables then implement the design using MySQL. Insert at least 5 records. Ensure that the data to be added are related to other tables. Follow this format in creating the database and table: Database format: databasename_yourname Table format: tablename_yourname QUESTION: Company ABC has the following business rules. A department employs many employees, but each employee is employed by only one department. A division operates...
Using Python Question 1 Write an input function. Then use it to supply the input to...
Using Python Question 1 Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even. Question 2 Write function that asks for input from a user. If the user types 'end', the program exits, otherwise it just keeps going.
Pythons code using idle Write an input function. Then use it to supply the input to...
Pythons code using idle Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT