Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ StackCalculator. Please complete the TO DOs in the porvided code below from the StackCalculator.cpp and driver.cpp files CODE TO COPY: ------------------------------------------------------------ StackCalculator.hpp -------------------------------------------------------------

In C++ StackCalculator. Please complete the TO DOs in the porvided code below from the StackCalculator.cpp and driver.cpp files

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribed

image text in transcribed

CODE TO COPY:

------------------------------------------------------------

StackCalculator.hpp -------------------------------------------------------------

#pragma once

#include

struct Operand

{

float number;

Operand* next;

};

class StackCalculator

{

private:

Operand* stackHead; // pointer to the top of the stack

public:

StackCalculator();

~StackCalculator();

bool isEmpty();

void push(float num);

void pop();

Operand* peek();

Operand* getStackHead() { return stackHead; }

bool evaluate(std::string* s, int size);

};

--------------------------------------------------------------

StackCalculator.cpp

--------------------------------------------------------------

#include

#include "StackCalculator.hpp"

using namespace std;

/*

* Purpose: Determine whether some user input string is a

* valid floating point number

* @param none

* @return true if the string s is a number

*/

bool isNumber(string s)

{

if(s.size() == 1 && s == "-")

return false;

else if(s.size() > 1 && s[0] == '-')

s = s.substr(1);

bool point = false;

for(int i = 0; i

if(!isdigit(s[i]) && s[i] != '.')

return false;

if(s[i]=='.' and !point) point = true;

else if(s[i]=='.' and point) return false;

}

return true;

}

StackCalculator::StackCalculator()

{

//TODO:

}

StackCalculator::~StackCalculator()

{

//TODO:

}

bool StackCalculator::isEmpty()

{

//TODO:

}

void StackCalculator::push(float number)

{

//TODO:

}

void StackCalculator::pop()

{

//TODO:

}

Operand* StackCalculator::peek()

{

//TODO:

return nullptr;// remove this line if you want

}

bool StackCalculator:: evaluate(string* s, int size)

{

/*TODO: 1.scan the array from the end

2.Use isNumber function to check if the input is an operand

3.push all operands to the stack

4.If operator take two element of from the stack,

compute and put the result back in stack

5.Handle the boundery cases as required.

6.Read the writeup for more details

*/

return true;

}

------------------------------------------------------------------------------ driver.cpp

------------------------------------------------------------------------------

#include

#include

#include

#include "StackCalculator.hpp"

using namespace std;

int main()

{

// stack to hold the operands

StackCalculator stack;

int numElement = 0;

string* inparr = new string[50];

// enter a number

string number;

cout

while(true)

{

cout ";

getline(cin, number);

/* TODO

1. Read input (operators and operands) until you encounter a "="

2. store them in inparr

*/

}

/* TODO - If the inparr is empty then print "No operands: Nothing to evaluate"

else call the evaluate function

*/

/* TODO - Validate the expression

1. If valid then print the result cout

2. Else, print "Invalid expression"*/

return 0;

}

OBJECTIVES 1. Create, add to delete from, and work with a stack implemented as a linked list 2. Create, add to delete from, and work with a queue implemented as an array Overview Stacks and Queues are both data structures that can be implemented using either an array or a linked list. You will gain practice with each of these in the following two mini-projects. The first is to build a simple calculator using a linked list stack and the second is to simulate a job scheduling system using a circular array based queue. Stack calculator In this assignment we will implement a linked list based stack. We will use this stack implementation to evaluate prefix expressions. In the following section we will discuss what is a prefix expression and how we can evaluate them using stack. Your task will be to implement the logic into c++ code. We will consider only two binary operators "+" and "" for prefix notations. Prefix Notation In general the way we express a binary operator in mathematical expression is infix. For example "4 + 3". In this expression the order is " operator ". The prefix notation for the will be + 4 3". The order here is "operator ". A visual representation of the operation is given here. Note though *4 + 3" and " +4 3" are different strings but both of them should be evaluated to 7. operator Even if the computation tree is deeper we can follow the same paradigm to represent the 3) operand2 expression. For example consider the expression- operandi 4 (4+3) + (2+5)". The computation tree of the same is given below. For each subexpression we will follow the same procedure "operator ". So the subexpression " 4 + 3" will be + 4 3" and "2 + 5* will be "+ 25". Now these two subexpressions " + 4 3" and "+ 2 5 are the operands for '*'. So the final expression become "* +43 +2 5". * +43 +25 +43 +25 Again note the expression "* + 4 3 + 2 5" evaluates to be 49 which is the same as its infix counterpart" (4+3) + (2+5)". Here is another example for ( 6 + 21 ) + 3 + + 6 213 +6 21 3 6 How to Evaluate To evaluate a prefix expression we will use stack. We will parse the expression from the back. If we encounter a number we will push it to the stack. If we encounter an operator ("+" or **") we will take two elements from the stack top and perform the operation. We will push back the result again in the stack. To determine whether an element is operator or operand we will use isNumber function. Note this function is provided in the cpp file so you do not need to implement is Number function. Consider the example +43+2 5". Our code will accept the notation as an array of strings- 4 3 3 2 5 Index:0 1 2 3 4 5 6 Instruction Stack Index: 0 2 un 3 3 4 + + st 2 6 5 f 5 Push 5 Index 4 2 4 3 3 5 2 6 5 + + 2 1 5 Push 2 Index: 0 . 1 + 2 4 3 3 4 + 5 2 7 t Pop two elements. Perform the operation '+'. Push the result. Index: 0 1 + N+ 3 3 4 + 5 2 5 3 7 Push 3 Index: 0 4 * 1 + 2 4 3 3 5 2 uno + 4 3 7 Push 4 Index: 0 1 + 2 4 4 + 5 2 U 7 7 Pop two elements. Perform the operation +. Push the result. Index: 0 1 + 2 4 3 3 4 + 5 2 6 5 49 Pop two elements. Perform the operation. Push the result. Class Specifications The node structure for the linked list is defined in StackCalculator.hpp. struct Operand { float number; // the number to store Operand* next; // the pointer to the next node }; The StackCalculator class definition is provided in the file StackCalculator.hpp in Canvas. Do not modify this file or your code won't work on Coderunner! Fill in the file StackCalculator.cpp according to the following specifications. Operand stackHead; Points to the top of the stack StackCalculator(); Class constructor, set the stackHead pointer to NULL -StackCalculator(); Destroy the stack. Take special care not to leave any memory leaks. bool isEmpty0; Returns true if the stack is empty void push(float num); Push a float element to the stack void pop(); Pop the element out of the stack. Do not leave any memory leak. Print "Stack empty, cannot pop a job." if the stack is empty. Operand* peek(); Returns the top of the stack. Print "Stack empty, cannot peek." if the stack is empty. Operand" getStackHead() { return stackHead; } Returns the stackHead. This is already implemented. bool evaluate(std::string* s, int size); This function will accept an array of strings and the length of the array. The array represents the prefix expression. This function will evaluate the expression and will store the ultimate result in the stack. If the function encounters any other operator than "+" or *** it should print "err: invalid operation" and return false. If the array does not have the correct number of operands it should print "err: not enough operands" and return false. If everything goes well it will return true. Driver code The driver code should start with printing "Enter the operators and operands (+', ***) in a prefix format" It will keep taking input from the user and will store the input to a string array. Print "#>" to each line of the console while accepting input. Refer to the example run. If user inputs "=" it will stop accepting the input and will call the evaluate function with the string array If the expression is not valid it should print "Invalid expression" For valid expression it should print the result with "Result=" Free any unused memory Example Runs: 1. Enter the operators and operands ('+','*') in a prefix format #> #> + #> 4 #> 3 #> + #> 2 #> 5 Result= 49 2. Enter the operators and operands ('+','*') in a prefix format #> + #> + #> 14 #> 12 #> 3 #> = Results 29 3. Enter the operators and operands ('+','*') in a prefix format #> * #> + #> 4 #> 3 #>= err: not enough operands Enter the operators and operands ('+','*') in a prefix format #> * #> 12 #> 20 #> 10 #> = err: invalid operation 6. Enter the operators and operands ('+', '*') in a prefix format #> #> + #> 4 #> 3 #> 2 #> 5 #> = Invalid expression

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

Spatial Databases A Tour

Authors: Shashi Shekhar, Sanjay Chawla

1st Edition

0130174807, 978-0130174802

More Books

Students also viewed these Databases questions

Question

40. Profits increased when workers saw the big picture.

Answered: 1 week ago

Question

=+6 Should we pursue centralized or localized HRM policies?

Answered: 1 week ago

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago