Scrset1 Exercises

Sequence, variables, input, arithmetic ops

 

1.

Write a perl console mode program called scrset1_1.pl that displays the following prompt:

 

Enter the distance of the planet from the sun:

 

After accepting the distance, the program should then proceed to calculate and display the distance traveled by the planet in one orbit around the sun.

Note: circumference = 2 * 3.1416* radius. Ensure you manually check a selection of answer displayed by your program

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Scrset1_1.pl – Orbit Distance Of Planet

 

Enter the distance of the planet from the sun : 1

 

In one orbit this planet travels 6.28 km

 

[HIT ENTER] ∆:∆

 

                             

I/O

 

 

Scrset1_1.pl

 

 

   Radius ←                  →    circumference

 

 

PSEUDO Code

 

Declare radius, circumference as number

 

Clear screen

 

Display “scrset1_1 Orbit Distance Of Planet” centered

Display 5 new lines

Display “Enter the distance of the planet from the sun : “ centered

Input radius

Set circumference = 2 * 3.1416 * radius

Display 4 new lines

Diplay “ In one orbit this planet travels” : centered

Display circumference

Display 6 new lines

Display “  33 spaces  & [HIT ENTER]∆:∆”

Pause program

 

Perl Code

 

#!c:\perl\bin\perl

# Developed by Munir Lodin on 25/02/08

# scrset1_1.pl

 

my ($radius, $circumference);

 

system("cls");

 

print("                            ");

print("Orbit Distance Of Planet");

print("\n\n\n\n\n");

print("               ");

print("Enter the distance of the planet from the sun : ");

 

$radius = <STDIN>;

chop($radius);

$circumference = 2 * 3.1416 * $radius;

 

print("\n\n\n\n");

print("               ");

print("In one orbit this planet travels $circumference");

print("\n\n\n\n\n");

print("                                 [HIT ENTER] : ");

<STDIN>;

 

%%%%%%%%%%%%%%%%%%%%%%%%

 

2.

Develop an html file (called scrset1_2.html) and a perl cgi program (called scrset1_2.pl) that work together via a web server to allow the user to enter values for distance and duration of a train journey into a web browser, click a calculate speed button, and then displays on a new page the average speed of the train journey along with the supplied distance and duration.

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Scrset1_2.html – Train Journey Cal

 

Enter distance of train journey :  20 km

 

Enter duration of train journey : 2 hr

 

Calculate Speed

 

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Scrset1_2.pl  – Train Journey Cal

 

The average speed of a train traveling 20 km in 2 hours is 10 km/hr

 

 

 

 

 

                             

 

I/O

 

 

Scrset1_2.pl 

 

 

   distance ←                  →    speed

   duration ←                 

 

PSEUDO Code

 

Declare distance, duration, speed as integer

 

Display the title

Distance param to “ txtDistance”

Duration param to “ txtDuration”

Set speed to distance Divided by duration

Do HTML

 

Perl Code

#!c:\perl\bin\perl

# Developed by Munir Lodin on 07/04/08

# scrset1_2.pl

 

use CGI ':standard';

my ($distance, $duration, $speed);

$distance = param('txtDistance');

$duration  = param('txtDuration');

$speed = $distance / $duration;

 

print << "DOC";

content-type: text/html\n\n

 

<html>

               <head>

                              <title>The Train Journey</title>

<style type="text/css">

body

               {

               background-color: silver;

               color: black;

               font-size: large;

               }

h1

               {

               text-align: center;

               }

</style>

               </head>

 

<body>

               <h1>Train Journey Calculator</h1>\n

<br><br><br><br>\n

&nbsp &nbsp &nbsp &nbsp &nbsp\n

 

<p>The Average speed of a train travelling $distance km

in $duration hours is $speed km/hr</p>\n

 

<a href="http://127.0.0.1/scrset1_2.html">

Do Another </a>

 

 

</body></html>\n

 

DOC

 

%%%%%%%%%%%%%%%%%%%%%%

 

3.

Write a console mode perl program called scrset1_3.pl that first displays the following prompt;

 

Enter the temperature in degrees Fahrenheit:

 

After accepting a value from the keyboard, your program should convert the temperature supplied into degrees Celsius using the equation Celsius = (5.0/9.0)*(Fahrenheit -32.0).

The Celsius temperature should then be displayed using an appropriate output message.

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_3.pl Temperature converter

 

Enter the temperature in degrees Fahrenhait : 212

 

That is  100 degrees celsius

 

[HIT ENTER] ∆:∆

 

                             

 

I/O

 

 

Scrset1_3.pl

 

 

 ftemp  ←                  → ctemp  

               

PSEUDO Code

 

Declare tempf tempc as integers

 

 

Clear screen

Display titile

Prompt user for Degrees Fahrenhait

Input farhenhait

Set $tempc = (5.0/9.0) * ($tempf - 32.0);

Diplay tempc

Pause program

 

Perl Code

#!c:\perl\bin\perl

# Developed by Munir Lodin on 25/02/08

# scrset1_3.pl

 

my ($tempf, $tempc);

 

system("cls");

 

print("                             ");

print("Temperature Converter");

print("\n\n\n");

print("                   ");

print("Enter temperature in degrees Fahrenheit : ");

$tempf = <STDIN>;

chop($tempf);

 

print("\n\n\n");

print("                   ");

$tempc = (5.0/9.0) * ($tempf - 32.0);

print("That is $tempc degrees celsius");

 

print("\n\n\n\n");

 

print("                    ********** END OF PROGRAM **********");

print("\n\n");

print ("                                HIT ENTER : ");

<STDIN>;

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

4.

Develop an html/perl cgi version of scrset1_3.pl called scrset1_4.pl. The only difference (apart from being browser based) should be that it is the Celsius temperature that is input and the Fahrenheit temperature that is displayed. Note that the user will trigger the conversion by clicking on a Convert button.

 

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Scrset1_4.html temp converter

 

Enter temperature in degrees : 100

 

              Convert

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Scrset1_4.pl  temp converter

 

100 degrees celsiuse is 212 degrees fahrenhait

 

 

              Try agian

 

I/O

 

 

Scrset1_4.pl

 

ctemp   ←                  →    ftemp

 

PSEUDO Code

Declare ctemp, ftemp as integers

 

Ftemp = ((9.0 * ctemp) / 5.0) + 32.0

 

 

Perl Code

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

5.

Write a console mode Perl program called scrset1_5.pl that prompts for and inputs 4 numbers, one by one. The program should then calculate and display the average of the 4 numbers entered.

 

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Scrset1_5.pl average of four number

 

Enter first number : 3

Enter second number : 4

Enter third number : 6

Enter fourth number : 7

 

The average is  5

 

 

I/O

 

 

Scrset1_5.pl

 

 

Num1   ←                  →    theaverage

Num2   ←                 

Num3   ←                          

Num4   ←                 

 

PSEUDO code

 

Declare num1, num2, num3, num4 as integer

Clear screen

Prompt for four numbers one by one

Input num1, num2, num3, num4

Display new lines

Display  “ the average is :

 

average = (num1 + num2 + num3 + num4) / 4;

 

Perl Code

 

#!c:\perl\bin\perl

# Developed by Munir Lodin on 07/04/08

# scrset1_5.pl

 

my ($num, $num2, $num3, $num4, $average);

system("cls");

 

print("                          ");

print("Average of four numbers");

print("\n\n\n");

print("                      ");

print("Enter first number : ");

$num = <STDIN>;

chop ($num);

print("\n\n");

print("                      ");

print("Enter second number : ");

$num2 = <STDIN>;

chop($num2);

print("\n\n");

print("                      ");

print("Enter third number : ");

$num3 = <STDIN>;

chop ($num3);

print("\n\n");

print("                      ");

print("Enter fourth number : ");

$num4 = <STDIN>;

chop ($num4);

$average = ($num + $num2 + $num3 + $num4) / 4;

print("\n\n\n");

print("                      ");

print("The calculated number is : $average");

print("\n\n\n");

 

 

print("                    ********** END OF PROGRAM **********");

print("\n\n");

print ("                                HIT ENTER : ");

<STDIN>;

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

6.

Write a console mode perl program called scrset1_6.pl that prompts for and inputs the number of minutes and, then proceeds to calculate the equivalent number of hours and minutes.

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_6.pl Time In Minutes

 

Enter time in minutes : 60

 

This is equal to 1 hour and 0 minutes

 

 

[HIT ENTER] ∆:∆

 

                             

 

I/O

 

 

Scrset1_6.pl

 

 

   minutes←                  →   hour

                                       → totalminutes

 

PSEUDO Code

Declare  minutes, hours, totalminutes as integers

Clear screen

Display the title centered

Display “enter time in minutes : “

Input totalminutes

Display new lines

Set hour as integer (totalminutes divided by 60

Seet minutes as integer (totalminutes % 60.

Diplay new lines

Diplay this is equal to hours and minutes

Pause program

 

Perl Code

 

#!c:\perl\bin\perl

# Developed by Munir Lodin on 07/04/08

# scrset1_6.pl

 

my ($minutes, $hours, $totalminutes);

system("cls");

 

print("                               ");

print("Calculating Time");

print("\n\n\n\n");

print("             ");

print("Enter time in minutes : ");

$totalminutes = <STDIN>;

chop ($totalminutes);

 

$hours = int($totalminutes / 60);

$minutes = int($totalminutes % 60);

 

print("\n\n\n\n");

print("             ");

print("This is equal to $hours hour and $minutes minutes");

print("\n\n\n\n");

 

print("                    ********** END OF PROGRAM **********");

print("\n\n");

print ("                                HIT ENTER : ");

<STDIN>;

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

7.

Write the html file/Perl cgi program required to prompt the user to enter a number of seconds via the web browser, and then after the user clicks on a convert button, display a new browser page showing the equivalent passage of time in hours, minutes and seconds.

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_7.html seconds conversion

 

Enter number of seconds : 3665

 

Convert

                             

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_7.pl seconds conversion

 

3665 seconds is 1 hour 1 minute and 5 seconds

 

Do another one

 

 

 

I/O

 

 

Scrset1_7.pl

 

 

  ins ←                  →    outh

                              →    outm

→    outs

 

PSEUDO Code

 

Perl Code

 

%%%%%%%%%%%%%%%%%%%%%

 

8.

Write a console mode perl program called scrset1_8.pl that prompts for and inputs 2 strings and then proceeds to display the 2 strings in the reverse order to which they were entered. A possible scenario could be:

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_8.pl – Reverse order

 

Enter a string :  first

 

Enter another string : last

 

Reversing the order entered gives :

 

Last

first

 

[HIT ENTER] ∆:∆

 

 

 

                             

 

I/O

 

 

Scrset1_8.pl

 

 

 Str1 ←                                  →   str1

 Str2   ←                              →    str2

 

PSEUDO Code

 

Declare str1, str2 as strings

 

Clear screen

 

Diplay title

Diplay prompt for two strings

Input strings

Diplay both strings in reverse order

Pause program

 

 

Perl Code

#!c:\perl\bin\perl

# Developed by Munir Lodin on 14/04/08

# scrset1_7.pl

 

my ($str1, $str2);

system("cls");

 

print("                            ");

print("Showing In Reverse Order");

print("\n\n\n\n");

print("             ");

print("Enter a string : ");

$str1 = <STDIN>;

chop ($str1);

 

print("\n\n\n");

print("             ");

print("Enter another string : ");

$str2 = <STDIN>;

chop ($str2);

 

system("cls");

 

print("                            ");

print("Showing In Reverse Order");

print("\n\n\n\n");

print("             ");

print("Reversing the order gives : ");

 

print("\n\n\n");

print("             ");

print("$str2");

print("\n\n");

 

print("             ");

print("$str1");

print("\n\n\n\n");

 

print("                    ********** END OF PROGRAM **********");

print("\n\n");

print ("                                HIT ENTER : ");

<STDIN>;

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

9.

Write a console mode perl program called scrset1_9.pl that prompts for/input 2 string from the keyboard and then concatenate them together into a string called $joined. The content of $joined should then be displayed.

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_9.pl Concatenating two strings

 

Enter  first string: I am

 

Enter second string: Munir

 

Joined together this is : I am Munir

 

 

[HIT ENTER] ∆:∆

 

                             

 

I/O

 

 

Scrset1_9.pl

 

 

 String1 ←                →    joined

String2    ←      

 

PSEUDO Code

 

 

Perl Code

#!c:\perl\bin\perl

# Developed by Munir Lodin on 14/04/08

# scrset1_9.pl

 

my ($string1, $string2, $joined);

 

system("cls");

 

print("                       ");

print("scrset1_9.pl - string concatenates");

 

print("\n\n\n");

print("             ");

print("Enter first string : ");

$string1 = <STDIN>;

chop ($string1);

 

print("\n\n\n");

print("             ");

print("Enter second string : ");

$string2 = <STDIN>;

chop ($string2);

 

$joined = $string1 . " " . $string2;

 

print("\n\n\n\n");

print("             ");

print("Joined together this is : ");

print("\n\n\n");

print("             ");

print("$joined");

 

print("\n\n\n\n");

 

print("                    ********** END OF PROGRAM **********");

print("\n\n");

print ("                                HIT ENTER : ");

<STDIN>;

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

10.

Write the necessary html file and perl cgi program (called scrset1_10.html, scrset1_10.pl respectively) to have the browser prompt for and input a command prompt command from the user. After clicking an Execute button, the command should be executed against the server and the results output into the browser in a new page in the same format as if the command had been entered at an actual command prompt.

 

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_10.html  Command prompt

 

Enter a command : set

 

Execute

                             

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_10.pl – Command prompt

 

Output of the set command is :

 

User = fw109h213……..

Path = …….

Compec======

 

Enter another command

 

 

 

I/O

 

 

Scrset1_10.pl

 

 

   ←                  →   

←                 

 

PSEUDO Code

 

Perl Code

 

:

use ∆ CGI

:

my∆($command);

:

:

$command ∆=∆param(‘txtcommand’);

:

Print(“content-type:∆text/html\n\n”);

:

Print(“<pre>\n”);

System($command);

Print(“</pre>\n”);

 

Commands

 

Incofiq

Set

Tasklist

Mem/c

 

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

11.

Write the necessary html file and Perl cgi program (called scrset1_11.p.html and scrset1_11.pl respectively) to set up web page generator. That is, prompt for a title, heading, background colour, text colour and body at the client, and proceed to use the Perl program to generate the page described.

 

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_11.html  Web Page Generator

 

Enter page title:

 

Enter main heading :

 

Select text colour:

 

Select background colour:

 

Enter body content:

 

Generate         Clear            back

 

 

 

                             

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_11.pl Web page Generator

 

 

 

I/O

 

 

Scrset1_11.pl

 

 

 title←                  → webpage

heading←                  

txtcolour

bgcolour←

content←

 

PSEUDO Code

 

 

Perl Code

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

12.

Write a console style perl program called scrset1_12.pl that prompts for a number and a power, and then proceeds to raise the supplied number to the supplied power and displays the answer in a user friendly format.

 

 

Screen Scenario

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 Scrset1_12.pl  power of number

 

Enter a number : 5

 

Enter a power : 1

 

 

[HIT ENTER] ∆:∆

 

                             

 

I/O

 

 

Scrset1_12.pl

 

 

number   ←                  →result   

power ←                 

 

PSEUDO Code

 

Declare number , power  as integer

Declare result

 

Clear screen

 

Display title centered

 

Prompt for “Enter a number : “

Input number

 

Prompt for “ Enter a power :”

Input power

 

Set  result  =  number ** power

 

Display the result

 

end program

 

 

Perl Code

#!c:\perl\bin\perl

# developed by Munir lodin on 28/04/08

# scrset1_12.pl

 

my ($number, $power, $result);

 

system("cls");

 

print(uc"                        scrset1_12.pl - power of number\n\n\n");

 

print("                    Enter a number : ");

$number = <STDIN>;

chop($number);

 

print("                    Enter a power : ");

$power = <STDIN>;

chop($power);

 

$result = $number ** $power;

 

print("                    $result");

print("\n\n\n\n");

 

 

print("                    ********** END OF PROGRAM **********");

print("\n\n");

print ("                                HIT ENTER : ");

<STDIN>;