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:
- Demonstrated your ability to use arrays.
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 | none | Print all stored values. |
BASE | an integer between 2 and 36 | Set the current base to the value given |
LOAD | A memory location | Set the accumulator to the value stored in the given memory location. |
STORE | A memory location | Save the value of the accumulator to the given memory location. |
PRINT | nothing | Print the value stored in the accumulator using the current base. |
ADD | Two quantities | Set the accumulator to the sum of the values given by the quantities |
SUBTRACT | Two quantities | Set the accumulator to the difference of the values given by the quantities |
MULTIPLY | Two quantities | Set the accumulator to the product of the values given by the quantities |
DIVIDE | Two quantities | Set the accumulator to the quotient of the values given by the quantities. |
- The accumulator is a register which holds a single number. It is the location where all computations are stored. Commands refer to the accumulator as $0.
- A memory location is a position in memory. There are 26 memory locations labeled $A through $Z. A memory location (currently) holds a single number.
- A magnitude is a string of alphanumeric characters. This represents a number in some base. Note a magnitude may start with + or - to indicate the sign of the number.
- A base represents the base a number is given in. A base starts with the @ symbol and is followed by a number between 2 and 36 inclusively.
- A number is a value represented by a magnitude and a base. Numbers are in the form DDDD@BB where DDDD represents the magnitude and @BB represents the base.
- A quantity is a number, a memory location or the accumulator.
- A value is the data stored at a given location.
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 | Output | Notes |
OFF | Exiting | |
RESET | Reset | |
SET | $0=value | value in the current base. |
DUMP | see below | |
BASE | New base B | B is the new base. The base does not include the @ sign |
LOAD | $0=value | value in the current base. |
STORE | $M=value | Where M is a memory label and value is in the current base. |
PRINT | value | value is in the current base. |
ADD | value | value represents the sum in the current base. |
SUBTRACT | value | value represents the difference in the current base. |
MULTIPLY | value | value 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
- You may not use any of the following.
- stringstreams.
- string.find(). You must implement this function.
- string.substr(). You must implement this function.
- atoi, atol, atof, ... or any automatic conversion from string to integer. You must implement this algorithm.
- Any utility that converts between bases, you must implement this algorithm.
- Any algorithm from the standard algorithm library.
- Any item from the SCL except strings.
- Using any prohibited function results in a 0 with no chance for redo.
- You MAY use string comparison.
- All magnitudes should be stored as a long.
- This code must compile without warnings or errors with the following compiler flags
- -g -O3 -Wall -Wextra -Wuninitialized -pedantic -Wshadow -Weffc++ -Wunused
- Make sure identifying information, including your name is in a comment at the top of your source code.
- You are responsible for ALL code submitted. You should know what this code does and be able to explain how the code works. I may, at my option, require an oral code review for any program submitted.
- Make sure your code is well structured and decomposed into small readable functions.
Required Files
The all source code required to perform the task described above.
Submission