CIT 590/591 Assignment 2: Negadecimal Calculator
Fall 2012, David Matuszek

Purposes of this assignment

Note: This is probably the most abstract (and least practical) assignment you will get this semester.

General idea of the assignment

Your assignment is to write a "calculator" for negadecimal external link 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 negadecimal.

Negadecimal is a place notation like decimal, only instead of powers of 10, the notation uses powers of -10: 1, -10, 100, -1000, 10000, -100000, etc. For example,

The number:     7   1   0   2   5
In decimal, means:     7*10000   1*1000   0*100   2*10   5*1
In negadecimal, means:     7*10000   1*-1000   0*100   2*-10   5*1

So, while 71025 is our usual decimal notation for 7102510 , the negadecimal number 71025-10 , translated to decimal, would be 68985.

An interesting characteristic of negadecimal is that numbers with anĀ even number of digits are negative, while numbers with an odd numberĀ of digits are nonnegative (zero or positive).

It is easy to translate from negadecimal to decimal:

5 times 1 is 5
2 times -10 is -20
0 times 100 is 0
1 times -1000 is -1000
7 times 10000 is 70000

5 - 20 + 0 - 1000 + 70000 = 68985

However, converting to negadecimal is more complicated. Fortunately, the Wikipedia article linked to above gives a function for converting to negaternary, and this function can be easily adapted to one that converts to negadecimal.

The user will use your program by typing in negadecimal expressions such as 500 + 692 and will get a negadecimal number such as 19192 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; all numbers except the response to dec mem are negadecimal numbers):

Compute: 3 * 5
195
Compute: dec mem
15
Compute: mem + 10
5
Compute: mem - 20
185
Compute: hello
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 negadecimal number (ndn for short) will mean a string composed of one or more of the characters '0'and '1'.

The user may type in any of the following expressions:

ndn + ndn Adds the two numbers. The result is stored in memory.
ndn - ndn Subtracts the second number from the first. The result is stored in memory.
ndn * ndn Multiplies the two numbers. The result is stored in memory.
ndn / ndn Divides the first number by the second. The result is stored in memory.
ndn % ndn Gives the remainder when dividing the first number by the second (see note). The result is stored in memory.
-ndn Computes the negative of the given number. The result is stored in memory.
dec ndn Gives the decimal value of the negadecimal number. This is the only expression that returns a decimal value. Memory is unchanged.
neg decimal Gives the negadecimal value of the decimal number. This is the only expression for which the number entered by the user is a decimal value. Moreover, the number entered may be negative.Memory is unchanged.
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 negadecimal.py.

Here are the functions you should have.

Functions that do computation

None of the following functions should request input from the user or print anything. Except as noted, input parameters and returned results are strings representing valid negadecimal numbers.

add(ndn_1, ndn_2)
Returns the sum of the two parameters. Also saves the result in memory.
subtract(ndn_1, ndn_2)
Returns the difference ndn_1 - ndn_2. Also saves the result in memory.
multiply(ndn_1, ndn_2)
Returns the product of the two parameters. Also saves the result in memory.
divide(ndn_1, ndn_2)
Returns the quotient ndn_1 / ndn_2. Does not check to make sure ndn_2 != 0. Also saves the result in memory.
remainder(ndn_1, ndn_2)
Returns the modulus ndn_1 % ndn_2. Does not check to make sure ndn_2 != 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(negadecimal)
Returns the number which, when added to negadecimal, would give zero. Also saves the result in memory.
to_decimal(ndn)
Returns the decimal integer value represented by the string parameter ndn.
to_negadecimal(dec)
Returns a string representing the negadecimal value of the integer parameter dec.
store(ndn)
Saves ndn in the global variable mem. This function may be called by all of the above functions, and should be the only function that changes this global variable.
fetch()
Returns the negadecimal string previously saved in the global variable mem (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. Calls one of the above methods to perform the arithmetic.
  4. Returns 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.

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

Grading

It would be a good idea to read How Assignments Are Graded, but here's a summary: Do exactly what the assignment says to do, use good programming style, test your program carefully, zip up your program (if it's more than a single file), and turn it in via Blackboard. Late programs will lose points.

Due date

Before 6am, Friday September 21. Submit your .py file to Blackboard.