<?php
/*
Задача: Дана сумма, находящаяся в банке на счету, в рублях. Вывести ее в текстовом виде вроде "шестнадцать миллионов десять тысяч три (16010003) рубля".
Решал: с 2.09.16 по 3.09.16
*/
header ( 'Content-Type: text/html; charset=utf-8' ) ;
function spellSmallNumber( $number , $isFemale )
{
0 => 'ноль' , 1 => 'один' , 2 => 'два' ,
3 => 'три' , 4 => 'четыре' , 5 => 'пять' ,
6 => 'шесть' , 7 => 'семь' , 8 => 'восемь' ,
9 => 'девять' , 10 => 'десять' , 11 => 'одиннадцать' ,
12 => 'двенадцать' , 13 => 'тринадцать' , 14 => 'четырнадцать' ,
15 => 'пятнадцать' , 16 => 'шестнадцать' , 17 => 'семнадцать' ,
18 => 'восемнадцать' , 19 => 'девятнадцать' , 20 => 'двадцать' ,
30 => 'тридцать' , 40 => 'сорок' , 50 => 'пятьдесят' ,
60 => 'шестьдесят' , 70 => 'семьдесят' , 80 => 'восемьдесят' ,
90 => 'девяносто' , 100 => 'сто' , 200 => 'двести' ,
300 => 'триста' , 400 => 'четыреста' , 500 => 'пятьсот' ,
600 => 'шестьсот' , 700 => 'семьсот' , 800 => 'восемьсот' ,
900 => 'девятсот'
) ;
1 => 'одна' , 2 => 'две'
) ;
if ( $number == 0 ) {
$result [ ] = $spelling [ $number ] ;
}
$hundreds = floor ( $number / 100 ) * 100 ; if ( $hundreds != 0 ) {
$result [ ] = $spelling [ $hundreds ] ;
}
$lastTwoDigit = $number % 100 ;
if ( ( $lastTwoDigit >= 11 && $lastTwoDigit <= 20 ) || ( ( $lastTwoDigit % 10 == 0 ) && $lastTwoDigit != 0 ) ) {
$result [ ] = $spelling [ $lastTwoDigit ] ;
} else {
$tens = floor ( $lastTwoDigit / 10 ) * 10 ;
if ( $tens != 0 ) {
$result [ ] = $spelling [ $tens ] ;
}
$units = $lastTwoDigit % 10 ;
if ( $units != 0 ) {
if ( $isFemale && ( $units == 1 || $units == 2 ) ) {
$result [ ] = $femaleSpelling [ $units ] ;
} else {
$result [ ] = $spelling [ $units ] ;
}
}
}
return $text ;
}
function inclineWord( $number , $word1 , $word2 , $word5 )
{
$lastTwoDigit = $number % 100 ;
if ( $lastTwoDigit >= 11 && $lastTwoDigit <= 14 ) {
return $word5 ;
} elseif ( $lastTwoDigit % 10 == 1 ) {
return $word1 ;
} elseif ( $lastTwoDigit % 10 >= 2 && $lastTwoDigit % 10 <= 4 ) {
return $word2 ;
} else {
return $word5 ;
}
}
function numberToText( $number )
{
$result = '' ;
1 => array ( ' тысяча ' , ' тысячи ' , ' тысяч ' ) , 2 => array ( ' миллион ' , ' миллиона ' , ' миллионов ' ) ) ;
$workNumber = $number ;
while ( $workNumber > 999 ) {
$numberArray [ ] = $workNumber % 1000 ;
$workNumber = ( int
) floor ( $workNumber / 1000 ) ; }
$numberArray [ ] = $workNumber ;
for ( $i = count ( $numberArray ) - 1 ; $i >= 0 ; $i -- ) { if ( $i == 1 ) {
$isFemale = 1 ;
} else {
$isFemale = 0 ;
}
if ( count ( $numberArray ) > 1 && $numberArray [ $i ] == 0 ) { continue ;
}
$result .= spellSmallNumber( $numberArray [ $i ] , $isFemale ) . inclineWord( $numberArray [ $i ] , $numberClass [ $i ] [ 0 ] , $numberClass [ $i ] [ 1 ] , $numberClass [ $i ] [ 2 ] ) ;
}
$result = $result . " ($number ) " . inclineWord( $number , 'рубль' , 'рубля' , 'рублей' ) ;
return $result ;
}
$result = numberToText( $number ) ;
echo "На вашем счету: $result \n " ;
$result = numberToText( $number ) ;
echo "На вашем счету: $result \n " ;
$result = numberToText( $number ) ;
echo "На вашем счету: $result \n " ;
$result = numberToText( $number ) ;
echo "На вашем счету: $result \n " ;
$result = numberToText( $number ) ;
echo "На вашем счету: $result \n " ;
PD9waHAKICAgIC8qCiAgICAgICAg0JfQsNC00LDRh9CwOiDQlNCw0L3QsCDRgdGD0LzQvNCwLCDQvdCw0YXQvtC00Y/RidCw0Y/RgdGPINCyINCx0LDQvdC60LUg0L3QsCDRgdGH0LXRgtGDLCDQsiDRgNGD0LHQu9GP0YUuINCS0YvQstC10YHRgtC4INC10LUg0LIg0YLQtdC60YHRgtC+0LLQvtC8INCy0LjQtNC1INCy0YDQvtC00LUgItGI0LXRgdGC0L3QsNC00YbQsNGC0Ywg0LzQuNC70LvQuNC+0L3QvtCyINC00LXRgdGP0YLRjCDRgtGL0YHRj9GHINGC0YDQuCAoMTYwMTAwMDMpINGA0YPQsdC70Y8iLgogICAgICAgIAogICAgICAgINCg0LXRiNCw0Ls6INGBIDIuMDkuMTYg0L/QviAzLjA5LjE2CiAgICAqLwogICAgCiAgICBlcnJvcl9yZXBvcnRpbmcoLTEpOwogICAgaGVhZGVyKCdDb250ZW50LVR5cGU6IHRleHQvaHRtbDsgY2hhcnNldD11dGYtOCcpOwogICAgCiAgICBmdW5jdGlvbiBzcGVsbFNtYWxsTnVtYmVyKCRudW1iZXIsICRpc0ZlbWFsZSkKICAgIHsKICAgICAgICAkcmVzdWx0ID0gYXJyYXkoKTsKICAgIAogICAgICAgICRzcGVsbGluZyA9IGFycmF5KAogICAgICAgICAgICAwICAgPT4gJ9C90L7Qu9GMJywgICAgICAgICAgMSAgID0+ICfQvtC00LjQvScsICAgICAgICAgICAgMiA9PiAn0LTQstCwJywgCiAgICAgICAgICAgIDMgICA9PiAn0YLRgNC4JywgICAgICAgICAgIDQgICA9PiAn0YfQtdGC0YvRgNC1JywgICAgICAgICAgNSA9PiAn0L/Rj9GC0YwnLAogICAgICAgICAgICA2ICAgPT4gJ9GI0LXRgdGC0YwnLCAgICAgICAgIDcgICA9PiAn0YHQtdC80YwnLCAgICAgICAgICAgIDggPT4gJ9Cy0L7RgdC10LzRjCcsCiAgICAgICAgICAgIDkgICA9PiAn0LTQtdCy0Y/RgtGMJywgICAgICAgIDEwICA9PiAn0LTQtdGB0Y/RgtGMJywgICAgICAgICAxMSA9PiAn0L7QtNC40L3QvdCw0LTRhtCw0YLRjCcsCiAgICAgICAgICAgIDEyICA9PiAn0LTQstC10L3QsNC00YbQsNGC0YwnLCAg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GCJwogICAgICAgICk7CiAgICAgICAgCiAgICAgICAgJGZlbWFsZVNwZWxsaW5nID0gYXJyYXkoCiAgICAgICAgICAgIDEgPT4gJ9C+0LTQvdCwJywgMiA9PiAn0LTQstC1JwogICAgICAgICk7CiAgICAgICAgICAgIAogICAgICAgIGlmICgkbnVtYmVyID09IDApIHsKICAgICAgICAgICAgJHJlc3VsdFtdID0gJHNwZWxsaW5nWyRudW1iZXJdOwogICAgICAgIH0gICAgCiAgICAgICAgICAgIAogICAgICAgICRodW5kcmVkcyA9IGZsb29yKCRudW1iZXIgLyAxMDApICogMTAwOwogICAgICAgIGlmICgkaHVuZHJlZHMgIT0gMCkgewogICAgICAgICAgICAkcmVzdWx0W10gPSAkc3BlbGxpbmdbJGh1bmRyZWRzXTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgJGxhc3RUd29EaWdpdCA9ICRudW1iZXIgJSAxMDA7CiAgICAgICAgaWYgKCgkbGFzdFR3b0RpZ2l0ID49IDExICYmICRsYXN0VHdvRGlnaXQgPD0gMjApIHx8ICgoJGxhc3RUd29EaWdpdCAlIDEwID09IDApICYmICRsYXN0VHdvRGlnaXQgIT0gMCkpIHsKICAgICAgICAgICAgJHJlc3VsdFtdID0gJHNwZWxsaW5nWyRsYXN0VHdvRGlnaXRdOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICR0ZW5zID0gZmxvb3IoJGxhc3RUd29EaWdpdCAvIDEwKSAqIDEwOwogICAgICAgIAogICAgICAgICAgICBpZiAoJHRlbnMgIT0gMCkgewogICAgICAgICAgICAgICAgJHJlc3VsdFtdID0gJHNwZWxsaW5nWyR0ZW5zXTsKICAgICAgICAgICAgfQogICAgICAgIAogICAgICAgICAgICAkdW5pdHMgPSAkbGFzdFR3b0RpZ2l0ICUgMTA7CgogICAgICAgICAgICBpZiAoJHVuaXRzICE9IDApIHsKICAgICAgICAgICAgICAgIGlmICgkaXNGZW1hbGUgJiYgKCR1bml0cyA9PSAxIHx8ICR1bml0cyA9PSAyKSkgewogICAgICAgICAgICAgICAgICAgICRyZXN1bHRbXSA9ICRmZW1hbGVTcGVsbGluZ1skdW5pdHNdOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAkcmVzdWx0W10gPSAkc3BlbGxpbmdbJHVuaXRzXTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSAKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgJHRleHQgPSBpbXBsb2RlKCcgJywgJHJlc3VsdCk7CiAgICAgICAgcmV0dXJuICR0ZXh0OwogICAgfQogICAgCiAgICBmdW5jdGlvbiBpbmNsaW5lV29yZCgkbnVtYmVyLCAkd29yZDEsICR3b3JkMiwgJHdvcmQ1KQogICAgewogICAgICAgICAkbGFzdFR3b0RpZ2l0ID0gJG51bWJlciAlIDEwMDsKICAgICAgICAgCiAgICAgICAgaWYgKCRsYXN0VHdvRGlnaXQgPj0gMTEgJiYgJGxhc3RUd29EaWdpdCA8PSAxNCkgewogICAgICAgICAgICByZXR1cm4gJHdvcmQ1OwogICAgICAgIH0gZWxzZWlmICgkbGFzdFR3b0RpZ2l0ICUgMTAgPT0gMSkgewogICAgICAgICAgICByZXR1cm4gJHdvcmQxOwogICAgICAgIH0gZWxzZWlmICgkbGFzdFR3b0RpZ2l0ICUgMTAgPj0gMiAmJiAkbGFzdFR3b0RpZ2l0ICUgMTAgPD0gNCkgewogICAgICAgICAgICByZXR1cm4gJHdvcmQyOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJldHVybiAkd29yZDU7CiAgICAgICAgfQogICAgfQogICAgCiAgICBmdW5jdGlvbiBudW1iZXJUb1RleHQoJG51bWJlcikKICAgIHsKICAgICAgICAkcmVzdWx0ID0gJyc7CiAgICAgICAgCiAgICAgICAgJG51bWJlckNsYXNzID0gYXJyYXkoCiAgICAgICAgICAgIDAgPT4gYXJyYXkoJycsICcnLCAnJyksCiAgICAgICAgICAgIDEgPT4gYXJyYXkoJyDRgtGL0YHRj9GH0LAgJywgJyDRgtGL0YHRj9GH0LggJywgJyDRgtGL0YHRj9GHICcpLAogICAgICAgICAgICAyID0+IGFycmF5KCcg0LzQuNC70LvQuNC+0L0gJywgJyDQvNC40LvQu9C40L7QvdCwICcsICcg0LzQuNC70LvQuNC+0L3QvtCyICcpCiAgICAgICAgKTsKICAgICAgICAKICAgICAgICAkd29ya051bWJlciA9ICRudW1iZXI7CiAgICAgICAgJG51bWJlckFycmF5ID0gYXJyYXkoKTsKICAgICAgICAKICAgICAgICB3aGlsZSAoJHdvcmtOdW1iZXIgPiA5OTkpIHsKICAgICAgICAgICAgJG51bWJlckFycmF5W10gPSAkd29ya051bWJlciAlIDEwMDA7CiAgICAgICAgICAgICR3b3JrTnVtYmVyID0gKGludCkgZmxvb3IoJHdvcmtOdW1iZXIgLyAxMDAwKTsgCiAgICAgICAgfQogICAgICAgICRudW1iZXJBcnJheVtdID0gJHdvcmtOdW1iZXI7CiAgICAgICAgCiAgICAgICAgZm9yICgkaSA9ICBjb3VudCgkbnVtYmVyQXJyYXkpIC0gMTsgJGkgPj0gMDsgJGktLSkgewogICAgICAgICAgICBpZiAoJGkgPT0gMSkgewogICAgICAgICAgICAgICAgJGlzRmVtYWxlID0gMTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICRpc0ZlbWFsZSA9IDA7CiAgICAgICAgICAgIH0gICAgICAgICAgICAKICAgICAgICAgICAgaWYgKGNvdW50KCRudW1iZXJBcnJheSkgPiAxICYmICRudW1iZXJBcnJheVskaV0gPT0gMCkgewogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgJHJlc3VsdCAuPSBzcGVsbFNtYWxsTnVtYmVyKCRudW1iZXJBcnJheVskaV0sICRpc0ZlbWFsZSkgLiBpbmNsaW5lV29yZCgkbnVtYmVyQXJyYXlbJGldLCAkbnVtYmVyQ2xhc3NbJGldWzBdLCAkbnVtYmVyQ2xhc3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