Note: This is probably the most abstract (and least practical) assignment you will get this semester.
Your assignment is to write a "calculator" for negadecimal 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,
|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
Compute: dec mem
Compute: mem + 10
Compute: mem - 20
I don't understand: hello
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
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.
Computes the negative of the given number. The result is stored in memory.
Gives the decimal value of the negadecimal number. This is the only expression that returns a decimal value. Memory is unchanged.
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.
Quits the program.
Your "calculator" will remember the last number computed (initially zero, when you start the program). In each of the above expressions,
ndnmay be a negadecimal number, written without quotes, or the word
mem. If it is the word
mem, then the calculator uses the number stored in its "memory" as that operand.
00191); however, your calculator should not give results with leading zeros.
Save your program on a file named
Here are the functions you should have.
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.
ndn_1 - ndn_2. Also saves the result in memory.
ndn_1 / ndn_2. Does not check to make sure
ndn_2 != 0. Also saves the result in memory.
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.
negadecimal, would give zero. Also saves the result in memory.
ndnin 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.
mem(or the string
storehas never been called). This should be the only function that refers to this global variable.
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.
to_negadecimal(decimal)are fundamental, so do these first.
REPLfunction is very short (7 lines), because most of the work is done in
.pyfile to Blackboard.