Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ problem: Your goal is to implement a stack based prefix expression evaluator. Your program will use the code given below, and your job is

C++ problem: Your goal is to implement a stack based prefix expression evaluator. Your program

will use the code given below, and your job is to complete the evaluatePrefix function so that it

will accept a string that is an expression in prefix notation and evaluates it to return the result. You will

need a complete source code, and tests for an answer.

#include

#include

#include

using namespace std;

double evaluatePrefix (string expr) {

stack Stack;

for (int j = expr.size () - 1; j >= 0; j--) {

// if jth character is the delimiter ( which is

// space in this case) then skip it

if (expr[j] == ' ')

continue;

// Push operand to Stack

// To convert expr[j] to digit subtract

// '0' from expr[j].

if (isdigit (expr[j])) {

// there may be more than

// one digit in a number

double num = 0, i = j;

while (j

j--;

j++;

// from [j, i] expr contains a number

for (int k = j; k =>

num = num * 10 + double (expr[k] - '0');

Stack.push (num);

}

else {

// Your code goes here

}

}

}

return Stack.top ();

}

// Driver code

int main () {

string input;

while (true) {

cout

getline (cin, input);

if (input.empty ()) {

cout

return 0;

}

cout

}

}

\"\"\"

Furniture - models an IKEA furniture item; considered abstract, as by itself,

it is meaningless as it doesn't include everything needed.

ALL CODE IN THIS LAB:

Author: C.R. Vessey, SMCS

Date: 17-FEB-2021

\"\"\"

class Furniture:

def __init__(self, s_type, s_artnum, f_price, s_name, s_desc, s_colour):

# Assume that fully private instance variables will be made using

# these parameter names, but without the \"s_\" or \"f_\" prefix.

# \"s_\" denotes a string type parameter, \"f_\" a float.

# Complete the constructor (replace these comments with appropriate code)

def __str__(self):

return \"Type: \" + self.__type + \\

\" \\tName: \" + self.__name + \\

\" \\tArticle: \" + self.__artnum + \\

\" \\tPrice: \" + f\"{self.__price:.2f}\" + \\

\" \\tDescription: \" + self.__desc + \\

\" \\tColour: \" + self.__colour

def setPrice(self, p_price):

# add code to set the price

def getPrice(self):

# add code to return the price

\"\"\"

Armchair - models an IKEA armchair, which is a type of Furniture.

Collects all data used by Furniture, which it extends, as well as data specific

to this object.

Uses the superclass __init__ and superclass __str__ method to accomplish its

tasks.

NOTE: This class is FULLY COMPLETE. It's intended to be an example for the

other classes which you will be completing on your own.

\"\"\"

class Armchair(Furniture):

def __init__(self, s_artnum, f_price, s_name, s_desc, s_colour, \\

f_width, f_depth, f_height, f_seat_width, f_seat_depth, f_seat_height):

super().__init__(\"Armchair\", s_artnum, f_price, s_name, s_desc, s_colour)

self.__width = f_width

self.__depth = f_depth

self.__height = f_height

self.__seat_width = f_seat_width

self.__seat_depth = f_seat_depth

self.__seat_height = f_seat_height

def __str__(self):

return super().__str__() + \" \\tWidth: \" + f\"{self.__width:.2f}\" + \\

\" \\tDepth: \" + f\"{self.__depth:.2f}\" + \\

\" \\tHeight: \" + f\"{self.__height:.2f}\" + \\

\" \\tSeat Width: \" + f\"{self.__seat_width:.2f}\" + \\

\" \\tSeat Depth: \" + f\"{self.__seat_depth:.2f}\" + \\

\" \\tSeat Height: \" + f\"{self.__seat_height:.2f}\"

### FROM THIS POINT ONWARD, YOU WILL BE WRITING THE CONTENTS OF EACH ENTIRE CLASS ###

\"\"\"

Bed - models an IKEA bed frame; collects all data used by Furniture, which it extends,

as well as data specific to this object. Uses the superclass __init__

and superclass __str__ methods to accomplish its tasks.

\"\"\"

class Bed(Furniture):

# write __init__ and __str__ methods appropriate to this

\"\"\"

Nightstand - models an IKEA nightstand; collects all data used by Furniture, which it extends,

as well as data specific to this object. Uses the superclass __init__

and superclass __str__ methods to accomplish its tasks.

\"\"\"

class Nightstand(Furniture):

# you write this one's code, too

\"\"\"

Floorlamp - models an IKEA floorlamp; collects all data used by Furniture, which it extends,

as well as data specific to this object. Uses the superclass __init__

and superclass __str__ methods to accomplish its tasks.

\"\"\"

class Floorlamp(Furniture):

# and you write the code for this one as well

##### MAIN UNIT TEST CODE - partial code is provided, you write what is indicated #####

\"\"\"

UNIT TEST FRAMEWORK

You have received one example subclass (Armchair), and the rest you will have

to complete.

Below, you see partial unit test driver code. Commentary will give you hints

as to how things are to be done.

* Tests Task 1 of Lab 2

* Reads data from file, figure out the category, and then creates the appropriate object,

putting it into the inventory list.

* Prints out the contents of the inventory, as well as the total value of the inventory.

\"\"\"

# create the inventory list

inventory = []

# set up the file to be read

file = open(\"ikea.txt\", \"r\")

# read the data file, and create objects out of what is read

# note that this requires identification of the type code, which tells you

# what type of object will be created from the data

for line in file:

data = line.split(\",\")

# get the item type code

type_code = data[0].strip().upper() # type codes should all be uppercase

# now read the data common to ALL Furniture items (the stuff that Furniture's

# __init__ method requires)

article_number = data[1].strip()

price = float(data[2].strip())

# DO THE SAME FOR NAME, DESCRIPTION, AND COLOUR (data[3] - data[5])

# Now, based on the type code, process the rest of the data and create an appropriate

# object based on the data read

if type_code == \"B\": # it's a bed

length = float(data[6].strip())

width = float(data[7].strip())

footboard_height = float(data[8].strip())

headboard_height = float(data[9].strip())

inventory.append(Bed(articleNumber, price, name, description, colour, \\

length, width, footboard_height, headboard_height))

elif type_code == \"N\": # it's a nightstand

# WRITE CODE FOR THIS

elif type_code == \"A\": # it's an armchair

# WRITE CODE FOR THIS

elif type_code == \"F\": # it's a floor lamp

# WRITE CODE FOR THIS

else:

print(\"ERROR - UNIDENTIFIED TYPE CODE:\", type_code)

# SET AN ACCUMULATOR VARIABLE CALLED total_value TO 0

for furniture_item in inventory:

print(str(furniture_item)) # print the item

print(\"=\"*30) # print a separator

# ADD THE PRICE OF THE ITEM TO THE ACCUMULATOR VARIABLE

print(\" Total Inventory Value:\", total_value)

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

Students also viewed these Programming questions

Question

What are the responsibilities of the position?

Answered: 1 week ago

Question

2x 2x 2x3 4 2X2 2 x 5x 2x3 1 8 x x 4x 11 2 + 2X2 = 4

Answered: 1 week ago

Question

Question 1 2 pts If \ pi 1 A = \ pi 1 B then what is \ sigma 2 C ?

Answered: 1 week ago

Question

9. I dont know where the two of (we, us) ________ went wrong.

Answered: 1 week ago