Question

In: Computer Science

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.

Solutions

Expert Solution

here i have create two function and create Roman numeral to modern Hindu–Arabic numeral

Code Roman_Numeral.php:

<?php
//Roman_symbol_value function returns roman symbol value
function Roman_symbol_value($no)
{
if ($no == 'I')
return 1;
if ($no == 'V')
return 5;
if ($no == 'X')
return 10;
if ($no == 'L')
return 50;
if ($no == 'C')
return 100;
if ($no == 'D')
return 500;
if ($no == 'M')
return 1000;
return -1;
}
//Roman_numeral function it is a given roman numeral
function Roman_numeral(&$string_value)
{
//asign result value zero
$result = 0;
   //loop continues until string_value length  
for ($i = 0; $i < strlen($string_value); $i++)
{

//get value of s[i] and store in a1
$a1 = Roman_symbol_value($string_value[$i]);
if ($i+1 < strlen($string_value))
{
//get value of s[i] and store in a2
$a2 = Roman_symbol_value($string_value[$i + 1]);
// compare a1 and a2
if ($a1 >= $a2)
{
$result = $result + $a1; //store value in result
}
else
{
$result = $result + $a2 - $a1;
$i++;
}
}
else
{
$result = $result + $a1;
$i++;
}
}
return $result;
}
//basic inputs
$str ="VI";
$str1="IV";  
$str2 ="MCMXC";
$str3="IX";  
//basic output in table format with function call Roman_numeral
echo "<table border=1><caption>Roman Numeral</caption><tr><th>Input</th><th>Output</th></tr>",
"<tr><td>",$str,"</td>","<td>",Roman_numeral($str),"</td></tr>",
           "<tr><td>",$str1,"</td>","<td>",Roman_numeral($str1),"</td></tr>",
           "<tr><td>",$str2,"</td>","<td>",Roman_numeral($str2),"</td></tr>",
               "<tr><td>",$str3,"</td>","<td>",Roman_numeral($str3),"</td></tr>";
?>

Output:

Thank you if you have any query regarding above answer please ask me in comment box.

if you like my work appreciate with thumbs up.

Thank You.


Related Solutions

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.
using the Oyez audio file and transcript (Links to an external site.)Links to an external site....
using the Oyez audio file and transcript (Links to an external site.)Links to an external site. (go to "Opinion Announcement," which is a seven minute audio clip on the court's decision on the Snyder v. Phelps case), briefly summarize the facts of the Snyder v. Phelps (2011) case. By using facts presented, clearly explain why the Supreme Court ruled in favor of the Westboro Baptist Church. Then, tell us why you agree/disagree with the Court’s decision.
Go to the Bureau of Economic Analysis (Links to an external site.)Links to an external site....
Go to the Bureau of Economic Analysis (Links to an external site.)Links to an external site. (BEA) website and look at quarterly data from the last few years of the National Accounts. Can you make a decision of what part of the business cycle the U.S. economy is currently in? Why? What factors lead you to this conclusion? You may want to do additional research of sources to reach a conclusion. If so, please identify the sources that added to...
Visit the Bureau of Labor Statistics (Links to an external site.)Links to an external site. website...
Visit the Bureau of Labor Statistics (Links to an external site.)Links to an external site. website and explore the Consumer Price Index section. Read the "Current CPI Economic News Releases" and summarize the latest release. Did the CPI increase or decrease? What caused the index to increase or decrease? Explain. Visit the inflation calculator (Links to an external site.)Links to an external site. section and easily find out how the buying power of the dollar has changed over the years....
Watch the Shifts in Aggregate Demand video (Links to an external site.)Links to an external site....
Watch the Shifts in Aggregate Demand video (Links to an external site.)Links to an external site. (embedded in the reading) and summarize the factors that cause the AD curve to shift. Did the video help you understand the model? Pick one of the four scenarios listed below the video and explain in your own words which way the AD curve will shift. At least 200 words
INSTRUCTIONS Watch the Starbucks (Links to an external site.)Links to an external site. video and answer...
INSTRUCTIONS Watch the Starbucks (Links to an external site.)Links to an external site. video and answer the question below. How has leadership impacted Starbucks' current growth and how will their leaders need to evolve in order to maintain the company's success given the threats to the specialty coffee industry? in 300 word
Again, please use www.federalreserve.gov (Links to an external site.)Links to an external site. and find what...
Again, please use www.federalreserve.gov (Links to an external site.)Links to an external site. and find what the current Fed policy is for interest rates. Do you think this policy is appropriate for the economy? Why or why not?
Centers for Disease Control and Prevention. (2017). Stroke(Links to an external site.)Links to an external site....
Centers for Disease Control and Prevention. (2017). Stroke(Links to an external site.)Links to an external site. (Links to an external site.)Links to an external site.. Retrieved from http://www.cdc.gov/stroke/index.htm Initial Discussion Post: Compose a response to the following questions: As an RN in an emergency room what signs and symptoms would you associate with someone having a stroke? Would these symptoms be different in different cultures and genders? Identify one culture or gender and discuss their risk factors and presentation of...
1. go to www.sec.gov (Links to an external site.)Links to an external site. 2. Hover your...
1. go to www.sec.gov (Links to an external site.)Links to an external site. 2. Hover your cursor over "Filings" in the menu bar across the top 3. click on "company filings search" that pops up when you hover 4. in the FAST SEARCH box on the far right type in "AEO" for the ticker and click "search" for American Eagle. For Buckle type in "BKE". 5. in the "filings" column on the far left look for the most recent "10-K"...
Edmunson Electrical Distribution (www.edmunson-electrical.co.uk (Links to an external site.)Links to an external site.) is a leading...
Edmunson Electrical Distribution (www.edmunson-electrical.co.uk (Links to an external site.)Links to an external site.) is a leading distributor of electrical equipment and components with over 230 branches in the United Kingdom. The company is a wholesaler of electrical products acting as an intermediary between manufacturers and customers. Accounts are classified according to turnover and margins achieved. The ‘bread and butter’ of the business is the electrical contractor, who provides high turnover but low margins. The more significant the purchases, the higher...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT