把两个英文单词加在一起~
写一个类Arith
,实现下面的功能
var k = new Arith("three");
k.add("seven"); //返回 "ten"
输入 - zero 到 ten ,肯定是小写
输出 - 返回相加的结果,肯定是小写
class Arith {
private $number;
public $numbers = [
'zero' => 0, 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5,
'six' => 6, 'seven' => 7, 'eight' => 8, 'nine' => 9, 'ten' => 10, 'eleven' => 11,
'twelve' => 12, 'thirteen' => 13, 'fourteen' => 14, 'fifteen' => 15, 'sixteen' => 16,
'seventeen' => 17, 'eighteen' => 18, 'nineteen' => 19, 'twenty' => 20
];
public function __construct(string $number) {
$this->number = $number;
}
public function add(string $number): string {
$sum = $this->get($this->number) + $this->get($number);
$flipped = array_flip($this->numbers);
return $flipped[$sum];
}
public function get(string $number): int {
// can convert to lower
return (empty($this->numbers[$number])) ? 0 : $this->numbers[$number];
}
}
class Arith{
private $i;
const A = [
'zero' => 0,
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6,
'seven' => 7,
'eight' => 8,
'nine' => 9,
'ten' => 10,
'eleven' => 11,
'twelve' => 12,
'thirteen' => 13,
'fourteen' => 14,
'fifteen' => 15,
'sixteen' => 16,
'seventeen' => 17,
'eighteen' => 18,
'nineteen' => 19,
'twenty' => 20
];
public function __construct(string $i)
{
$this->i = self::A[$i];
}
public function add(string $v) {
return array_search($this->i + self::A[$v], self::A);
}
}
class Arith{
private $_numbers = ["zero" => 0, "one" => 1, "two" => 2, "three" => 3,
"four" => 4, "five" => 5, "six" => 6, "seven" => 7, "eight" => 8,
"nine" => 9, "ten" => 10, "eleven" => 11];
private $_words = ["zero", "one", "two", "three",
"four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen", "twenty"];
private $_word = '';
public function __construct($word) {
$this->_word = $word;
}
public function add($word) {
return $this->_words[$this->_numbers[$this->_word] + $this->_numbers[$word]];
}
}
class Arith
{
protected $num;
//初始化数据
public function __construct($num)
{
$this->num = $this->_tonNum($num);
}
private function _tonNum($num)
{
$num_num = 0;
switch ($num) {
case "zero":
$num_num = 0;
break;
case "one":
$num_num = 1;
break;
case "two":
$num_num = 2;
break;
case "three":
$num_num = 3;
break;
case "four":
$num_num = 4;
break;
case "five":
$num_num = 5;
break;
case "six":
$num_num = 6;
break;
case "seven":
$num_num = 7;
break;
case "eight":
$num_num = 8;
break;
case "nine":
$num_num = 9;
break;
case "ten":
$num_num = 10;
break;
case "eleven":
$num_num = 11;
break;
case "twelve":
$num_num = 12;
break;
case "thirteen":
$num_num = 13;
break;
case "fourteen":
$num_num = 14;
break;
case "fifteen":
$num_num = 15;
break;
case "sixteen":
$num_num = 16;
break;
case "seventeen":
$num_num = 17;
break;
case "eighteen":
$num_num = 18;
break;
case "nineteen":
$num_num = 19;
break;
case "twenty":
$num_num = 20;
break;
}
return $num_num;
}
/**
* 转换成文字
* @param $num
*/
public function tostr($num)
{
$num_num = "";
switch ($num) {
case 0:
$num_num = "zero";
break;
case 1:
$num_num = "one";
break;
case 2:
$num_num = "two";
break;
case 3:
$num_num = "three";
break;
case 4:
$num_num = "four";
break;
case 5:
$num_num = "five";
break;
case 6:
$num_num = "six";
break;
case 7:
$num_num = "seven";
break;
case 8:
$num_num = "eight";
break;
case 9:
$num_num = "nine";
break;
case 10:
$num_num = "ten";
break;
case 11:
$num_num = "eleven";
break;
case 12:
$num_num = "twelve";
break;
case 13:
$num_num = "thirteen";
break;
case 14:
$num_num = "fourteen";
break;
case 15:
$num_num = "fifteen";
break;
case 16:
$num_num = "sixteen";
break;
case 17:
$num_num = "seventeen";
break;
case 18:
$num_num = "eighteen";
break;
case 19:
$num_num = "nineteen";
break;
case 20:
$num_num = "twenty";
break;
}
return $num_num;
}
//增加的方法
public function add($add_num)
{
//做加法
$add_num = $this->_tonNum($add_num);
$re = $this->tostr($add_num + $this->num);
//转换
return $re;
}
}
解决问题的整体思路是差不多的,但是在具体实现细节上会有区别,而具体实现的点也就是考察的价值。