Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Your job is to provide an implementation for a module and a class whose interface is predefined. We are building a postfix calculator called Calc. For details about postfix notation, see Section 14.10...

1 answer below »

Your job is to provide an implementation for a module and a class whose interface is predefined. We are building a postfix calculator called Calc. For details about postfix notation, see Section 14.10 in your textbook (p XXXXXXXXXXYou have been provided with a driver in the form of a main function with a while loop in it. In addition, there is a module called input that contains a couple of routines for validating input. You must implement the routines in the input module. You will find prototypes in input.hpp.

In order to implement postfix notation, you need a stack. You are provided with an interface for a Stack class that uses the vector class from the Standard Template Library (See Chapter 8 in your textbook). You must implement the Stack class according to the class declaration provided in Stack.hpp.

You must hand in two files: one called input.cpp and one called Stack.cpp. The input.cpp file should contain an implementation of the function prototypes in input.hpp. The Stack.cpp file should contain an implementation of the class declared in the Stack.hpp file.

Your implementation must match the interface exactly. I will drop them into a project directory with my own header files and main program, and they should work without modification.

The Program:

The Calc program is a command-line calculator. When you run it, it shows you a prompt, which is a dollar sign ($). You can type numbers, and you can type mathematical operators. There are five operators: +, -, *, /, and % for add, subtract, multiply, divide, and modulus, respectively. All operators are integer only.

Numbers may be preceded by an optional ‘+’ or ‘-’, followed by one or more digits. This calculator operates on integers only. When you enter a number, it is placed on the stack.

When you enter an operator, the program applies the operator to the top two numbers on the stack. If there are not enough numbers, the program prints an error message. If the operation is successful, the result is printed to the console. The result also remains on the stack top.

You can type ‘p’ to print out the current stack contents. You can type ‘x’ to exit the program.
All other input produces an error message.

Answered Same Day Dec 08, 2021

Solution

Ria answered on Dec 10 2021
131 Votes
Calc/input.cpp
#include "input.hpp"
#include using namespace std;
ool is_a_binary_operator(std::string inputValue, char& op)
{
    int i, f = 0;
    for (i = 0; inputValue[i] != '\0' && f == 0; i++)
    {
        if (inputValue[i] == '+' || inputValue[i] == '-' || inputValue[i] == '*' || inputValue[i] == '/' || inputValue[i] == '%')
        {
            op = inputValue[i];
            f = 1;
        }
    }
    return f;
}
ool is_a_number(std::string inputValue, int& x)
{
    int i, f = 0, num=0;
    for (i = 0; inputValue[i] != '\0'; i++)
    {
        if (isdigit(inputValue[i]))
        {
            num = num * 10 + inputValue[i] - '0';
            x = num;
            f = 1;
        }
    }
    return f;
}
Calc/input.hpp
#ifndef INPUT_HPP
#define INPUT_HPP
#include --------------------------------------------------------------------
Function: is_a_binary_operato
Description: Reads a mathematical operator from a string.
Operators recognized: +, -, *, /, %
If successful, the operator is returned in
the `op' reference parameter.
Parameters: inputValue : string
op : char&
Returns: true if a binary operator was found, and false
otherwise
--------------------------------------------------------------------
ool is_a_binary_operator(std::string inputValue, char&...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here