CS363-U HW1

Making a Calculator

   January 2004
 S  M Tu  W Th  F  S
25 26 27 28 29 30 31
    ^ASSIGNED
   February 2004
 S  M Tu  W Th  F  S
 1  2  3  4  5  6  7
    ^DUE
As always, this assignment is done on your own. Don't even help each other (you can help each other on the labs as much as you want). You may seek help from the TA's or the instructor, but don't expect more than hints and a re-hashing of the lab and lecture material.

You are to design a web page which looks like the face of a fancy calculator. You are then to write a script which takes input from the web page and performs the calculations at each key stroke.

To get started, take a look at my calc.html and calc.cgi. Have a look at the "<input size=40 name=foo value=bar>" tag in HTML which permits you to put a default value into an input field. See if you can change the calc.cgi so that it writes the resulting value of the operation as the default value of x. Yes, this means your program will re-write the whole FORM, plus a default value.

Now, add some operations: +, -, *, /. You should use text instead of images, because getting each of these to be images is hard (see below).

That really should be enough for this first homework. But if you want to be cool, add some trig functions, sin, cos, tan, and some other stuff like 1/x, -x, x2, .... These are easy. You'll probably need to use a structure like this (gawk has no separate syntax for switches/cases):

if (command == "+") {
} else if (command == "-") {
} else ...
if you want to pull this off.

If you want to go even higher, add a memory function (STO and RCL, to store a value and to recall it), or even a multi-stage command, such as STO + 9, which adds x into the 9th memory register on an HP calculator (or at least it did in my day). This is as far as you should go -- I really don't need to see any programmable or graphing calculators. If you have good taste, you will design an RPN calculator. Otherwise, a calculator with "=" is ok. In an RPN calculator, you say "33" "ENTER" "25" "+" instead of "33" "+" "25" "=". And in really nice old RPN calculators, you show the entire stack, three or four values at a time (not just the X value), so that after the "33" "ENTER" "25", the stack is (0, 0, 33, 25), and after the "+" the stack is (0, 0, 0, 58).

If you think about it, you could have used a button for each operation, <input type=image src=foo.gif>, so that you do not need a submit button. This means that you can't let the user input x and y in input fields AND click on a button to perform an arithmetic operation. You will probably find it useful to use the HTML tag <input type=hidden name=foo value=bar>. So you might as well go whole hog and make each digit, 0, 1, 2, 3, ..., 9, its own button. You can still display x and y -- you just won't let them type the number -- they have to enter a number one digit at a time through cgi submission each time. If you can't figure out how to do this, don't worry. B. Well, seriously, you ought to be able to figure this out. It's an clever problem-solving engineering question at this point, not a matter of knowing more html or cgi. Btw, be careful -- x and y are reserved names for forms with images as submit buttons (x and y report the coordinates of the image where the click occurred!).