PHP 數(shù)據(jù)結(jié)構(gòu) 算法 三元組 Triplet
更新時(shí)間:2011年07月02日 23:56:29 作者:
PHP 數(shù)據(jù)結(jié)構(gòu) 算法 三元組 Triplet,學(xué)習(xí)php的朋友可以參考下。
復(fù)制代碼 代碼如下:
<?php
/**
* 三元組 Triplet
*
*/
class Triplet
{
private $_data = null;
// 初始化三元組
public function init($val1,$val2,$val3)
{
$this->_data[0] = $val1;
$this->_data[1] = $val2;
$this->_data[2] = $val3;
return true;
}
// 銷毀三元組
public function destroy()
{
unset($this->_data);
return true;
}
// 返回第$key的值
public function get($key)
{
if($key < 1 || $key > 3) return false;
return $this->_data[$key - 1];
}
// 設(shè)置第$key元的值為$val
public function put($key,$val)
{
if($key < 1 || $key > 3) return false;
$this->_data[$key - 1] = $val;
return true;
}
// 是否按升序排序
public function isAscending()
{
return ($this->_data[0] <= $this->_data[1]) && ($this->_data[1] <= $this->_data[2]);
}
// 是否按降序排序
public function isDescending()
{
return ($this->_data[0] >= $this->_data[1]) && ($this->_data[1] >= $this->_data[2]);
}
// 獲取最大值
public function max()
{
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];
}
// 獲取最小值
public function min()
{
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];
}
}
//
$objTriplet = new Triplet();
echo "init:";var_dump($objTriplet->init(1,2,3)); echo "<br/>";
echo "get 1:";var_dump($objTriplet->get(1)); echo "<br/>";
echo "get 4:";var_dump($objTriplet->get(4)); echo "<br/>"; // false
echo "put 3,4:";var_dump($objTriplet->put(3,4)); echo "<br/>";
echo "max:";var_dump($objTriplet->max()); echo "<br/>";
echo "min:";var_dump($objTriplet->min()); echo "<br/>";
echo "isAscending:";var_dump($objTriplet->isAscending()); echo "<br/>";
echo "isDescending:";var_dump($objTriplet->isDescending()); echo "<br/>";
?>
您可能感興趣的文章:
- 基于遞歸實(shí)現(xiàn)的php樹形菜單代碼
- php顯示當(dāng)前文件所在的文件以及文件夾所有文件以樹形展開
- PHP無限分類(樹形類)
- PHP無限分類(樹形類)的深入分析
- PHP+Mysql樹型結(jié)構(gòu)(無限分類)數(shù)據(jù)庫設(shè)計(jì)的2種方式實(shí)例
- php讀取二進(jìn)制流(C語言結(jié)構(gòu)體struct數(shù)據(jù)文件)的深入解析
- php數(shù)據(jù)結(jié)構(gòu)與算法(PHP描述) 查找與二分法查找
- php數(shù)據(jù)結(jié)構(gòu)與算法(PHP描述) 快速排序 quick sort
- PHP中使用數(shù)組實(shí)現(xiàn)堆棧數(shù)據(jù)結(jié)構(gòu)的代碼
- php數(shù)據(jù)結(jié)構(gòu) 算法(PHP描述) 簡(jiǎn)單選擇排序 simple selection sort
- PHP 數(shù)據(jù)結(jié)構(gòu) 算法描述 冒泡排序 bubble sort
- php實(shí)現(xiàn)的樹形結(jié)構(gòu)數(shù)據(jù)存取類實(shí)例
相關(guān)文章
php中轉(zhuǎn)義mysql語句的實(shí)現(xiàn)代碼
如果你需要向數(shù)據(jù)庫,插入形如’你好’,這樣包含有單引號(hào)或者雙引號(hào)的字符串怎么辦,當(dāng)然可以使用反斜杠進(jìn)行轉(zhuǎn)義,但是如果內(nèi)容太多呢?2011-06-06PHP+ajax實(shí)現(xiàn)獲取新聞數(shù)據(jù)簡(jiǎn)單示例
這篇文章主要介紹了PHP+ajax實(shí)現(xiàn)獲取新聞數(shù)據(jù),涉及php ajax交互獲取信息及json格式處理的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05