• Tidak ada hasil yang ditemukan

Calculating the Mass of Solid Objects

8.5 More Examples

8.5.2 Calculating the Mass of Solid Objects

</form>

</body>

</html>

Note that thevalueattribute of the<option>tag can be, but does not have to be, the same as the text for the option. For the“rectangular block”

shape,value is assigned as a single word (block), which will look like a single string literal value when it is used later in the PHP application.

For all but the simplest problems, it is never a good idea to try to write an entire application all at once. In this case, the HTML document helps to organize the problem by organizing all the required inputs. Once you are happy with Document 8.10a, then write a single-line PHP application that uses print_r() to display what is posted to the $_POST array:

<?php

print_r($_POST);

?>

This code will display something like this:

Array ([L] => 3 [W] => 2 [H] => 10 [R] => 3 [shapes] => cylinder [material] => silver)

and the results show that, really without any programming effort on your part, all the input data have been passed to the PHP application.

Once you are convinced that the inputs are successfully passed to PHP, then the calculations can be done. The first step is to create a data file containing materials and their densities:

(density.dat)

material density (kg/m^3) water 1000

aluminum 2739 gold 19320 silver 10429 oxygen 1.429 air 1.205

The header line is optional, but it is always a good idea to describe the contents of a datafile, including, in this case, the physical units in which the densities should be supplied.

The next step is less obvious. Although it is certainly possible to“hard code” volume calculations for each allowed shape, a more interesting solution is to create a second datafile that contains PHP code for calculating the volume of each shape:

(volume.dat) shape volume cube $L*$L*$L

sphere 4./3.*M_PI*$R*$R*$R cylinder M_PI*$R*$R*$L block $L*$W*$H

The code string for each allowed shape assumes specific variable names for the dimensions—L,$W, $H, and $R—which must correspond to the names defined in 8.10a.

Continue building the PHP application like this:

<?php

print_r($_POST);

$material=$_POST["material"];

$shape=$_POST["shapes"];

$L=$_POST["L"];

$W=$_POST["W"];

$H=$_POST["H"];

$R=$_POST["R"];

echo"<br />" .$material.", ".$shape."<br />";

?>

This code will display:

Array ([L] => 1 [W] => 1 [H] => 1 [R] => 3 [shapes] => cube [material] => oxygen) oxygen, cube

Now it is clear that the PHP application is properly receiving the inputs passed from Document 8.10a and has stored them in local variables. (You could also echo the values of$L,$W,$H, and$Rif you like.) In Document 8.10a, the fields were given the names L, W, H, and R, but this would not need to be the case. All that is important for the PHP application is to give the variables the same names used in thevolume.datfile.

Document 8.10b gives the entire PHP code for this problem. This code should be written in three sections: first, the definition of the variables as

8.5 More Examples 177

shown above, then the code to search for the material in its data file, and finally the code to do the mass calculation.

Document 8.10b (getMass.php)

<?php

print_r($_POST);

//exit;

$material=$_POST["material"];

$shape=$_POST["shapes"];

//exit;

$L=$_POST["L"];

$W=$_POST["W"];

$H=$_POST["H"];

$R=$_POST["R"];

echo"<br />".$material.", ".$shape."<br />";

$materialFile=fopen("density.dat","r");

$shapeFile=fopen("volume.dat","r");

// Read materialsle.

$found=false;

$line=fgets($materialFile);

while((!feof($materialFile)) && (!$found)) {

$values=fscanf($materialFile,"%s %f",$m,$d);

if(strcasecmp($material,$m) == 0) { echo"density = ".$d." kg/m^3<br />";

$found=true; }

}

// Read volumele.

$found=false;

$line=fgets($shapeFile);

while((!feof($shapeFile)) && (!$found)) {

$values=fscanf($shapeFile,"%s %s",$s,$v);

if(strcasecmp($shape,$s) == 0) { echo $shape.", ".$v."<br />";

$found=true; }

}

fclose($materialFile);

fclose($shapeFile);

$vv=$v."*$d";

echo $vv. "<br />";

echo "Mass = " .eval("return round($vv,3);") ." kg<br />";

?>

In the interests of demonstrating just the essential code needed to solve this problem, Document 8.10b does not include code to determine

whether the supplied material is included in the file of materials or whether there is a match with the shape supplied, but this would not be difficult to do.

The not-so-obvious and rather clever part of this application is included in the two shaded lines of code in Document 8.10b:

$vv=$v ."*$d";

echo"Mass = " .eval("return round($vv,3);") ."<br />";

The first of these lines appends "*$d" to the volume calculation string—mass equals volume times density. This string now looks like

“legal” PHP code, for example:

M_PI*$R*$R*$L*$d

(You couldecho the value of$vvif you want to see what it contains.) The next line of code“executes”this statement, using the eval()construct (it looks like a function, but is not). The return keyword is required to get back the numerical result, and theround() function is applied to the cal- culation to remove extraneous digits from the output.

The obvious advantage of this approach is that you can add new materials and shapes without altering the PHP code, assuming that, at most, four variables—length, width, height, and radius—will be sufficient to describe all dimensions needed for the volume calculations. For more complicated shapes, it might be necessary to add new variables or apply different interpretations to existing variables.