您的位置:首页 > 理论基础 > 数据结构算法

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

2011-11-11 08:57 477 查看
简介:这是数据结构&算法(PHP描述) 三元组 Triplet的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=339825' scrolling='no'>
1 <?php
2 /**
3  *  三元组 Triplet
4  *
5  */
6 class Triplet
7 {
8     private $_data = null;
9
10     // 初始化三元组
11     public function init($val1,$val2,$val3)
12     {
13         $this->_data[0] = $val1;
14         $this->_data[1] = $val2;
15         $this->_data[2] = $val3;
16         return true;
17     }
18
19     // 销毁三元组
20     public function destroy()
21     {
22         unset($this->_data);
23         return true;
24     }
25
26     // 返回第$key的值
27     public function get($key)
28     {
29         if($key < 1 || $key > 3) return false;
30         return $this->_data[$key - 1];
31     }
32
33     // 设置第$key元的值为$val
34     public function put($key,$val)
35     {
36         if($key < 1 || $key > 3) return false;
37         $this->_data[$key - 1] = $val;
38         return true;
39     }
40
41     // 是否按升序排序
42     public function isAscending()
43     {
44         return ($this->_data[0] <= $this->_data[1]) && ($this->_data[1] <= $this->_data[2]);
45     }
46
47     // 是否按降序排序
48     public function isDescending()
49     {
50         return ($this->_data[0] >= $this->_data[1]) && ($this->_data[1] >= $this->_data[2]);
51     }
52
53     // 获取最大值
54     public function max()
55     {
56         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];
57     }
58
59     // 获取最小值
60     public function min()
61     {
62         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];
63     }
64 }
65
66 //
67 $objTriplet = new Triplet();
68 echo "init:";var_dump($objTriplet->init(1,2,3)); echo "<br/>";
69
70 echo "get 1:";var_dump($objTriplet->get(1)); echo "<br/>";
71 echo "get 4:";var_dump($objTriplet->get(4)); echo "<br/>"; // false
72 echo "put 3,4:";var_dump($objTriplet->put(3,4)); echo "<br/>";
73
74 echo "max:";var_dump($objTriplet->max()); echo "<br/>";
75 echo "min:";var_dump($objTriplet->min()); echo "<br/>";
76
77 echo "isAscending:";var_dump($objTriplet->isAscending()); echo "<br/>";
78 echo "isDescending:";var_dump($objTriplet->isDescending()); echo "<br/>";
79 ?>


爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具
http://biancheng.dnbcw.info/php/339825.html pageNo:7
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: