Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

An accumulator is a primitive kind of calculator that can evaluate arithmetic expressions. In fact, the Arithmetic-Logic Unit (ALU) of the rst computers was just

An accumulator is a primitive kind of calculator that can evaluate arithmetic expressions. In fact, the Arithmetic-Logic Unit (ALU) of the rst computers was just an accumulator. An arithmetic expression, as you know, consists of two kinds of tokens: operands and operators

All our operands will be (float) numbers and for a start, we shall use only two operators:

+ (plus)

and

- (minus)

A sample run of the program would look like this.

Welcome to your friendly neighborhood accumulator! Please input

your expression, one token at a time, starting with an operand and

type in '=' when completed.

3.0

+

2.0

-

-5.0

=

10.0

Thank-you for using your friendly neighborhood accumulator!

The problem is formally described as follows:

Input: An alternating sequence of operands and operators, given one at a time, starting and ending with an operand. The sequence is followed by an equals sign, to indicate termination. Each operand is a float value and each operator is a plus or a minus.

Output: The value of the expression dened by the sequence of operands and operators. We shall assume for now that the user will not make mistakes when using the accumulator, i.e., there is no bad input

(e.g., two consecutive tokens being both operands or both operators, other unexpected characters etc.) Note that the equals sign acts like a sentinel.

The Strategy.

To initialize the process, the first operand (a number) is read and stored as the total. Next, an operator (a character) is read and we decide what action to take. The action can be one of the following:

(i) In case of termination (equals sign) the process is exited and the total is printed;

(ii) otherwise another operand is read and either added to or subtracted from the total depending on the operator.

Implement your algorithm in C++.

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

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions