Heim  >  Artikel  >  Backend-Entwicklung  >  数据结构&算法(PHP描述) 三元组 Triplet

数据结构&算法(PHP描述) 三元组 Triplet

WBOY
WBOYOriginal
2016-07-25 08:50:56942Durchsuche
数据结构&算法(PHP描述) 三元组 Triplet
  1. /**
  2. * 三元组 Triplet
  3. */
  4. class Triplet
  5. {
  6. private $_data = null;
  7. // 初始化三元组
  8. public function init($val1, $val2, $val3)
  9. {
  10. $this -> _data[0] = $val1;
  11. $this -> _data[1] = $val2;
  12. $this -> _data[2] = $val3;
  13. return true;
  14. }
  15. // 销毁三元组
  16. public function destroy()
  17. {
  18. unset($this -> _data);
  19. return true;
  20. }
  21. // 返回第$key的值
  22. public function get($key)
  23. {
  24. if ($key 3) return false;
  25. return$this -> _data[$key-1];
  26. }
  27. // 设置第$key元的值为$val
  28. public function put($key, $val)
  29. {
  30. if ($key 3) return false;
  31. $this -> _data[$key-1] = $val;
  32. return true;
  33. }
  34. // 是否按升序排序
  35. public function isAscending()
  36. {
  37. return ($this -> _data[0] _data[1]) && ($this -> _data[1] _data[2]);
  38. }
  39. // 是否按降序排序
  40. public function isDescending()
  41. {
  42. return ($this -> _data[0] >= $this -> _data[1]) && ($this -> _data[1] >= $this -> _data[2]);
  43. }
  44. // 获取最大值
  45. public function max()
  46. {
  47. return ($this -> _data[0] >= $this -> _data[1])? ($this -> _data[0] >= $this -> _data[2])?$this -> _data[0] :$this -> _data[2] : ($this -> _data[1] >= $this -> _data[2])?$this -> _data[1] :$this -> _data[2];
  48. }
  49. // 获取最小值
  50. public function min()
  51. {
  52. return ($this -> _data[0] _data[1])? ($this -> _data[0] _data[2])?$this -> _data[0] :$this -> _data[2] : ($this -> _data[1] _data[2])?$this -> _data[1] :$this -> _data[2];
  53. }
  54. }
  55. $objTriplet = new Triplet();
  56. echo"init:";
  57. var_dump($objTriplet -> init(1, 2, 3));
  58. echo"
    ";
  59. echo"get 1:";
  60. var_dump($objTriplet -> get(1));
  61. echo"
    ";
  62. echo"get 4:";
  63. var_dump($objTriplet -> get(4));
  64. echo"
    "; // false
  65. echo"put 3,4:";
  66. var_dump($objTriplet -> put(3, 4));
  67. echo"
    ";
  68. echo"max:";
  69. var_dump($objTriplet -> max());
  70. echo"
    ";
  71. echo"min:";
  72. var_dump($objTriplet -> min());
  73. echo"
    ";
  74. echo"isAscending:";
  75. var_dump($objTriplet -> isAscending());
  76. echo"
    ";
  77. echo"isDescending:";
  78. var_dump($objTriplet -> isDescending());
  79. echo"
    ";
  80. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn