Project 2 — Calculator solution


Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment


5/5 - (3 votes)

Develop a program that implements a four-function calculator that performs all arithmetic with
Your program will read a list of arithmetic expressions, evaluate them and display their results. All
numbers in the expression will be integers; however, the results of calculations will be fractions.
Your program must be able to process any valid arithmetic expression that includes the following:
• Nonnegative integer numbers
• The four basic arithmetic operations
• Parentheses
• Variable names, up to 40 variables; names follow C++ naming rules
• Assignment in the form r = epresson
For each expression, evaluate it, display the result and store the result in the appropriate variable,
if necessary.
.Required Objects
A calculator needs two Stack objects — one to store numbers and one to store operators. In this
program, the number stack — the numStack — will store Fraction objects and the operator stack
— the opStack — will store characters.
In order to store and retrieve variable values, a Dictionary object will be necessary. The keys are
strings and the values are Fractions. The exact implementation of the variable dictionary does not
.Calculator Algorithm
The program must read multiple lines from the standard input. Each line contains an arithmetic
expression and possibly an assignment to a variable. An algorithm for processing such a line follows
in Algorithms 1 and 2.
Data Structures and Objects Project 2 — Calculator Fall 2017 — CRN 42034
Algorithm 1 Main calculator algorithm
1: procedure Evaluate(string s)
2: Clear numStack
3: Clear opStack
4: Push $ onto opStack
5: ƒrst ← 0
6: dest ← Λ
7: Scan forward for = symbol
8: if = is found then
9: ƒrst ← position of character after =
10: dest ← first name found on line
11: end if
12: while ƒrst < s.ength do 13: ProcessSymbol(s, ƒrst) 14: end while 15: while top of opStack is not $ do 16: Perform top operation 17: end while 18: if dest 6= Λ then 19: Insert or update dictionary, key is dest, value is top of numStack 20: end if 21: output top of numStack 22: end procedure 2 of 4 Data Structures and Objects Project 2 — Calculator Fall 2017 — CRN 42034 Algorithm 2 Processing a symbol in the input string 1: procedure ProcessSymbol(string s,int ƒrst) 2: if s[ƒrst] is a digit then 3: Convert digit sequence to Fraction 4: Push Fraction object onto numStack 5: Advance ƒrst to first character past digit sequence 6: else if s[ƒrst] is a letter then 7: Extract name into string 8: Search for name in dictionary, push value onto numStack 9: Advance ƒrst to first character past name 10: else if s[ƒrst] is ( then 11: Push ( onto opStack 12: Increment ƒrst 13: else if s[ƒrst] is ) then 14: while top of opStack is not ( do 15: Perform top operation 16: end while 17: Pop ( from top of numStack 18: Increment ƒrst 19: else if s[ƒrst] is an operator then 20: while top of opStack has precedence over s[ƒrst] do 21: Perform top operation 22: end while 23: Push s[ƒrst] onto opStack 24: Increment ƒrst 25: else 26: Increment ƒrst 27: end if 28: end procedure 3 of 4 Data Structures and Objects Project 2 — Calculator Fall 2017 — CRN 42034 To process an operator, pop the opStack into a variable. Then, pop two values from the numStack into two Fraction objects. The first value popped is the right operand, the second value is the left operand. Perform the given operation and push the answer onto the numStack. If the expression is well-formed, then at line 18 of Algorithm 1, the opStack will only have $ and the numStack will have only one value which is the result of evaluating the expression. If the expression is not well-formed, an exception might be thrown or one of the stacks will have more than one value. In these cases, output an error message. What to turn in Turn in your source code and Makefile. If you use Code::Blocks, turn in a tarball of your project directory.