Source for file Calculator.php

Documentation is available at Calculator.php

  1. <?php
  2.  
  3. # Copyright (c) 2002-2005 Cunningham & Cunningham, Inc.
  4. # Released under the terms of the GNU General Public License version 2 or later.
  5. #
  6. # PHP5 translation by Luis A. Floreani <luis.floreani@gmail.com>
  7.  
  8. require_once Fixture::INCLUDE_DIR . DIRECTORY_SEPARATOR . 'ColumnFixture.php';
  9.  
  10. class Calculator extends ColumnFixture {
  11. public $volts = 0.0;
  12. public $key = "";
  13. public static $hp;
  14. function __construct() {
  15. $this->hp = new HP35();
  16. }
  17.  
  18. public function execute() {
  19. if ($this->key != "")
  20. $this->hp->key($this->key);
  21. }
  22.  
  23. public function points() {
  24. return false;
  25. }
  26. public function flash() {
  27. return false;
  28. }
  29. public function watts() {
  30. return 0.5;
  31. }
  32. public function x() {
  33. return new ScientificDouble($this->hp->r[0]);
  34. }
  35. public function y() {
  36. return new ScientificDouble($this->hp->r[1]);
  37. }
  38. public function z() {
  39. return new ScientificDouble($this->hp->r[2]);
  40. }
  41. public function t() {
  42. return new ScientificDouble($this->hp->r[3]);
  43. }
  44.  
  45. public $typeDict = array(
  46. "volts" => "double",
  47. "key" => "string",
  48. "points" => "boolean",
  49. "flash" => "boolean",
  50. "watts" => "double",
  51. "x" => "ScientificDouble",
  52. "y" => "ScientificDouble",
  53. "z" => "ScientificDouble",
  54. "t" => "ScientificDouble"
  55. );
  56. }
  57.  
  58.  
  59. class HP35 {
  60. public $r;
  61. public $s = 0;
  62. function __construct() {
  63. $this->r = array (0,0,0,0);
  64. }
  65. public function key($key) {
  66. if ($this->numeric($key)) {$this->pushValue(floatval($key));}
  67. else if ($key == "+") {$this->pushValue($this->pop() + $this->pop());}
  68. else if ($key == "-") {$t=$this->pop(); $this->push($this->pop()-$t);}
  69. else if ($key == "*") {$this->pushValue($this->pop() * $this->pop());}
  70. else if ($key == "enter") {$this->push();}
  71. else if ($key == "clx") {$this->r[0] = 0;}
  72. else if ($key == "/") {
  73. $t = $this->pop();
  74. if ($t != 0)
  75. $this->pushValue($this->pop()/$t);
  76. }
  77. else if ($key == "x^y") {$this->pushValue(exp(log($this->pop())*$this->pop()));}
  78. else if ($key == "clr") {$this->r[0] = 0;$this->r[1] = 0;$this->r[2] = 0;$this->r[3] = 0;}
  79. else if ($key == "chs") {$this->r[0] = -$this->r[0];}
  80. else if ($key == "sin") {$this->pushValue(sin(deg2rad($this->pop())));}
  81. else if ($key == "chs") {$this->r[0] = -$this->r[0];}
  82. else
  83. throw new Exception("can't do key: " . $key);
  84. }
  85. public function numeric($key) {
  86. return is_numeric($key);
  87. }
  88. public function push() {
  89. for ($i=3; $i>0; $i--) {
  90. $this->r[$i] = $this->r[$i-1];
  91. }
  92. }
  93. public function pop() {
  94. $result = $this->r[0];
  95. for ($i=0; $i<3; $i++) {
  96. $this->r[$i] = $this->r[$i+1];
  97. }
  98. return $result;
  99. }
  100. public function pushValue($value) {
  101. $this->push();
  102. $this->r[0] = $value;
  103. }
  104. }
  105.  
  106. ?>

Documentation generated on Sun, 02 Apr 2006 16:01:03 +0200 by phpDocumentor 1.3.0RC5