Question
Can you help me with the //TODO parts in the Stackcpp.h file? Thanks! Stackcpp.h #include Stack.h template //TODO T Stack ::peek(){ //if stack is not
Can you help me with the //TODO parts in the Stackcpp.h file? Thanks!
Stackcpp.h
#include "Stack.h" template
//TODO
T Stack
} //TODO template
template
main.cpp
#include
#include "Stackcpp.h"
//TODO //Requires: input expression //Effects: checks( ) parenthesis matching. Multiple pairs ok, nested pair checking not need eg. (()) //Modifies: nothing bool checkMatch(std::string expression){ //save the expression in a stream std::istringstream input(expression); //create a storage stack Stack
} //GIVEN //return an integer that corresponds to the OPERATOR
//higher precedence gives higher value //same level of precedence gives the same value int precedence(char oper){ if (oper=='+'|| oper== '-') return 1; if (oper=='*'|| oper== '/') return 2; return 0; } //GIVEN //check if character is a number bool isOperand(char ch){ return (ch>='0'&&ch<='9'); }
//TODO //Requires the accurately convered expression as a post fix string //Effects: computes and evaluates the postfix expression by performing Stack //operations, Uses multiple stacks //Modifies nothing int evaluate(std::string postfix){ std::istringstream input(postfix); char ch; int data=0; Stack
//store the expression on the stack while (input >> ch) { if (isOperand(ch)) { data = 0; while (isOperand(ch)) { data = data * 10 + (int)(ch - 48); input >> ch; } result.push(data); continue; } } //since this reverses the postfi expression, store in another stack to //reverse the stack //if operand, store numeric value on result stack // use int(ch)-48 to convert char to integer value // if its an operator, pop two operands from result stack // apply the operator on them and store the resulting value in result stack // GIVEN //switch (ch){ // case '*': result.push(one*two); break; // case '/':result.push(one/two);break; // case '+': result.push(one+two); break; //case '-': result.push(one-two); break; // default: break;
// the final value is on the top of the stack, return this return result.pop(); } //GIVEN check precedence level of two operators bool checkPrec(char oper1, char oper2){ if (precedence(oper1)>precedence(oper2)) return true; else return false; } //TODO std::string convertToPostfix(std::string expression){ Stack } int main() { std::string expression1 ="1 + (3 * 5) / (6 - 4)"; std::string expression2 ="(1 + 3 )* 5 / (6 - 4)+2"; std::string expression3 ="1 + (3 * 5) / 6 - 4)"; std::string expression4 ="1 + (3 * 5() / (6 - 4)"; convertAndCalculate(expression1); convertAndCalculate(expression2); convertAndCalculate(expression3); convertAndCalculate(expression4); } LinkedList.h #include "ListNodecpp.h" template public: ListNode public: LinkedList public: int size(); public: void add( T t); public: std::string toString(); void printList(); }; LinkedListcpp.h #include "LinkedList.h" template } ListNode.h #include //template // post: constructs a node with data 0 and null link public: ListNode(); public: ListNode(T data); public: ListNode(T idata, ListNode #include "ListNode.h" template Stack.h #include "LinkedListcpp.h" //So Stack inherits from LinkedList and has access to all LinkedList Methods // Stack IS A LinkedList template }; MUST MATCH PROVIDED OUTPUT TEXT Expression= 1 + (3 * 5) / (6 - 4) 1 + (3 * 5) / (6 - 4)1 + (3 * 5) / (6 - 4) Post Fix Expression= 135*64-/+ Result of evaluation = 8 Expression= (1 + 3 )* 5 / (6 - 4)+2 (1 + 3 )* 5 / (6 - 4)+2(1 + 3 )* 5 / (6 - 4)+2 Post Fix Expression= 13+5*64-/2+ Result of evaluation = 12 Expression= 1 + (3 * 5) / 6 - 4) 1 + (3 * 5) / 6 - 4)Parenthesis mismatch Expression= 1 + (3 * 5() / (6 - 4) 1 + (3 * 5() / (6 - 4)1 + (3 * 5() / (6 - 4)Parenthesis mismatch
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started