fork(1) download
  1. <?php
  2.  
  3. $input = "110022";
  4. $counter = $input;
  5. $male_or_female_unit = 1;
  6. $number = '';
  7. $digit_1 = '';
  8. $digit_2 = '';
  9. $digitTotal = '';
  10. $output = '';
  11.  
  12. function writeNumber() {
  13. global $number, $input;
  14.  
  15. if (strlen($input) < 4) {
  16. $number = $input;
  17. } elseif ((strlen($input) % 3) == 0) {
  18. $number = $input[0] . $input[1] . $input[2];
  19. } else {
  20. $number = substr($input, 0, (strlen($input) % 3));
  21. }
  22. }
  23.  
  24. function insertDigit() {
  25. global $output, $digitTotal, $digit_1, $digit_2, $number;
  26. $digitArray_1 = array("/0a$/", "/0b$/", "/0c$/", "/1a$/", "/1b$/", "/1c$/", "/2a$/", "/2b$/", "/2c$/", "/3a$/", "/3b$/", "/3c$/");
  27. $digitArray_2 = array("рубль", "рубля", "рублей", "тысяча", "тысячи", "тысяч", "миллион", "миллиона", "миллионов", "миллиард", "миллиарда", "миллиардов");
  28. $digitTotal = $digit_1 . $digit_2;
  29. if (($number == "000") && ($digit_1 != "0")) {
  30. $digitTotal = "";
  31. }
  32. $output = $output . " " . $digitTotal;
  33. $output = preg_replace($digitArray_1, $digitArray_2, $output);
  34. }
  35.  
  36. function checkDigit() {
  37. global $digit_1, $input, $male_or_female_unit;
  38. if ((1 <= strlen($input)) && (3 >= strlen($input))) { //0 - первая тройка, 1 - тысячи, 2 - мульёны, 3 - мульярды
  39. $digit_1 = "0";
  40. } elseif ((4 <= strlen($input)) && (6 >= strlen($input))) {
  41. $digit_1 = "1";
  42. $male_or_female_unit = 0;
  43. } elseif ((7 <= strlen($input)) && (9 >= strlen($input))) {
  44. $digit_1 = "2";
  45. } elseif ((10 <= strlen($input)) && (12 >= strlen($input))) {
  46. $digit_1 = "3";
  47. }
  48. }
  49.  
  50. function subnumber($x) {
  51. global $digit_2;
  52.  
  53. if (substr($x, -2, 2) == "00") {
  54. $digit_2 = "c";
  55. } elseif ($x[-1] == "1") {
  56. $digit_2 = "a";
  57. } elseif (($x[-1] == "2") || ($x[-1] == "3") || ($x[-1] == "4")) {
  58. $digit_2 = "b";
  59. } else {
  60. $digit_2 = "c";
  61. }
  62.  
  63. if (strlen($x) == 3) {
  64. hundreds($x);
  65. (preg_match('/(?<=\d)[1]\d/', $x)) ? ifTeenDecimals($x) : ifNotTeenDecimals($x);
  66. } elseif (strlen($x) == 1) {
  67. units($x);
  68. } else {
  69. (preg_match('/[1]\d/', $x)) ? ifTeenDecimals($x) : ifNotTeenDecimals($x);
  70. }
  71. }
  72.  
  73. function hundreds($x) {
  74. global $output;
  75. $numberReplace = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
  76. $hundredWords = array("", "cто", "двести", "триста", "четыреста", "пятьсот", "шестьсот", "семьсот", "восемьсот", "девятьсот");
  77. $output = $output . str_replace($numberReplace, $hundredWords, $x[0]) . " ";
  78. return $output;
  79. }
  80.  
  81. function ifTeenDecimals($x) {
  82. global $output, $digit_2;
  83. $numberReplace = array("10", "11", "12", "13", "14", "15", "16", "17", "18", "19");
  84. $decimalWords = array("десять", "одиннадцать", "двенадцать", "тринадцать", "четырнадцать", "пятнадцать", "шестнадцать", "семнадцать", "восемнадцать", "девятнадцать");
  85. $output = $output . str_replace($numberReplace, $decimalWords, substr($x, -2, 2));
  86. $digit_2 = "c";
  87. return $output;
  88. }
  89.  
  90. function units($x) {
  91. global $output, $male_or_female_unit, $digit_2;
  92.  
  93. if (($x[-1] != 1) && ($x[-1] != 2)) {
  94. $numberReplace = array("0", "3", "4", "5", "6", "7", "8", "9");
  95. $unitsWords = array("", "три", "четыре", "пять", "шесть", "семь", "восемь", "девять");
  96. $output = $output . str_replace($numberReplace, $unitsWords, $x[-1]);
  97. return $output;
  98. } else {
  99. if ($male_or_female_unit == 1) {
  100. $output = $output . str_replace(array("1", "2"), array("один", "два"), $x[-1]);
  101. return $output;
  102. } else {
  103. $output = $output . str_replace(array("1", "2"), array("одна", "две"), $x[-1]);
  104. return $output;
  105. }
  106. }
  107. }
  108.  
  109. function ifNotTeenDecimals($x) {
  110. global $output;
  111. $numberReplace = array("0", "2", "3", "4", "5", "6", "7", "8", "9");
  112. $decimalWords = array("", "двадцать", "тридцать", "сорок", "пятьдесят", "шестьдесят", "семьдесят", "восемьдесят", "девяносто");
  113. $output = $output . str_replace($numberReplace, $decimalWords, $x[-2]) . " ";
  114. return units($x);
  115. }
  116.  
  117. function computePart() {
  118. global $input, $number, $male_or_female_unit;
  119. $male_or_female_unit = 1;
  120. writeNumber();
  121. checkDigit();
  122. subnumber($number);
  123. insertDigit();
  124. $input = substr($input, strlen($number));
  125. }
  126.  
  127. for ($i = 0; floor(strlen($counter) / 3) >= $i; $i++) {
  128. computePart();
  129. $output = $output . " ";
  130. }
  131. $output = preg_replace(array("/\s+/", "/(рубл[яейь]+)(\s+)рублей/u"), array(" ", "$1"), $output);
  132.  
  133. echo "На вашем счету $output(" . $counter . " р.)";
  134.  
  135. ?>
Success #stdin #stdout #stderr 0.01s 82944KB
stdin
Standard input is empty
stdout
На вашем счету cто десять тысяч двадцать два рубля (110022 р.)
stderr
PHP Notice:  Uninitialized string offset: -1 in /home/Ayikfs/prog.php on line 55
PHP Notice:  Uninitialized string offset: -1 in /home/Ayikfs/prog.php on line 57
PHP Notice:  Uninitialized string offset: -1 in /home/Ayikfs/prog.php on line 57
PHP Notice:  Uninitialized string offset: -1 in /home/Ayikfs/prog.php on line 57
PHP Notice:  Uninitialized string offset: -2 in /home/Ayikfs/prog.php on line 113
PHP Notice:  Uninitialized string offset: -1 in /home/Ayikfs/prog.php on line 93
PHP Notice:  Uninitialized string offset: -1 in /home/Ayikfs/prog.php on line 93
PHP Notice:  Uninitialized string offset: -1 in /home/Ayikfs/prog.php on line 96