fork(3) download
  1. <?php
  2.  
  3. function roman2number($roman){
  4. $conv = array(
  5. array("letter" => 'I', "number" => 1),
  6. array("letter" => 'V', "number" => 5),
  7. array("letter" => 'X', "number" => 10),
  8. array("letter" => 'L', "number" => 50),
  9. array("letter" => 'C', "number" => 100),
  10. array("letter" => 'D', "number" => 500),
  11. array("letter" => 'M', "number" => 1000),
  12. array("letter" => 0, "number" => 0)
  13. );
  14. $arabic = 0;
  15. $state = 0;
  16. $sidx = 0;
  17. $len = strlen($roman);
  18.  
  19. while ($len >= 0) {
  20. $i = 0;
  21. $sidx = $len;
  22.  
  23. while ($conv[$i]['number'] > 0) {
  24. if (strtoupper(@$roman[$sidx]) == $conv[$i]['letter']) {
  25. if ($state > $conv[$i]['number']) {
  26. $arabic -= $conv[$i]['number'];
  27. } else {
  28. $arabic += $conv[$i]['number'];
  29. $state = $conv[$i]['number'];
  30. }
  31. }
  32. $i++;
  33. }
  34.  
  35. $len--;
  36. }
  37.  
  38. return($arabic);
  39. }
  40.  
  41. echo roman2number('IV');
  42.  
Success #stdin #stdout 0.01s 23936KB
stdin
Standard input is empty
stdout
4