Advertisement
Guest User

Worst suvat calculator

a guest
Jan 19th, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.45 KB | None | 0 0
  1. $s = 0;
  2. $u = 0;
  3. $v = 0;
  4. $a = 0;
  5. $t = 0;
  6. $unknown = 0;
  7.  
  8. get_input();
  9. get_unknowns();
  10. calculate();
  11.  
  12. sub get_input
  13. {
  14. print("What distance are you travelling? Input the value or 'u' for unknown.\n");
  15. $s = <STDIN>;
  16. chomp($s);
  17.  
  18. print("What is the initial velocity? Input the value or 'u' for unknown.\n");
  19. $u = <STDIN>;
  20. chomp($u);
  21.  
  22. print("What is the final velocity? Input the value or 'u' for unknown.\n");
  23. $v = <STDIN>;
  24. chomp($v);
  25.  
  26. print("What is the acceleration? Input the value or 'u' for unknown.\n");
  27. $a = <STDIN>;
  28. chomp($a);
  29.  
  30. print("How long is this journey? Input the value or 'u' for unknown.\n");
  31. $t = <STDIN>;
  32. chomp($t);
  33. }
  34.  
  35. sub get_unknowns
  36. {
  37.     if($s eq "u"){
  38.         $unknown = "s";
  39.     } elsif($u eq "u"){
  40.         $unknown = "u";
  41.     } elsif($v eq "u"){
  42.         $unknown = "v";
  43.     } elsif($a eq "u"){
  44.         $unknown = "a";
  45.     } else {
  46.         $unknown = "t";
  47.     }
  48. }
  49.  
  50. sub calculate
  51. {
  52.     if($unknown eq "s"){
  53.         calculate_displacement($u, $v, $a, $t);
  54.     } elsif($unknown eq "u") {
  55.         calculate_initial_velocity($s, $v, $a, $t);
  56.     } elsif($unknown eq "v") {
  57.         calculate_final_velocity($s, $u, $a, $t);
  58.     } elsif($unknown eq "a") {
  59.         calculate_acceleration($s, $u, $v, $t);
  60.     } else {
  61.         calculate_time($s, $u, $v, $a);
  62.     }
  63. }
  64.  
  65. sub calculate_displacement
  66. {
  67.     my($initial_v, $final_v, $acceleration, $time) = @_;
  68.     my $displacement = $time * $initial_v + 1/2 * $acceleration * $time * $time;
  69.     print("The displacement of this journey is: $displacement m\n");
  70. }
  71.  
  72. sub calculate_initial_velocity
  73. {
  74.     my($displacement, $final_v, $acceleration, $time) = @_;
  75.     my $initial_v = sqrt($final_v * $final_v - 2 * $acceleration * $displacement);
  76.     print("The initial velocity of this journey is: $initial_v metres per second.\n");
  77. }
  78.  
  79. sub calculate_final_velocity
  80. {
  81.     my($displacement, $initial_v, $acceleration, $time) = @_;
  82.     my $final_v = sqrt($initial_v * $initial_v + 2 * $acceleration * $displacement);
  83.     print("The final velocity of this journey is: $final_v metres per second.\n");
  84. }
  85.  
  86. sub calculate_acceleration
  87. {
  88.     my($displacement, $initial_v, $final_v, $time) = @_;
  89.     my $acceleration = ($final_v * $final_v - $initial_v * $initial_v)/(2 * $displacement);
  90.     print("The acceleration of this journey is: $acceleration metres per second per second.\n");
  91. }
  92.  
  93. sub calculate_time
  94. {
  95.     my($displacement, $initial_v, $final_v, $acceleration) = @_;
  96.     my $time = ($final_v - $initial_v)/$acceleration;
  97.     print("The time taken for this journey to be completed is: $time seconds\n");
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement