ClarkeRubber

UNSW ProgComp: Problem 2 - 2008

Jun 12th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1. <?php
  2.  
  3. $input = <<<END
  4. 4
  5. 2  2.0
  6. 36.2 50.00000
  7. 14.5  30
  8. -4   2
  9. END;
  10.  
  11. //Why so many spaces, bro?
  12.  
  13. $input = explode("\n", $input);
  14. array_shift($input);
  15.  
  16. foreach($input as $key => $value){
  17.     $value = preg_replace("#[ ]+#", ' ', $value); //bloody spaces
  18.     $input[$key] = explode(" ", $value);
  19. }
  20.  
  21. foreach($input as $key => $value){
  22.     /*
  23.     $value[0] = radius
  24.     $value[1] = chord length
  25.     */
  26.     $break = 0;
  27.     $out_r = str_pad(number_format($value[0], 4, '.', ''), 8, ' ', STR_PAD_LEFT);
  28.     $out_c = str_pad(number_format($value[1], 4, '.', ''), 8, ' ', STR_PAD_LEFT);
  29.     if($value[0] < 0){
  30.         //report "Radius is non-positive"
  31.         echo $out_r.' '.$out_c." Radius is non-positive\n";
  32.         $break = 1;
  33.     }
  34.     if(!($value[1] < 2*$value[0]) && $break == 0){
  35.         //report "Chord is too long"
  36.         echo $out_r.' '.$out_c." Chord is too long\n";
  37.         $break = 1;
  38.     }
  39.     if($break == 0){
  40.         $a = sqrt(pow($value[0], 2) - pow(($value[1]/2), 2));
  41.         $s = $value[0] - $a;
  42.         $angle = 2*rad2deg(asin($value[1]/(2*$value[0])));
  43.  
  44.        
  45.         $out_s = str_pad(number_format($s, 4, '.', ''), 9, ' ', STR_PAD_LEFT);
  46.         $out_a = str_pad(number_format($angle, 4, '.', ''), 9, ' ', STR_PAD_LEFT);
  47.  
  48.         echo $out_r.' '.$out_c.' s ='.$out_s.' angle ='.$out_a."\n";
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment