Question

[PART ONE OF PROJECT, ALREADY COMPLETED] An accumulator is a primitive kind of calculator that can...

[PART ONE OF PROJECT, ALREADY COMPLETED]

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.

CODE FROM COMPLETED FIRST PART OF PROJECT BELOW :

#include <iostream>
using namespace std;
  
  
int main()
{
   float num=0;
   float total=0;
   char symbol;

   cout << "Welcome to your friendly neighborhood accumulator! Please input your expression, one token at a time, starting with an operand and type in '=' when completed." << '\n';
   cin >> num;
    total=num;
  
    while(1)
   {
       cin >> symbol;
       switch(symbol)
       {
           case '+':
           cin >> num;
           total+=num;
           break;
           case '-':
           cin >> num;
           total-=num;
           break;
       }
      
       if(symbol=='=')
       {
           cout << total;
           break;
       }
  
   }
       return 0;
}

[PART TWO, NEED HELP WITH!]

Using a do-while loop.

Recall that in a post-test loop, the loop body must execute once before the exit condition is checked. Therefore, to use a post-test loop, we have to change the way we implement the strategy.

This can be done as follows:

initialize total as zero;

initialize operator as '+';

read num;

process num (add or subtract from total, depending on operator);

read next operator;

If (operator is '=') exit and print total.

read num;

Translate this into C++ code.

Homework Answers

Answer #1

#include<iostream.h>
#include<conio.h>
void main()
{
float num=0;
float total=0;
char symbol;
clrscr();

cout << "Welcome to your friendly neighborhood accumulator! Please input your expression, one token at a time, starting with an operand and type in '=' when completed." << '\n';
cin >> num;
total=num;

while(1)
{
   cin >> symbol;
   switch(symbol)
   {
   case '+':
   cin >> num;
   total+=num;
   break;
   case '-':
   cin >> num;
   total-=num;
   break;
   }


   if(symbol=='=')
   {
   cout << total;
   cout<<"\nThanks for using Accumulator";
   break;
   }
   else
   continue; /* while loop be continue in case of not finding equal symbol*/

}
getch();
}

I have checked this code against inputs

your code was almost correct.Still have a problem with this code. Please leave comment.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT