Answered step by step
Verified Expert Solution
Question
...
1 Approved Answer
Background: Stack 1 12 6 5 Push Push 4 Push 3 Push 2 Push 4 3 2 1 3 2 1 5 4 3 2
Background: Stack 1 12 6 5 Push Push 4 Push 3 Push 2 Push 4 3 2 1 3 2 1 5 4 3 2 1 2 1 1 6 6 10 Pop 5 4 5 4 3 2 1 Pop 4 3 2 1 Pop 2 Pop 3 2 1 Pop 2 1 1 Simple representation of a stack runtime with push and pop operations. (https://en.wikipedia.org/wiki/Stack_(abstract_data_type)#/media/File:Lifo stack.png) [LW] Stack Calculator CSCE 121 A stack is an abstract data type that serves as a collection of elements and has two principal operations: 1. push : add an element at the top of the stack : remove the element at the top of the stack A stack may also have a peek operation, which gives access to the top element without removing it. 2. pop Grading As stated in the syllabus, you must complete both items below to receive credit for this week's lab. The Teaching Assistants will strictly enforce attendance that is described in this section at the start and end of your lab. complete the labwork attend your assigned lab . Submission Grade You must submit the completed labwork as two files on Mimir: Stack.cpp: the data structure code to be used in your stack calculator RPN.cpp: the code for the stack calculator user interface [LW] Stack Calculator CSCE 121 Instructions Below are the full set of instructions for this labwork. The Teaching Assistants will provide assistance in this section while your group is working on the labwork. Task 1: Implement a Stack (of doubles) Read Stack. h, which contains the definition of the Stack type and declarations of the Stack operations which you must implement. = Pushing and popping should be done at the back (last element) of the array: Let A = [1, 2, 3, ], capacity 4, length 3 Push 4 Now A = [1, 2, 3, 4], capacity 4, length = 4 Pop (returns 4) Now A = [1, 2, 3, ], capacity = 4, length 3 Push 5 Now A = [1, 2, 3, 5], capacity = 4, length = 4 = Resizing the array should double the capacity: = 4 Resizing the array should double the capacity: Let A = [1, 2, 3, 4], capacity = 4, length Push 5 Now A [1, 2, 3, 4, 5, ], capacity = = 8, length = 5 Task 2: Implement a 4-function RPN Calculator Reverse Polish Notation (RPN) is a mathematical notation in which operators follow their operands (also called postfix notation). For example, the arithmetic expression 1 + 2 would be written as 1 2 + in RPN. . One of the benefits of RPN is that parentheses are not required. For example, the arithmetic expression 1 + 2* 3/4 - 5 is ambiguous without some assumption about the order of operations. . According to the standard order of operations (PEMDAS), the expression means: (1 + ((2 * 3) / 4)) - 5 In RPN, the equivalent expression is: 1 2 3 * 4 / + 5 - [LW] Stack Calculator CSCE 121 The RPN algorithm is simple and uses a stack to store intermediate values of the computation: 1. Read numbers and push onto the stack until you read an operator (one of +, -, *, /) or = a. Hint: read a string (e.g. std::string or char[]), then check if the first character is a digit or. (dot). If so, then the string is an operand (convert to double with stod(std::string) or atof(char[])), otherwise the string is an operator (use switch to handle the different cases). i. Or, attempt the conversion to double and catch the exception if the string is not a double (therefore it is an operator). 2. If an operator (+, -, *, or /) is read, a. pop the top two elements of the stack into variables for right and left operands i. First pop goes into right ii. Second pop goes into left b. Do the operation and push the result onto the stack 3. Repeat steps 1--3 until '=' is read 4. Pop the result from the stack You should read from standard input and print to standard output. Constraints 1. Your source code file must be named: a. Stack.cpp: implements Stack b. RPN.cpp: implements 4-func RPN calc 2. You may #include a. : for using the NAN constant b. : for std::cin, cout, endl c. : for std::string, stod(), isdigit() d. Stack.h: for using your Stack.cpp functions e. and nothing else. 3. You will submit only: a. Stack.cpp: implements Stack b. RPN.cpp: implements 4-func RPN calc 4. The starter code contains: a. Stack.h\" 5. Your program should not leak memory a. Every new should have a corresponding delete (or delete[]) [LW] Stack Calculator CSCE 121 Examples for RPN Calculator Dark Red Bold text is user input. Input 1 2 3 * 4 / + 5 = Expected Output -2.5 Input 11 1 2 + 3 * 4 / 5 - = Expected Output -2.75 Input 10 10 10 * * 59 + 1024 8 * * 19 10000 / = Expected Output 867.531 Input 10 05 - = Expected Output 15 Input 1 2 3 4 5 6 7 8 9 10 + + + + + = Expected Output 54 [LW] Stack Calculator CSCE 121 Input 3 2 A = Expected Output [ERROR] invalid operator: I need help with Tas-2. Please make sure you are only using the includes that are allowed for task-2 or else it does not pass any test cases. We are not allowed to include stack.h for task-2.The includes that are allowed are mentioned under 'Constrains', please look at it.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
![blur-text-image](https://dsd5zvtm8ll6.cloudfront.net/includes/images/document_product_info/blur-text-image.webp)
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
![blur-text-image](https://dsd5zvtm8ll6.cloudfront.net/includes/images/document_product_info/blur-subtext-image.webp)
Step: 3
![blur-text-image](https://dsd5zvtm8ll6.cloudfront.net/includes/images/document_product_info/blur-subtext-image.webp)
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started