Jump to content

Recommended Posts

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 far 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 = "first" name = "input[]" size=3> <br/> 
			Number 2: <input type = "second" name = "input[]" size=3> <br> 
			Number 3: <input type = "third" name = "input[]" size=3> <br> 
			Number 4: <input type = "fourth" 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'];

// assigning $_GET value
print_r($myInputs);
echo $myInputs[0]+ $myInputs[1]; 

	$myArray = array();
	$myArray['a'] = 5;
	$myArray['b'] = 10 ; 
	$myArray['c'] = 15 ;
	$myArray['d'] = 20 ;

// using a,b,c,d only... using $_GET here
echo $myArray['a']+$myArray['b'];

// get directly from$_GET values.
echo $_GET['input'][0]+ $_GET['input'][1]; 

?>


Link to comment
https://forums.phpfreaks.com/topic/283381-form-not-outputting-number/
Share on other sites

hi, the values like a=5 b=10 c=15 d=10

is numbers i provide. all the use does it input a b c or d. which those numbers are the values I indicated.

does that make sense?

so the values are supposed to be in a file, and you're supposed to get the operators from the user?

hi, the values like a=5 b=10 c=15 d=10

is numbers i provide. all the use does it input a b c or d. which those numbers are the values I indicated.

does that make sense?

No, this does not make sense. First you say YOU provide the values. Then you say the USER inputs them. So which is it?

 

Based on your OP instructions:

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.

So this looks like some instructions you got from somewhere else (like a schoolbook). And I think you are misunderstanding what you are supposed to be doing.

 

The instructions say to let the user input an expression. So your form should only have 1 text field, not 4. Then you are supposed to evaluate that expression, swapping out the variables they specified (e.g. "a" or "b") with values from a text file.

 

So, you are

 

1) supposed to have a text file with like "a=1,b=7,c=3,d=14" in it.

2) have a form to let the user enter in an expression, e.g. "a+b" or "c-d+a"

3) write code that swaps those letters out to be the values from the text file (step 1). So for example "c-d+a" would be converted to "3-14+1".

4) write code to evaluate the expression and output the result. e.g. "c-d+a" would, based on the #1 output "-10"

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.