資料結構&演算法(PHP描述) 三元組 Triplet

來源:互聯網
上載者:User

簡介:這是資料結構&演算法(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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.