fork download
  1. <?php
  2. //Code challenge
  3. //company: Sparkcentral
  4. //applicant: Mike Roderick
  5.  
  6. class simpleCalculator{
  7. private $str_calc;
  8.  
  9. //constructor, accepts inoput string for calculation
  10. public function simpleCalculator($str_input){
  11. //remove whitespace and other undesireables, store result in private variable
  12. $this->str_calc = preg_replace('/[^0-9%+-\/\*]/s', '', $str_input);
  13. if (sizeof($this->str_calc) > 0){
  14. print "successfully initialized.\nValue stored:".$this->str_calc."\n";
  15. return true;
  16. } else {
  17. return false;
  18. }
  19. }
  20.  
  21. public function calculateResult(){
  22. //order of operations: multiplication, division and modulus, then addition and subtraction
  23. $this->handleMath('*');
  24. $this->handleMath('/');
  25. $this->handleMath('%');
  26. $this->handleMath('+');
  27. $this->handleMath('-');
  28. print "calculated value:".$this->str_calc."\n";
  29. }
  30.  
  31. private function handleMath($operator){
  32. //Iterate over the equation, according to the operator that was passed in
  33. while(strpos($this->str_calc,$operator) !== false){
  34. //for debugging
  35. //echo $this->str_calc."...";
  36.  
  37. //find number1 and number2 based on operand
  38. $operator_position = strpos($this->str_calc,$operator);
  39.  
  40. //get the first number+position in this operation
  41. $first_half = substr($this->str_calc,0,$operator_position);
  42. $first_parts = preg_split("/[%+-\/\*]+/",$first_half);
  43. $number1 = array_pop($first_parts);
  44. $numberpos1 = strrpos($first_half,$number1);
  45.  
  46. //get the second number+position in this operation
  47. $second_half = substr($this->str_calc,$operator_position+1);
  48. $second_parts = preg_split("/[%+-\/\*]+/",$second_half);
  49. $number2 = array_shift($second_parts);
  50. $numberpos2 = ($operator_position + 1) + strlen($number2);
  51.  
  52. //do the math
  53. $result = $this->mathOp($number1, $number2, $operator);
  54.  
  55. //shoehorn the result back into the string
  56. $this->str_calc = substr($this->str_calc,0,$numberpos1) . $result . substr($this->str_calc,$numberpos2);
  57.  
  58. //for debugging
  59. //print "becomes: ".$this->str_calc."\n";
  60. }
  61. }
  62.  
  63. private function mathOp($number1, $number2, $operator){
  64. switch($operator){
  65. case '*':
  66. return $number1 * $number2;
  67. break;
  68. case '/':
  69. return $number1 / $number2;
  70. break;
  71. case '+':
  72. return $number1 + $number2;
  73. break;
  74. case '-':
  75. return $number1 - $number2;
  76. break;
  77. case '%':
  78. return $number1 % $number2;
  79. break;
  80. }
  81. }
  82.  
  83. }
  84. echo 'starting...';
  85. $x = new simpleCalculator('a e q 5 + 9 - 4 * 2 / 4 % 5 * 2');
  86. $x->calculateResult();
  87. ?>
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
starting...successfully initialized.
Value stored:5+9-4*2/4%5*2
calculated value:12