CIT 591 Assignment 2: Balanced Ternary
Fall 2010, David Matuszek

Purposes of this assignment

General idea of the assignment

Your assignment is to write a "calculator" for balanced ternary integers. It will be able to do addition, subtraction, multiplication, and integer division, as well as negation and modulus (remainder). It will also have a one-number "memory,", and it will be able to convert between decimal and balanced ternary.

The user will use your program by typing in expressions such as N0 + 1NN and will get a balanced ternary number such as 1N as a result. Details of what the user may type are given below.

We will do some automated testing of your assignment. That means our program will call functions in your program, and if those functions are missing, or have a different name, or different parameters, or return the wrong kind of values, then they will fail our tests. So be sure to write functions exactly as specified below.

Example (I am using "Compute:" as a prompt):

Compute: N0000 + 1N0
N01N0
Compute: dec mem
-75
Compute: bt -75
N01N0
Compute: mem * N
10N10
Compute: hello
Error: I don't understand: hello
Compute: quit
Done.

Details

In the descriptions below, the word integer will refer to an ordinary integer, such as you have already been using in Python. The term balanced ternary number (BT for short) will mean a string composed of one or more of the characters '1', '0', and 'N' (but not 'n')

The user may type in any of the following expressions:

BT + BT Adds the two numbers
BT - BT Subtracts the second number from the first
BT * BT Multiplies the two numbers
BT / BT Divides the first number by the second
BT % BT Gives the remainder when dividing the first number by the second (see note)
-BT Computes the negative of the given number
dec BT Gives the decimal value of the BT number
bt decimal Gives the BT value of the decimal number
quit Quits the program

Your "calculator" will remember the last number computed (initially zero, when you start the program). In each of the above expressions,

Save your program on a file named bt.py.

Here are the functions you should have.

Functions that do computation

Except as noted,

add(bt1, bt2)
Returns the sum of the two parameters. Also saves the result in memory.
subtract(bt1, bt2)
Returns the difference bt1 - bt2. Also saves the result in memory.
multiply(bt1, bt2)
Returns the product of the two parameters. Also saves the result in memory.
divide(bt1, bt2)
Returns the quotient bt1 / bt2. Does not check to make sure bt2 != 0. Also saves the result in memory.
remainder(bt1, bt2)
Returns the modulus bt1 % bt2. Does not check to make sure bt2 != 0. If negative numbers are involved, the results are the same as they would be for integers in Python (in other words, just let Python do whatever it does in this case). Also saves the result in memory.
negate(bt)
Returns the number which, when added to bt, would give zero. Also saves the result in memory.
bt_to_int(bt)
Returns the integer value represented by the string parameter bt. Also saves the given bt (not the decimal result!) in memory .
int_to_bt(int)
Returns a string representing BT value of the integer parameter int. Also saves the result in memory.
store(bt)
Saves bt in a global variable. This function is called by all of the above functions, and should be the only function that changes this global variable.
fetch()
Returns the bt string previously saved in a global variable (or the string '0', if store has never been called). This should be the only function that refers to this global variable.
evaluate(string)
Takes a string that may have been entered by the user (or may have come from our test methods), and
  1. Finds the various parts of the string (for example, first operand, operator, second operand).
  2. Checks for problems (illegal characters, division or mod by zero, etc.). If any problem is found, return an error message. Do not change the contents of memory (mem).
  3. Call one of the above methods to perform the arithmetic.
  4. Save the balanced ternary result in memory (mem), and return the result as a string.

You may find it useful to write "helper functions" for each of steps 1, 2, and 3, otherwise you may end up with a very long function.

I want to make this as clear as I can: For every legal operation that the user types in, except quit, the balanced ternary version of the result is saved in memory. This includes the operations bt and dec. (Illegal operations should not change memory.) To evaluate bt and dec, you will probably call functions int_to_bt and bt_to_int, but the functions int_to_bt and bt_to_int should not themselves change memory, or it will be very difficult to make your program do the right thing. To evaluate dec, call bt_to_int to get an answer, then save the result; and similarly for the bt operation.

Catch as many errors in the user input as you can. You probably won't be able to catch them all, unless you use features of Python that we haven't talked about yet. We will be testing that your methods work when given correct input.

The only function that interacts with the user

REPL()
This function implements a basic Read-Evaluate-Print-Loop. It should:
This should be the only function that does any input/output.

Programming hints

Due date:

Before midnight, Thursday September 23. Submit your .py file to Blackboard.