PHP 将数字转为罗马字符的简单示例

前端之家收集整理的这篇文章主要介绍了PHP 将数字转为罗马字符的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP将数字转换为罗马字符感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
  1. /**
  2. * PHP将数字转换为罗马字符
  3. *
  4. * @param
  5. * @arrange 512-笔记网: 512Pic.com
  6. **/
  7. function dec2roman ($number) {
  8. # Making input compatible with script.
  9. $number = floor($number);
  10. if($number < 0) {
  11. $linje = "-";
  12. $number = abs($number);
  13. }
  14. # Defining arrays
  15. $romanNumbers = array(1000,500,100,50,10,5,1);
  16. $romanLettersToNumbers = array("M" => 1000,"D" => 500,"C" => 100,"L" => 50,"X" => 10,"V" => 5,"I" => 1);
  17. $romanLetters = array_keys($romanLettersToNumbers);
  18. # Looping through and adding letters.
  19. while ($number) {
  20. for($pos = 0; $pos <= 6; $pos++) {
  21. # Dividing the remaining number with one of the roman numbers.
  22. $dividend = $number / $romanNumbers[$pos];
  23. # If that division is >= 1,round down,and add that number of letters to the string.
  24. if($dividend >= 1) {
  25. $linje .= str_repeat($romanLetters[$pos],floor($dividend));
  26. # Reduce the number to reflect what is left to make roman of.
  27. $number -= floor($dividend) * $romanNumbers[$pos];
  28. }
  29. }
  30. }
  31. # If I find 4 instances of the same letter,this should be done in a different way.
  32. # Then,subtract instead of adding (smaller number in front of larger).
  33. $numberOfChanges = 1;
  34. while($numberOfChanges) {
  35. $numberOfChanges = 0;
  36. for($start = 0; $start < strlen($linje); $start++) {
  37. $chunk = substr($linje,$start,1);
  38. if($chunk == $oldChunk && $chunk != "M") {
  39. $appearance++;
  40. } else {
  41. $oldChunk = $chunk;
  42. $appearance = 1;
  43. }
  44. # Was there found 4 instances.
  45. if($appearance == 4) {
  46. $firstLetter = substr($linje,$start - 4,1);
  47. $letter = $chunk;
  48. $sum = $firstNumber + $letterNumber * 4;
  49. $pos = array_search($letter,$romanLetters);
  50. # Are the four digits to be calculated together with the one before? (Example yes: VIIII = IX Example no: MIIII = MIV
  51. # This is found by checking if the digit before the first of the four instances is the one which is before the digits in the order
  52. # of the roman number. I.e. MDCLXVI.
  53. if($romanLetters[$pos - 1] == $firstLetter) {
  54. $oldString = $firstLetter . str_repeat($letter,4);
  55. $newString = $letter . $romanLetters[$pos - 2];
  56. } else {
  57. $oldString = str_repeat($letter,4);
  58. $newString = $letter . $romanLetters[$pos - 1];
  59. }
  60. $numberOfChanges++;
  61. $linje = str_replace($oldString,$newString,$linje);
  62. }
  63. }
  64. }
  65. return $linje;
  66. }
  67. function roman2dec ($linje) {
  68. # Fixing variable so it follows my convention
  69. $linje = strtoupper($linje);
  70. # Removing all not-roman letters
  71. $linje = ereg_replace("[^IVXLCDM]","",$linje);
  72. print("\$linje = $linje<br>");
  73. # Defining variables
  74. $romanLettersToNumbers = array("M" => 1000,"I" => 1);
  75. $oldChunk = 1001;
  76. # Looping through line
  77. for($start = 0; $start < strlen($linje); $start++) {
  78. $chunk = substr($linje,1);
  79. $chunk = $romanLettersToNumbers[$chunk];
  80. if($chunk <= $oldChunk) {
  81. $calculation .= " + $chunk";
  82. } else {
  83. $calculation .= " + " . ($chunk - (2 * $oldChunk));
  84. }
  85. $oldChunk = $chunk;
  86. }
  87. # Summing it up
  88. eval("\$calculation = $calculation;");
  89. return $calculation;
  90. }
  91. # Implementation of the array_search function. Works only with numerical arrays.
  92. function array_search($searchString,$array) {
  93. foreach ($array as $content) {
  94. if($content == $searchString) {
  95. return $pos;
  96. }
  97. $pos++;
  98. }
  99. }
  100. /*** 来自编程之家 jb51.cc(jb51.cc) ***/

猜你在找的PHP相关文章