Skip to content

Instantly share code, notes, and snippets.

@kinglozzer
Created September 27, 2016 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinglozzer/1494400f65a0a569784b5b20101022a5 to your computer and use it in GitHub Desktop.
Save kinglozzer/1494400f65a0a569784b5b20101022a5 to your computer and use it in GitHub Desktop.
<?php
// Work out result of sum, without using eval or regular expressions
// Code should parse/tokenize the input string, and return the result.
// Simple functions such as str_replace(), strpos(), substr() can be used
// NO external libraries may be used
/**
* Return answer, operator precedence ( / * - + ) must be retained.
* @param String $sum Input
* @return String The result of the sum
*/
function calc($sum) {
}
header('Content-type:text/plain');
// Should print 100
print calc("10*10")."\n";
// Should print 19
print calc("20+-1")."\n";
// Should print 3
print calc("3*3/3")."\n";
// Should print -1180
print calc("-100*23/2+-30")."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment