2017-10-09 122 views
2

我创建了此功能将数字转换为单词。以及我如何使用这个我的虚词转换成数字:如何使用我的函数将单词转换为数字?

简单的函数代码:

$array = array("1"=>"ЯК","2"=>"ДУ","3"=>"СЕ","4"=>"ЧОР","5"=>"ПАНҶ","6"=>"ШАШ","7"=>"ҲАФТ","8"=>"ХАШТ","9"=>"НӮҲ","0"=>"НОЛ","10"=>"ДАҲ","20"=>"БИСТ","30"=>"СИ","40"=>"ЧИЛ","50"=>"ПАНҶОҲ","60"=>"ШАСТ","70"=>"ҲАФТОД","80"=>"ХАШТОД","90"=>"НАВАД","100"=>"САД"); 
    $n = "98"; // Input number to converting 

if($n < 10 && $n > -1){ 
    echo $array[$n]; 
} 
if($n == 10 OR $n == 20 OR $n == 30 OR $n == 40 OR $n == 50 OR $n == 60 OR $n == 70 OR $n == 80 OR $n == 90 OR $n == 100){ 
    echo $array[$n]; 
} 

if(mb_strlen($n) == 2 && $n[1] != 0) 
{ 
    $d = $n[0]."0"; 
    echo "$array[$d]У ".$array[$n[1]]; 
} 

我的功能至今数转换到一百。我现在如何使用我的函数的答案将文本转换为数字?

+1

你能提供输出的例子吗? – Rulisp

回答

2

你没有指定输入规格,但我假设你想要它与单词之间的空格。

//get our input=>"522" 
    $input = "ПАНҶ САД БИСТ ДУ"; 
    //split it up 
    $split = explode(" ", $input); 
    //start out output 
    $c = 0; 
    //set history 
    $history = ""; 
    //loop the words 
    foreach($split as &$s){ 
     $res = search($s); 

     //If number is 9 or less, we are going to check if it's with a number 
     //bigger than or equal to 100, if it is. We multiply them together 
     //else, we just add them. 
     if((($res = search($s)) <=9)){ 
      //get the next number in the array 
      $next = next($split); 
      //if the number is >100. set $nextres 
      if(($nextres = search($next)) >= 100){ 
       //I.E. $c = 5 * 100 = 500 
       $c = $nextres * $res; 
       //set the history so we skip over it next run 
       $history = $next; 
      }else{ 
       //Single digit on its own 
       $c += $res; 
      } 
     }elseif($s != $history){ 
      $c += $res; 
     } 
    } 
    //output the result 
    echo $c; 



    function search($s){ 
     global $array; 

     if(!$res = array_search($s, $array)){ 
      //grab the string length 
      $max = strlen($s); 
      //remove one character at a time until we find a match 
      for($i=0;$i<$max; $i++){ 
       if($res = array_search(mb_substr($s, 0, -$i),$array)){ 
        //stop the loop 
        $i = $max; 
        } 
      }  
     } 

     return $res; 
    }  

输出为522

+0

最有可能会有这样的答案:БИСТУДУ== 22,ПАНҶОҲУШАШ== 56,НАВАДУНӮҲ== 99.所以我标记了similar_text()的标签“相似性”。或者其他一些选项。我们对单词仍然有不同的后缀。应该注意的是,只有postfix可以添加到数字的名称。 – John

+0

对不起,我没有注意到,我没有用你的语言教育不幸。我现在要记下你的笔记,并试着想出一个解决方案。 – IsThisJavascript

+0

有必要摆脱不同的后缀。我也有自己的选择来解决这个问题。例如,您需要获取传入的单词并在循环中检查数组中的所有值(例如,如果您可以将文本形式的数字生成为100,则循环将高达99),并且如果没有匹配,你需要用循环中数组的值来模拟这个字符串。最后你需要打印找到的匹配关键字。 – John

2

所以,@ WillParky93假设,你的输入有单词之间的空格。

<?php 
mb_internal_encoding("UTF-8");//For testing purposes 
$array = array("1"=>"ЯК","2"=>"ДУ","3"=>"СЕ","4"=>"ЧОР","5"=>"ПАНҶ","6"=>"ШАШ","7"=>"ҲАФТ","8"=>"ХАШТ","9"=>"НӮҲ","0"=>"НОЛ","10"=>"ДАҲ","20"=>"БИСТ","30"=>"СИ","40"=>"ЧИЛ","50"=>"ПАНҶОҲ","60"=>"ШАСТ","70"=>"ҲАФТОД","80"=>"ХАШТОД","90"=>"НАВАД","100"=>"САД"); 
$postfixes = array("3" => "ВУ"); 
$n = "98"; // Input number to converting 
$res = ""; 
//I also optimized your conversion of numbers to words 
if($n > 0 && ($n < 10 || $n%10 == 0)) 
{ 
    $res = $array[$n]; 
} 


if($n > 10 && $n < 100 && $n%10 != 0) 
{ 
    $d = intval(($n/10)); 
    $sd = $n%10; 

    $ending = isset($postfixes[$d]) ? $postfixes[$d] : "У"; 
    $res = ($array[$d * 10]).$ending." ".$array[$sd]; 
} 

echo $res; 

echo "\n<br/>"; 
$splitted = explode(" ", $res); 

//According to your example, you use only numerals that less than 100 
//So, to simplify your task(btw, according to Google, the language is tajik 
//and I don't know the rules of building numerals in this language) 
if(sizeof($splitted) == 1) { 
    echo array_search($splitted[0], $array); 
} 
else if(sizeof($splitted) == 2) { 
    $first = $splitted[0]; 
    $first_length = mb_strlen($first); 
    if(mb_substr($first, $first_length - 2) == "ВУ") 
    { 
     $first = mb_substr($first, 0, $first_length - 2); 
    } 
    else 
    { 
     $first = mb_substr($splitted[0], 0, $first_length - 1); 
    } 

    $second = $splitted[1]; 
    echo (array_search($first, $array) + array_search($second, $array)); 
} 
+1

您的代码也可以使用。但是当我添加数字“9”时,将数字转换为文本时的答案是错误的。这是你的错误。请检查并比较你的错误。答案一定是唯一的“НӮҲ”但你的代码返回“НӮҲНОЛУНӮҲ” – John

+0

@约翰感谢注意到,错误固定 – Rulisp

+0

这将是您对新兴的后缀决定?我在评论中注明了这一点,请阅读。我提到了为名称$ postfix = array(“у”,“ву”)创建一个异常数组的问题。 – John