Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Calculator in PHP
    This application works perfectly well when running natively in php. But for some obscure reason coderbyte mashes up the string. I have no idea why. If someone could explain why this doesn't work on coderbyte, I'd be grateful. Actually, this is my second try. In my first script I used eval() and I guess you can't eval() an eval(), but this should be sandbox safe.
    <?php
    
    function calstring($string) {
    	if(preg_match('/(\d+)(?:\s*)([\+\-\*\/])(?:\s*)(\d+)/', $string, $matches) !== FALSE){
    	    $operator = $matches[2];
    	    if ($operator == '+') {
    	    	$rez = $matches[1] + $matches[3];
    	    } elseif ($operator == '-') {
    	    	$rez = $matches[1] - $matches[3];
    	    } elseif ($operator == '*') {
    	    	$rez = $matches[1] * $matches[3];
    	    } elseif ($operator == '/') {
    	    	$rez = $matches[1] / $matches[3];
    	    }
        }
        return $rez;
    }
    
    function isoperator($char) {
    	return preg_match("/[+\-\*\/]/", $char);
    }
    
    function stringalgebra($string) {
    	$curnum = 0;
    	$memnum = 0;
    	$oldnum = 0;
    	$memoperator = '';
    	$oldoperator = '';
    	for ($i = 0; $i < strlen($string); $i++) {
    		if (is_numeric($string[$i])) {
    			$curnum = 10*$curnum + $string[$i];
    		} else {
    			if (empty($memoperator)) {
    				$memnum = $curnum;
    			} elseif (preg_match("/[\+\-]/", $string[$i])) {
    				$memnum = calstring($memnum.$memoperator.$curnum);
    				if (!empty($oldoperator)) {
    					$memnum = calstring($oldnum.$oldoperator.$memnum);
    					$oldoperator = '';
    				}
    			} elseif (preg_match("/[\*\/]/", $string[$i])) {
    				if (preg_match("/[\*\/]/", $memoperator)) {
    					$memnum = calstring($memnum.$memoperator.$curnum);
    				} else {
    					$oldnum = $memnum;
    					$memnum = $curnum;
    					$oldoperator = $memoperator;
    				}
    			}
    			$memoperator = $string[$i];
    			$curnum = 0;
    		}
    	}
    	$rez = calstring($memnum.$memoperator.$curnum);
    	if (!empty($oldoperator)) {
    		$rez = calstring($oldnum.$oldoperator.$rez);
    	}
    	return $rez;
    }
    
    function resolveparenthese($string){
    	$len = strlen($string);
    	$parenthesestart = '';
    	for ($i = 0; $i < $len; $i++) {
    		if ($string[$i] == '(') {
    			$parenthesestart = $i;
    		}
    		elseif ($string[$i] == ')') {
    			$withinparenthese = substr($string, ($parenthesestart+1), ($i-$parenthesestart-1));
    			$rez = stringalgebra($withinparenthese);
    			if ($parenthesestart != 0 && !isoperator($string[$parenthesestart-1])) {
    				$rez = '*'.$rez; 
    			}
    			if ($i != $len-1 && !isoperator($string[$i+1])) {
    				$rez = $rez.'*'; 
    			} 
    			if ($parenthesestart != 0) {
    				$rez = substr($string, 0, ($parenthesestart)).$rez;
    			}
    			if ($i < $len-1) {
    				$rez = $rez.substr($string, ($i+1-$len));
    			}
    			return $rez;
    		}
    	}
    	return false;
    }
    
    
    function Calculator($str) {
    	while (strstr($str, '(') != false) {
    		$str = resolveparenthese($str);
    	}
    	$rez = stringalgebra($str);
    	return $rez;
    }
    ?>
    
    torbjornperttu posted this question on 12/1/15 | calculator, php
    Answers
  • +
  • 0
  • -
  • Hi torbjornperttu, Sorry for the issue, but I think you were testing this code when our phprepl was being updated last night. I just tried your code now and it worked fine for me! Let me know.
    Log in to write an answer.