Homework 4, Multi-base Calculator

Short Description:

Write a program which acts as a calculator.

This assignment is worth 30 points.

Goals

When you finish this homework, you should: The main goal of this assignment is to introduce you to arrays.

Formal Description

For this assignment you are to build a calculator which can perform computations an any base between 2 and 36. This calculator has a single accumulator register and a 26 position memory. The commands for this calculator are as follows
Command Arguments Action
OFF none Exit the program.
RESET none Reset all memory locations to 0, reset the accumulator to 0, reset the current base to 2
SET number Set the value of the accumulator to be the given number.
DUMP nonePrint all stored values.
BASE an integer between 2 and 36Set the current base to the value given
LOAD A memory locationSet the accumulator to the value stored in the given memory location.
STORE A memory locationSave the value of the accumulator to the given memory location.
PRINT nothingPrint the value stored in the accumulator using the current base.
ADD Two quantitiesSet the accumulator to the sum of the values given by the quantities
SUBTRACT Two quantitiesSet the accumulator to the difference of the values given by the quantities
MULTIPLY Two quantitiesSet the accumulator to the product of the values given by the quantities
DIVIDE Two quantitiesSet the accumulator to the quotient of the values given by the quantities.

Initially all memory locations and the accumulator are set to 0 and the base is set to 2.

Your application should print a prompt: "% " (do not include the quotes, they are to emphasize the space) to the standard output then read and process the command the user gives. You may assume that there will be no errors in the command and that the user will issue an OFF statement to finish execution. The program should print the result of the computation. The following table specifies what should be printed for each command.

Command OutputNotes
OFF Exiting
RESET Reset
SET $0=value value in the current base.
DUMP see below
BASE New base BB is the new base. The base does not include the @ sign
LOAD $0=valuevalue in the current base.
STORE $M=valueWhere M is a memory label and value is in the current base.
PRINT valuevalue is in the current base.
ADD valuevalue represents the sum in the current base.
SUBTRACT valuevalue represents the difference in the current base.
MULTIPLY valuevalue represents the product in the current base.
DIVIDE value value represents the quotient in the current base.

For the DUMP command print all of the memory, one per line, starting with the accumulator then locations A-Z. Use the format $X=value where X is the correct label for the memory location and value is the value in the current base. There should be no spaces in the output. The dump should be proceeded with the message: Memory Dump on a single line. There should be a blank line printed after the end of a memory dump.

An example run

% BASE 16
New base 16
% SET 2@10
$0=2@16
% STORE $A
$A=2@16
% MULTIPLY 2@31 $A
$0=4@16
% STORE $B
$B=4@16
% MULTIPLY $B 2@22
$0=8@16
% STORE $C
$C=8@16
% ADD $C $B
$0=C@16
% SUBTRACT 10@20 10@30
$0=-A@16
% OFF

Discussion

Required Files

The all source code required to perform the task described above.

Submission