Assignment 3

Due: 4:00 PM on Wednesday, March 15, 2000

Question #1

Write a Perl program bitplay.prl that accepts up to 10 integers from a user, stores these numbers in an array, extracts the first 8 bits from each stored number, interprets these 8-bit chunks as characters, and prints the resulting string. Input of numbers is terminated either by entering 10 numbers or by entering -1 (see sample output below). For example, given the input numbers 65, 66, 67, and 68, the resulting string is "ABCD".

Your program's output must have the following format:

Question #2

You have been asked to help an unnamed African country with the following computational problem: That country' intelligence agency has been warned that members of the Bou clan may be assassinated in the near future and wishes to monitor e-mail traffic file by file for messages that contain sentences of the form where a sentence is terminated by '.', '?', or '!', verb-root is one of the strings "Murder", "murder", "Kill", or "kill", title is "Citizen" or "citizen" and name denotes a pattern that describes all possible names within the clan Bou, i.e., For instance, some valid clan Bou names are "Bouta", "Boumala", and "Boumame", and some invalid names are "Bou", "Boume", and "Boulalalala". Some example sentences that fit the pattern are while sentences that do not fit the pattern are

Write a Perl program detect.prl which takes an input file as a command line argument, extracts all sentences in that file, examines each sentence for the pattern described above, and if that pattern is found, prints a warning to the appropriate member of the Bou clan as well as the full sentence in which the pattern occurs. When all sentences have been examined, your program should print a summary of the number of sentences in which patterns were found. Sentences may be split across lines in the given text file; however, all sentences must be pre-processed to eliminate internal newlines or extra spaces before they are printed. You may assume that the character '#' does not occur in any given file.

Your program's output must have the following format:

Question #3

Write a Perl program stack.prl that implements a stack of numbers as an array of length MAX_STACK_DEPTH, where MAX_STACK_DEPTH is a constant (for the purposes of this assignment, MAX_STACK_DEPTH will be set to 3). All stack variables will be global relative to the program. This program should have the following functions: The last two functions access an operation status variable associated with the stack. This variable can have three values: Attempts to push an element on a full stack cause the operation status to be set to 2; attempts to pop or read the top element of an empty stack cause the operation status to be set to 1; all other operations cause the operation status to be set to 0. For printing error messages, you may also find it useful to store a code indicating the most recently requested operation, e.g., Finally, write a main program that takes stack-manipulation commands from a user (see sample output below for command format), performs these commands using the functions described above, and displays the results. Your program will initialize the stack before processing the first of these commands. The list of commands is terminated by the command end.

Your program's output must have the following format:


For question #1, recall that numbers are stored in the C-language double format internally in Perl; however, the first 8 bits of a Perl number are the first eight bits of the mantissa, which is the value you want (important: this is true on our department's garfield computer but may not be so on your PCs). You may also find the inbuilt Perl chr function of use (see chapter 3 of the textbook).

Question #2 becomes a lot easier if you use the inbuilt Perl join and split functions to merge input lines and split them into sentences. You can use the substitution pattern-matching operator to trim the lines, i.e., remove pesky newlines and extra spaces, before examination/printing (this operator is also a neat way to extract the name of the citizen of the Bou clan in an occurrence of the pattern, using Perl's internal pattern-match buffers). Finally, recall that you recognize sentences by the occurrence of ',', '?', or "!' but these characters are part of the output if a pattern is found, so you cannot use split relative to these characters (this is where the note about character '#' not being in the file comes in handy).

Modulo differences in syntax, your code for question #3 of assignment #1 can be adapted very easily to solve question #3 in this assignment.

Finally, always remember that input operators in Perl leave the newlines on strings, which should probably be removed using the inbuilt chomp function (see Chapter 3 of the textbook).

Compiling and Running

See appropriate web pages for instructions on compiling and running Perl programs. For this assignment, you have to use the Perl-invocation method that has #!/usr/bin/perl as the first line in your Perl program.

Testing Your Programs

Test suites for test-asn have been provided for each question and can be accessed by using "3a", "3b", or "3c" as the appropriate code in the call to test-asn, e.g.,

Those wishing to see the complete test specification files should consult files a03a.t, a03b.t, and a03c.t in subdirectories test3a, test3b, and test3c, respectively, of directory /local/share/ASSIGNMENTS/cs3710. For question #2, the files which detect.prl is run against in the 9 specified tests are tf1 , tf2 , tf3 , tf4 , tf5 , tf6 , t7a , t7b , and tf8 .

Submitting Your Programs

Please submit your program files bitplay.prl, detect.prl, and stack.prl using system program submit-assignment and pass in hardcopies of these files to me by the assignment due date.

Additional Notes:

This page is adapted from that for CS3710 (Winter 1999) created by Mike Rendell.

Created: February 23, 2000
Last Modified: March 14, 2000