CIT 590 Assignment 2: Balanced Ternary
Spring 2013, 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
Compute: dec mem
Compute: bt -75
Compute: mem * N
Compute: hello
Error: I don't understand: hello
Compute: quit


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. These are the only kinds of expressions that your calculator needs to handle.

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 Just puts the number into memory, no other action taken.
-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

Here are the functions you should have.

Functions that do computation

Except as noted,

add(bt1, bt2)
Returns the sum of the two parameters as a BT number. Also saves the result in memory.
subtract(bt1, bt2)
Returns the difference bt1 - bt2 as a BT number. Also saves the result in memory.
multiply(bt1, bt2)
Returns the product of the two parameters as a BT number. Also saves the result in memory.
divide(bt1, bt2)
Returns the quotient bt1 / bt2 as a BT number. Does not check to make sure bt2 != 0. Also saves the result in memory.
remainder(bt1, bt2)
Returns the modulus bt1 % bt2 as a BT number. 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.
Returns the BT number which, when added to bt, would give zero. Also saves the result in memory.
Returns the integer value represented by the string parameter bt.
Returns a string representing the BT value of the integer parameter dec.
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). Do not perform perform the next step.
  3. Calls one of the above methods to perform the arithmetic, and return the result as a string.

You may find it useful to write "helper functions" for some of these steps, otherwise you may end up with a very long function.
Saves bt in a global variable. This function is called by most of the above functions, and should be the only function that changes this global variable.
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.

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.)

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

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

Programming hints

It's a good idea to end your program with

    if __name__ == '__main__':
(In this program, the "main" method is named REPL, not main. )

Due date:

Before 6am Friday January 25. Submit your .py file to Canvas. No other form of submission will be accepted.