Homework 1, Calculator
Short Description:
Write a program which will act as a simple calculator.
This assignment is worth 100 points.
Goals
When you finish this homework, you should:
- Have reviewed basic C++ and refreshed your programming skills.
- Refreshed C++ usage of strings and files.
- Have reviewed and strengthened your design skills.
- Have reviewed and strengthened your skills with procedures and functions.
Formal Description
Create a program which will act as a simple calculator.
This calculator has an accumulator,
four memory locations and should support the operations in the table below.
The result of all computations are stored in the accumulator.
The calculator only supports integer operation.
Operation | # Operands | Comment |
+ | 2 | Addition |
- | 2 | Subtraction |
- | 1 | Negation |
* | 2 | Multiplication |
/ | 2 | Integer Division |
% | 2 | Remainder |
^ | 2 | Power, the second operand must be non-negative |
load | 1 | Load the accumulator from a memory location |
store | 1 | Store the accumulator to a memory location |
print | 0 | Print the value in the accumulator |
system | 1 | Set the output system |
+= | 1 | Add operand to accumulator |
-= | 1 | Subtract operand from accumulator |
*= | 1 | Multiply accumulator by operand |
/= | 1 | Divide accumulator by operand |
%= | 1 | Remainder of accumulator divided by operand |
^= | 1 | Raise accumulator to operand, the operand must be non-negative. |
dump | 0 | Print the value of the accumulator and all registers. |
Your calculator should support number representation in the following number systems:
- Decimal (dcml)
- The decimal number system consists of the digits 0 through 9, with normal associated values.
- This is a position based number system where d1d2d3 = d1 × 102 + d2 × 101 + d3 × 100.
- The number 23410 = 234dcml.
- There is a unique representation for each number.
- Binary (biny)
- The binary system consists of the digits T and F, where Tbiny = 110 and Fbiny = 010.
- This is a position based number system where d1d2d3 = d1 × 22 + d2 × 21 + d3 × 20.
- The number 23410 = TTTFTFTFbiny.
- There is a unique representation for each number.
- Hexadecimal (hexa)
- The hexadecimal number system consists of the digits 0 through 9 and A through F.
- A represents 1010, B represents 1110, ... F represents 1510.
- This is a position based number system where d1d2d3 = d1 × 162 + d2 × 161 + d3 × 160.
- The number 23410 = EAhexa.
- There is a unique representation for each number.
- Trinary (trin)
- The trinary number system consists of the digits 0 through 2, with normal values.
- This is a position based number system where d1d2d3 = d1 × 32 + d2 × 31 + d3 × 30.
- The number 23410 = 22200trin.
- There is a unique representation for each number.
- Modified Urnfield (murn)
- The Urnfield number system consists of the marks / and \. In addition, we will add the marks (,), [,] and 0.
- In the modified system, 0murn = 010, / = 110, \ = 510, ( = 2510, )= 12510, [ = 62510 and ] =312510.
- This is a position based number system, where digits are listed in ascending order. {/,\,(,),[,]}, with as few digits as possible.
- The number 23410 = ////\(((()murn.
- There is a unique representation for each number.
- Modified Mayan (mmay)
- The Modified Mayan system uses the following symbols 0,., |, and ,.
- The symbols . and | are used to form the numbers 1 through 19.
- . = 110 and | = 510.
- Use the minimum number of symbols to represent a number.
- Thus ..mmay = 210, ....mmay = 410, ..|||mmay = 1710
- There is a unique representation for each digit 010 through 1910
- This is a position based number system where d1,d2,d3 = d1 × 202 + d2 × 201 + d3 × 200. (We will use , to separate digits)
- The number ... (310) and the number .,.,. (42110) are different numbers in this system.
The number 23410 = .||,.|||mmay.
- The number 23410 = .||,....||mmay.
- There is a unique representation for each number.
- Positionless (posi)
- This system uses the symbols I, X, M, C, K, G, T, P
- Iposi= 10010,
Xposi= 10110,
Mposi= 10210,
Cposi= 10310,
Kposi= 10410,
Gposi= 10510,
Tposi= 10610,
Pposi= 10710
- This is a positionless number system where the value of the symbols are added.
- The number 23410 = MMXXXIIIIposi = IIIIXXXMMposi = IXMIXMIXIposi = IXIMXMIXIposi
- There is not a unique representation for each number.
Operands
All operands follow the operator. Operands and operators are delimited by spaces.
Each operation begins on a new line operator first, followed by operands, followed by optional comments.
Operands will be either a number or a memory location. Memory locations are labeled A, B, C and D. Numbers will be given in the form
representation: name, where representation is a number in one of the representation system described above and name is the four letter name for the representation system. All of the following are valid
+ | 234:deci | ////\(((():murn |   |
+ | A | 22200:trin |   |
+ | TTTFTFTF:biny | B |   |
+ | C | D |   |
+ | 234:deci | ////\(((():murn | additional data is a comment and should be ignored |
The result of all mathematical operations are stored in the accumulator.
The system operator will have a single operand, the four letter name of a system which will be used for
all output. The default system should be dcml.
All input data should be validated. If an invalid input value, operator or operand is encountered, the system should print
the input line, followed by a diagnostic error message. The system should continue processing commands.
Example
The following is a valid set of input.
+ 1:dcml 1:dcml The accumulator should now contain a 2
store A Register A should now contain the value 2
+ A //:murn The accumulator should now contain a 4
store B Register B should now contain the value 4
system biny Change the output to binary
print Should print TFF:biny to the screen
system trin Change the output to trinary
print Should print 11:trin to the screen
+ B FXF:biny Should print an error message that X is an invalid binary digit
dump Should print the value of the accumulator (4) plus the 4 memory locations A:2, B:4, C:0 , D:0
Discussion
- All memory locations should be initialized to 0.
- This program should work in batch mode. It should open the file computations.cmds and process the commands contained
in this file. If the file is not present, an error message should be printed and the program exit.
- You should perform all conversions between number systems algorithmically. Do NOT use large case statements.
- When attempting to print a number in a given system, if a number can not be expressed in that system, for example numbers larger than 78,124 can not be expressed in murn, print a message stating that the number is too large.
- Represent negative numbers with using a minus sign (-), so -//\:murn is -710
- Your code SHOULD NOT crash or hang ever.
- atoi, isdigit and isblank are a few functions from the isalpha family of functions. You might want to investigate these, and other associated functions.
- The string functions, substr, find might be in this program and should be reviewed.
- File streams should be passed as variable parameters.
- Whenever possible, eliminate repeated code by building subroutines. If you find you are repeating code, pull it out and put it into a subroutine.
- You may not use string streams for this project.
- You should probably do a preliminary design.
- You should probably write some experimental code, and then revise your design.
- You should create a sub-directory and perform all work for this project within this directory.
- If you need help, you should ask.
- Please do not put this work off until the weekend before it is due.
Required Files
You should submit the following in a tar file:
- All source code required to build all programs.
- A makefile which will build all programs as well as clean the directory
- A README describing
- Author identification information
- The project
- Problems encountered
- Any portions of the code which are not working.
- Any unique features of the code.
- Any other information you wish to share.
You should not submit
- Object files or executable.
- Core files.
- Source code files which are not related to the project.
Submission
Please create a tar file containing the files described above. Email this file to danbennett360@gmail.com by class time of the due date.