CS3283 Lab Exercise Two on Tcl/Tk (5% CA)

 

(Deadline: Apr 6, 5pm, Submission Venue : Vincent's off. at S17.4.20)

 

Getting started

 

1. Download the TCL83B2.EXE – which is a local copy of Tcl/Tk for installation on PC – from the “Course Material” link under our course webpage. 

 

2. Install the Tcl interpreter and associated shell programs onto your PC.

     

 

Lab Exercise 2 (5% of C.A.)

 

Note that this is a bring-home lab. exercise in which you will be given

More than 2 weeks to do it. You only need to print out this exercise sheet, fill in the answers in the space provided (pls. attach any extra paper if space is insufficient), and submit it to my office. Remember to write down your full name and matric no. before submission. No late submission will be accepted !

 

Q1. (1%)

     

      Input the following Tcl script into the Tcl interpreter (it’s up to you to use interactive or non-interactive mode to load the following script into Tcl interpreter).

 

#!/usr/local/bin/tclsh

 

proc power {base p}  {

                        set result 1

                        while {$p > 0} {

                                    set result [expr $result * $base]

                                    set p [expr $p-1]

                        }

                        return $result

        }

 

 

After inputting the script, key in the following Tcl commands:

 

% power 16

 

-          what is the output ?

 

 

 

% power 2.4 6

 

-          what is the output ?

 

 

 

 

 

Q2. (2%)

Write a Tcl script which defines a procedure fac {n} to calculate and return the n! by using recursion (assume n is 0 or positive integer), and also print out the result of 10! by calling this procedure fac before the end of the script. After that, write another Tcl procedure fac1 {n} which is a non-recursive version of the procedure fac.

Ans:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

Q3. (2%)

 

Append the following code to the above “power” procedure in Q1, and save as “q3.tk”. Load the resulting script into the “wish” shell.

 

entry .base -width 8 -relief sunken -textvariable base

label .label1 -text "raised to the power"

entry .power -width 8 -relief sunken -textvariable power

label .label2 -text "is equal to"

label .result -textvariable result

pack .base .label1 .power .label2 .result -side left \

        -padx 1m -pady 2m

bind .base <Return> {set result [power $base $power]}

bind .power <Return> {set result [power $base $power]}

 

 

 

-          perform a screen capture of the resulting window, and insert the image file for the screen capture into the space provided below:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-          also, clearly explain the meaning of all those underlined part in the above code.

Ans: