Computer Sciences Department logo

CS 368 (Summer 2009) — Day 13 Homework

Due Tuesday, August 4th, at the start of class.

Description

In this assignment, you'll write a Perl script that functions as a simple calculator. This script should employ the Perl exception handling capabilities to handle illegal operations (such as division by zero, etc).

Details

Your script should prompt the user, and then read a math operation from the standard input stream. At a minimum, the program should handle input of the form: <number><operator><number>, where <operator > is "*", "/", "+", or "-".

Your script should then process this operation, compute the result, and print it. Here's a sample of my solution running:

perl ./homework-13.pl
Enter operation (q to quit): 7/8
7/8 = 0.875
Enter operation (q to quit): 3*4
3*4 = 12
Enter operation (q to quit): 0/0
0/0: Illegal division by zero
  

Optional. If you wish, you can make your script handle more generic forms of input:

Enter operation (q to quit): 3+5*5
3+5*5 = 28
Enter operation (q to quit): (3+5)*5
(3+5)*5 = 40
Enter operation (q to quit): q
  

Hints