Data structure & algorithm (PHP description) Triplet Triplet
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 08:50:56
Original
1030 people have browsed it
Data structure & algorithm (PHP description) Triplet Triplet
- /**
- * Triplet
- */
- class Triplet
- {
- private $_data = null;
- // Initialize triplet
- public function init($val1, $val2, $val3)
- {
- $this -> _data[0] = $val1;
- $this -> _data[1] = $val2;
- $this -> _data[2] = $val3;
- return true;
- }
- // Destroy the triple
- public function destroy()
- {
- unset($this -> _data);
- return true;
- }
- // Return the value of $key
- public function get($key)
- {
- if ($key < 1 || $key > 3) return false;
- return$this -> _data[$key-1];
- }
- // Set the value of the $key element to $val
- public function put($key, $val)
- {
- if ($key < 1 || $key > 3) return false;
- $this -> _data[$key-1] = $val;
- return true;
- }
- // Whether to sort in ascending order
- public function isAscending()
- {
- return ($this -> _data[0] <= $this -> _data[1]) && ($this - > _data[1] <= $this -> _data[2]);
- }
- // Whether to sort in descending order
- public function isDescending()
- {
- return ($this -> _data[0] > ;= $this -> _data[1]) && ($this -> _data[1] >= $this -> _data[2]);
- }
- // Get the maximum value
- 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];
- }
- // Get the minimum value
- 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"
";
-
- echo "get 1:";
- var_dump($objTriplet -> get(1));
- echo"
";
- echo"get 4:";
- var_dump($objTriplet -> get(4 ));
- echo"
"; // false
- echo"put 3,4:";
- var_dump($objTriplet -> put(3, 4));
- echo"
";
-
- echo"max:";
- var_dump($objTriplet -> max());
- echo"
";
- echo"min:";
- var_dump($objTriplet -> ; min());
- echo"
";
-
- echo"isAscending:";
- var_dump($objTriplet -> isAscending());
- echo"
";
- echo"isDescending:";
- var_dump($objTriplet -> isDescending());
- echo"
";
-
- ?>
Copy code
|
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31