Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task Write a program to emulate calculations performed on the HP-35 calculator. Background The HP-35 was the first scientific hand-held calculator, released in 1972 and

Task

Write a program to emulate calculations performed on the HP-35 calculator.

Background

The HP-35 was the first scientific hand-held calculator, released in 1972 and an immediate success with engineers and scientists. Amongst other features, it used an unusual way of entering numbers, called RPN. RPN (reverse Polish notation, named for Polish mathematician Jan Lukasiewicz) allows complex calculations to be entered without the use of parentheses. The key difference is that operators are entered after the operands, rather than between them

The HP-35 implemented RPN using a stack of 4 registers, referred to as X, Y , Z, and T. The value in the X register is the one shown on the calculator's LED display; the other registers are used for temporary values during calculations. New values are entered into the X register, with existing values "pushed" up the register stack: the old value of X goes into Y, the old value of Y goes into Z, the old value of Z goes into T, and the old value of T is lost.

Single-operand functions (such as square root) use and replace the value in the X register; other registers are unchanged. Functions that operate on 2 values (such as the arithmetic operators) use the values in Y and X, with the result going back into X. Values in the Z and T registers are "dropped" down the stack, with Z overwriting the old Y and T overwriting the old Z. The value of T is preserved, providing a simple way to do calculations with repeated values.

This scheme allows calculations to be entered without using parentheses. For example, you could compute the compound expression (5 + 4)/(3 - 2) like this:

5 ENTER 4 + 3 ENTER 2 -

Hints

You'll need to define the class HPStack to represent the calculator's operand stack, with operations such as push (push all values up 1 level and store a value into X), pop (drop all values down 1 level and return the old X value), and peek (return the current X value). The simplest approach is to use an array to store the register values, with the methods manipulating the array elements.

The calculator should read and process tokens representing numbers and operator keys, with the current value of the X register displayed after processing each line. It should exit when all lines of input have been processed.

image text in transcribed

Hints for each level:

Use an array to store the register values

Limited stack depth: 4

LIFO

Need to implement three basic functions:

Push(): Add to top

Pop() : Remove from top

Peek() : Return top value

level1 basic:

Body of main() is provided.

+ is done.

Implement ,*,/

Test all provided examples

image text in transcribed

level 2 Scientific:

(pi) ,chs (change the sign of X), 1/x (recip), log (decimal logarithm), ln (natural logarithm), ex (exp), x (sqrt), sin, cos, tan (and their inverses arcsin, arccos, and arctan), and xy (pow).

All these functions already exist.

Include

The function names should be accepted in either upper or lower case : toupper().

level3 memory:

STO : store X into memory

RCL : push store value into X

CLR : reset all registers to 0

Clear() function in HPStack Class

CLx : remove X

Stack based : all modifications to the stack should be via pop() and push().

level 4 stack control:

Implement stackrelated functions

SWAP : swap values in X and Y

Swap() function in HPStack Class

ROLL : each value moves down the stack, X move to T

Roll() function in HPStack Class

ENTER : each value moves up the stack, X is preserved and T is lost.

A suitable main function will contain code such as this: HPStack stack string line while (getiine (cin, 11ne)) stringstream expression(line) ; string token: hile (expression >> token) if (isdigit (token [0] )) stack.push (atof (token.data )) t else if (token+" I/ other arithmetic ops similar le xstack.pop doubl double y = stack.pop(); stack.push (y + x) cout

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

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions