Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $s = 0;
- $u = 0;
- $v = 0;
- $a = 0;
- $t = 0;
- $unknown = 0;
- get_input();
- get_unknowns();
- calculate();
- sub get_input
- {
- print("What distance are you travelling? Input the value or 'u' for unknown.\n");
- $s = <STDIN>;
- chomp($s);
- print("What is the initial velocity? Input the value or 'u' for unknown.\n");
- $u = <STDIN>;
- chomp($u);
- print("What is the final velocity? Input the value or 'u' for unknown.\n");
- $v = <STDIN>;
- chomp($v);
- print("What is the acceleration? Input the value or 'u' for unknown.\n");
- $a = <STDIN>;
- chomp($a);
- print("How long is this journey? Input the value or 'u' for unknown.\n");
- $t = <STDIN>;
- chomp($t);
- }
- sub get_unknowns
- {
- if($s eq "u"){
- $unknown = "s";
- } elsif($u eq "u"){
- $unknown = "u";
- } elsif($v eq "u"){
- $unknown = "v";
- } elsif($a eq "u"){
- $unknown = "a";
- } else {
- $unknown = "t";
- }
- }
- sub calculate
- {
- if($unknown eq "s"){
- calculate_displacement($u, $v, $a, $t);
- } elsif($unknown eq "u") {
- calculate_initial_velocity($s, $v, $a, $t);
- } elsif($unknown eq "v") {
- calculate_final_velocity($s, $u, $a, $t);
- } elsif($unknown eq "a") {
- calculate_acceleration($s, $u, $v, $t);
- } else {
- calculate_time($s, $u, $v, $a);
- }
- }
- sub calculate_displacement
- {
- my($initial_v, $final_v, $acceleration, $time) = @_;
- my $displacement = $time * $initial_v + 1/2 * $acceleration * $time * $time;
- print("The displacement of this journey is: $displacement m\n");
- }
- sub calculate_initial_velocity
- {
- my($displacement, $final_v, $acceleration, $time) = @_;
- my $initial_v = sqrt($final_v * $final_v - 2 * $acceleration * $displacement);
- print("The initial velocity of this journey is: $initial_v metres per second.\n");
- }
- sub calculate_final_velocity
- {
- my($displacement, $initial_v, $acceleration, $time) = @_;
- my $final_v = sqrt($initial_v * $initial_v + 2 * $acceleration * $displacement);
- print("The final velocity of this journey is: $final_v metres per second.\n");
- }
- sub calculate_acceleration
- {
- my($displacement, $initial_v, $final_v, $time) = @_;
- my $acceleration = ($final_v * $final_v - $initial_v * $initial_v)/(2 * $displacement);
- print("The acceleration of this journey is: $acceleration metres per second per second.\n");
- }
- sub calculate_time
- {
- my($displacement, $initial_v, $final_v, $acceleration) = @_;
- my $time = ($final_v - $initial_v)/$acceleration;
- print("The time taken for this journey to be completed is: $time seconds\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement