Home php教程 PHP源码 数据结构&算法(PHP描述) 三元组 Triplet

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

May 25, 2016 pm 05:05 PM

PHP 数据结构 算法 三元组 Triplet,学习php的朋友可以参考下。

<?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]; 
} 
// 设置第$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/>"; 
?>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)