soupi Posted October 28, 2013 Share Posted October 28, 2013 Given an input of an expression consisting of a string of letters and operators (plus sign, minus sign, and letters. IE: ‘b-d+e-f’) and a file with a set of variable/value pairs separated by commas (i.e: a=1,b=7,c=3,d=14) write a program that would output the result of the inputted expression.For example, if the expression input was ("a + b+c -d") and the file input was ( a=1,b=7,c=3,d=14) the output would be -3.Thus fur I have got this, it calculates the numbers but not sure how to calculate it by the letters abcd, <html> <head> </head> <body> <form action = "" method = "GET"> Number 1: <input type = "text" name = "input[]" size=3> <br/> Number 2: <input type = "text" name = "input[]" size=3> <br> Number 3: <input type = "text" name = "input[]" size=3> <br> Number 4: <input type = "text" name = "input[]" size=3> <br> <input type="hidden" name="calc" value ="yes"> <input type = "submit" name = "Calculate"/> </form> </body> </html> <?php print_r($_GET); $myInputs = $_GET['input']; print_r($myInputs); $myArray = array(); $myArray['a'] = 5; $myArray['b'] = 10 ; $myArray['c'] = 15 ; $myArray['d'] = 20 ; echo $myArray[$_GET['a']] + $myArray[$_GET['b']] ; /* if ($_GET['calc'] == "yes") { $a = $_GET['a']; $b = $_GET['b']; $c = $_GET['c']; $d = $_GET['d']; $total = $a+$b+$c+$d; print "Total is: $total <p>"; } */ ?> Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 28, 2013 Share Posted October 28, 2013 (edited) I'm guessing this is homework! Where are the operators? But this line doesn't make sense: echo $myArray[$_GET['a']] + $myArray[$_GET['b']];* maybe it does make sense now Maybe you need to go through your list of given operators, test to see what it is then do the operation, adding it to the running total. Or a less safe way may be to use eval() ? Edited October 28, 2013 by mentalist Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 28, 2013 Share Posted October 28, 2013 You could add index values to the input names: Number 1: <input type = "text" name = "input[a]" size=3> <br> Number 2: <input type = "text" name = "input[b]" size=3> <br> Number 3: <input type = "text" name = "input[c]" size=3> <br> Number 4: <input type = "text" name = "input[d]" size=3> <br> Quote Link to comment Share on other sites More sharing options...
Barand Posted October 28, 2013 Share Posted October 28, 2013 Or you you could try looking at the solution Ignace gave you when you posted this same question about 3 days ago http://forums.phpfreaks.com/topic/283274-simple-math-problem/?do=findComment&comment=1455423 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.