Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project 1, A Multi-number System Calculator. Short Description: Create a simple calculator that can work with a number of different number systems. Goals When you

Project 1, A Multi-number System Calculator.

Short Description:

Create a simple calculator that can work with a number of different number systems.

Goals

When you finish this homework, you should have:

  • Refreshed your knowledge of programming.
  • Used command line flags and eliminated the warnings they produced.
  • Employed a makefile
  • Improved you knowledge of strings and characters in c++
  • Use a structure
  • Implement a program from a functional decomposition
  • Use header and implementation files.

Formal Description

You are friends with the famous archaeologist Dr. Edinburgh MacJones, or Eddie for short, who has been working on solving an ancient puzzle. This puzzle includes numbers carved in artifacts from several different ancient civilizations. Eddie is convinced he could solve the puzzle if he only had a method of computing across these different systems. Your job is to implement a simple calculator capable of assisting Eddie in his work.

Calculator Architecture

Eddie would like a calculator the follow properties:

  • All math is done in a register called the accumulator.
    • This holds a single value.
    • It is initialized to be 0 when the calculator is turned on.
  • There are four memory locations, labeled A, B, C, D.
    • Each of these hold a single value.
    • Each is initialized to be 0 when the calculator is turned on.
  • The calculator supports a simple command set
    • Mathematical operations +, -, *, /, % and ^
    • Commands: input, print, load, store, dump, output_base
  • The calculator can used different number systems (more below).
  • The calculator can output in different number systems.
    • The default output for the calculator is decimal (deci)
    • But this can be set to any number system.
    • The current system to use for output is the current base

Calculator Command Language

The language for the calculator is a simple prefix notation. In general, the operator is given first and any operands follow. This is very close to most assembly languages. In the following table, the operator is the command to be executed and the operand(s) are the values to be used by that operator. Note that commands have between 0 and 2 operands. In addition, please note that the number formats will be explained later.

Operator Operand Count Operand Type Example
dump 0 na dump
print 0 na print
input 1 numeric constant input .|,|:maya
load 1 memory location load A
store 1 memory location store C
output_base 1 number system abbreviation output_base murn
mathematical operation 2 numeric constant + XXX:unry 3:deci
memory location - A B
memory location, numeric constant * C //\:murn
numeric constant, memory location ^ XCI:posi D

Calculator Operation

In general the calculator should:

  1. Read in a command.
  2. Print the phrase "Processing Command: "
  3. Print the command back to the screen.
  4. Print a new line character.
  5. Execute the command.
  6. Print a blank line.

The calculator operates in batch mode, reading in all input from the file computations.cmds. You may assume that all commands are correct and in the correct format. Execution stops when the calculator reaches the end of the file. The file is in standard unix format with a newline at the end of each line, including the last line in the file. There is no line-feed character. Each element of the command is separated by a single space with no space after the last element.

Calculator Command Detail

  • +, *, -: perform addition, multiplication or subtraction as appropriate and store the results in the accumulator.
  • / and %: check the second operand
    • If the second operand is 0 the error message "Error, division by 0." is printed an no further action is taken.
    • If the second operand is not 0, the result of division (/) or modulus(%) of the first operand by the second is stored in the accumulator.
  • input: the operand is stored in the accumulator
  • print:
    1. A tab character is printed
    2. The number is printed in the current base
    3. A colon (:) is printed
    4. The abbreviation for the current base is printed.
    5. A new line character is printed.
  • load: the value of the named memory location is stored in the accumulator.
  • store: the value of the accumulator is stored in the named memory location.
  • output_base: change the current base.
  • dump:
    1. Print 35 stars (*).
    2. Print a new line character.
    3. Print the word "Accumulator: ".
    4. Print a space.
    5. Print the value of the accumulator.
    6. Print a new line character.
    7. For each memory location in order (A, B, C, D):
      1. Print the memory location name (A, B, ...).
      2. Print a colon.
      3. Print a space.
      4. Print the value stored at that memory location.
      5. Print a new line character.
    8. Print the phrase "Output Base: " .
    9. Print a space.
    10. Print the current base abbreviation.
    11. Print a new line character.
    12. Print 35 stars (*).
    13. Print a new line character.
    • An example:
      Processing Command: dump *********************************** Accumulator: 0 A: 0 B: 0 C: 0 D: 0 Output Base: deci *********************************** 
    • Note, all output in the final column is aligned.

Number Systems

Eddie has determined that you need to support the following number systems:

  1. Decimal
  2. Boolean
  3. Unary
  4. Positionless
  5. Modified Urnfield
  6. Mayan

This list might grow, so please keep your program as flexible as possible.

Decimal This is the standard system you know and use daily. Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Abbreviation: deci Representation:

abcd:deci where abcd = a*103 + b*102 + c*101 + d*100. There is no limit on the number of digits, but the number value will not exceed machine size. Numbers may be proceeded by a - which makes them negative. Representation is unique. 

Boolean This is a simple version of the binary number system. Digits: TF, where T = 1 and F = 0 Abbreviation: bool Representation:

abcd:bool where abcd = a*23 + b*22 + c*21 + d*20. There is no limit on the number of digits, but the number value will not exceed machine size. Numbers may be proceeded by a - which makes them negative. Representation is unique. 

Unary This system was probably the first invented. Digits: X Abbreviation: unry Representation:

abcd:unry where abcd = 1 + 1 + 1 + 1 There is no limit on the number of digits, but the number value will not exceed machine size. Numbers may be proceeded by a - which makes them negative. Representation is unique. Represent 0:deci as 0:unry 

Positionless This is a simple grouping by 10 system. Digits: I, X, C, K, M, G, T, P

Digit Value base 10
I 1
X 10
C 100
K 1,000
M 10,000
G 100,000
T 1,000,000
P 10,000,000

Abbreviation: posi Representation:

abcd:posi where abcd = value(a) + value(b) + value(c) + value(d) There should be no more than 9 of each letter in a computation. The largest number is PPPPPPPPPP:posi-I:posi Numbers may be proceeded by a - which makes them negative. Representation is not unique IIX = IXI = XII Represent 0:deci as 0:posi 

Modified Urnfield The Urnfield culture was an early European culture. Eddie has extended this system with several symbols. The original contained values from 1 to 24. This is essentially a base 5 system with special characters. Digits: /,\,(,),[,],<,>

Digit Value base 10
/ 1
\ 5
( 25
) 125
[ 625
] 3125
< 15,625
> 78,125

Abbreviation: murn Representation:

digits are placed in ascending order using as few digits as possible. \:murn is correct for 5 /////:murn is not the correct representation for 5. //\:murn is the correct representation for 7, /\/:murn and \//:murn are incorrect. The largest number is ////\\\\(((())))[[[[]]]]<<<<>>>>:murn Numbers may be proceeded by a - which makes them negative. Representation is unique. Represent 0:deci as 0:murn 

Mayan The Mayans had a base 20 system, but their digits were composed of multiple characters. Digits: combinations of 0, .,|

Digit Value base 10
0 0
. 1
- 5
Unlike the Urnified system, groups of digits repeat. The Myans stacked their symbols vertically. Since we can not do that, we will separate digits by commas. Myan digits are constructed in ascending order using the minimal number of symbols. 
Mayan Digit Value
0 0
. 1
.. 2
... 3
.... 4
| 5
.| 6
some digits missing
....| 0
|| 10
some digits missing
....||| 19

Abbreviation: maya Representation:

a,b,c,d:maya where a,b,c,d:maya= a*203 + b*202 + c*201 + 2*100. There is no limit on the number of digits, but the number value will not exceed machine size. Numbers may be proceeded by a - which makes them negative. Representation is unique. 

Examples:

Decimal Value Boolean Unary Positionless Modified Urnfield Mayan
0 0 0 0 0 0
1 T X I / .
2 TF XX II // ..
5 TFT XXXXX IIIII \ |
20 TFTFF (omitted) XX \\\\ .,0
42 TFTFTF (omitted) IIXXXX //\\\( ..,..
314 TFFTTTFTF (omitted) IIIIXCCC ////\\(()) |||,....||
78125 TFFTTFFFTFFTFTTFT (omitted) IIIIIXXCKKKKKKKKMMMMMMM > ....|,|||,.|,|

Notes

  • Your code must compile on cslab103
    • Without errors or warnings.
    • With the following compiler flags
      • -O3 -g -Wall -Wpedantic -Wextra -Wshadow -Wunused -Wunused-parameter -Wunused -Wuninitialized -Wshadow -Werror -Wmisleading-indentation -Wconversion -std=c++17
    • I would use this Makefile as my starting point.
  • Your code must follow the input and output specifications stated above.
    • This includes spacing, spelling and newlines.
    • I would suggest testing your program before submission
      • Use this computations.cmds file.
      • Use this output file.
      • You can test your program with the command
        calc | diff - output 
        • If no output is produced, then your program is working correctly.
        • If there is output
          • Line numbers are shown (your output line: the file output line)
          • Lines stating with a < show what your program produced
          • Lines starting with a > show what the expected output was.
          • 1c1 < Processing Command: Dump --- > Processing Command: dump 
            • This shows that on line 1, the program incorrectly spelled dump with an uppercase D.
  • You should perform all conversions between number systems algorithmically. Do NOT use large case statements.
  • 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 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.
  • We will discuss using implementation and header files. You must employ this in your final project.
  • We will discuss structures, you must employ at least one in your final project.

Required Files

A single tar or zip file containing the source code and makefile for this program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

11. What is a loss leader? (LO5)

Answered: 1 week ago