ClarkeRubber

UNSW ProgComp: Problem 2 - 2005

Jun 9th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. <?php
  2.  
  3. $input = <<<END
  4. 67.5 150
  5. 64 160.0
  6. 74.00000000 200
  7. 100 205
  8. END;
  9.  
  10. //Weight in kilograms - Height in centimeters
  11.  
  12. $input = explode("\n", $input);
  13.  
  14. foreach($input as $key => $value){
  15.     $line = explode(" ", $value);
  16.     $weight = $line[0];
  17.     $height = $line[1];
  18.  
  19.     $height_m = intval($height)/100;
  20.  
  21.     if($height != 0){
  22.         $bmi = round($weight/pow($height_m, 2), 1);
  23.     }
  24.  
  25.     if($bmi > 30){
  26.         $weight_status = "Obese";
  27.     }elseif($bmi > 25){
  28.         $weight_status = "Overweight";
  29.     }elseif($bmi >= 18.5){
  30.         $weight_status = "Normal";
  31.     }else{
  32.         $weight_status = "Underweight";
  33.     }
  34.  
  35.     $out_weight = str_pad(number_format(round($weight, 1), 1)."kg", 8, " ", STR_PAD_RIGHT);
  36.     $out_height = str_pad(round($height)."cm", 7, " ", STR_PAD_RIGHT);
  37.     $out_bmi = str_pad("BMI=".number_format(round($bmi, 1), 1), 10, " ", STR_PAD_RIGHT);
  38.  
  39.     if(count($input)-1 != $key){
  40.         //add newline to end of print
  41.         print $out_weight.$out_height.$out_bmi.$weight_status."\n";
  42.     }else{
  43.         //print without appended new line
  44.         print $out_weight.$out_height.$out_bmi.$weight_status;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment