Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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::peek(){ //if stack is not empty, return a copy of the top element // otherwise throw an exception and exit

} //TODO template T Stack:: pop(){ //if Stack is empty throw an exception //Otherwise return a copy of the top element // and remove it from the stack } //TODO

template void Stack:: push(T e){ // Add the element e to the top of the stack } This is the code that goes along with it. Thanks!

main.cpp

#include #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 store; char ch; //read from stream character by character while( input>>ch){ // if the character is '(' push it onto the stack if (ch == '(') { store.push(ch); } // if character is ')' remove the matching '(' from the stack. If stack is empty return false //if not empty and '(' not found on the stack, return false if (ch == ')') { if (store.empty() || store.peek() != '(') { return false; } store.pop(); } } // if parenthesis are matched, then stack is empty return store.empty();

} //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 result; Stack ops;

//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 opstack; if (expression.compare("") ==0) return ""; std::istringstream input(expression); std::string postfix=""; char ch; while (input>>ch){ if (isOperand(ch)) { postfix += ch; } else if (ch == '(') { opstack.push(ch); } else if (ch == ')') { while (!opstack.empty() && opstack.peek() != '(') { postfix += opstack.pop(); } opstack.pop(); } else { while (!opstack.empty() && opstack.peek() != '(' && checkPrec(opstack.peek(), ch)) { postfix += opstack.pop(); } opstack.push(ch); } } while (!opstack.empty()) { postfix += opstack.pop(); } return postfix; } //GIVEN void convertAndCalculate(std:: string expression){ std::string postfix=""; int result=0; std::cout<<"Expression= "<

} 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 class LinkedList{

public: ListNode * front; int count;

public: LinkedList(); public: bool empty();

public: int size(); public: void add( T t); public: std::string toString(); void printList();

};

LinkedListcpp.h

#include "LinkedList.h" template LinkedList::LinkedList(){ front = nullptr; count=0; } template bool LinkedList::empty(){ return (front == nullptr); } template int LinkedList::size(){ return count; } template void LinkedList::add( T t){ ListNode* e = new ListNode(t,front); front=e; count++; } /* template std::string LinkedList:: toString(int i){ return get(i).toString(); } */ template void LinkedList::printList(){ ListNode * cur =front; while (cur!=nullptr){ std::cout<toString()<<" "; cur=cur->next; }

} ListNode.h

#include #include template

//template class ListNode{ public: T data; ListNode * next;

// post: constructs a node with data 0 and null link public: ListNode(); public: ListNode(T data); public: ListNode(T idata, ListNode * inext); public:std::string toString(); }; ListNodecpp.h

#include "ListNode.h" template // post: constructs a node with data 0 and null link ListNode:: ListNode() { std::cout<<" IN constructor"< ListNode:: ListNode(T idata) { data=idata; next=nullptr; } template ListNode:: ListNode(T idata, ListNode* inext) { data = idata; next = inext; } template std::string ListNode::toString(){ return data.toString(); }

Stack.h

#include "LinkedListcpp.h" //So Stack inherits from LinkedList and has access to all LinkedList Methods // Stack IS A LinkedList template class Stack: public LinkedList{ public: T peek(); public: T pop(); public: void push(T e);

};

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

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_2

Step: 3

blur-text-image_3

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

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions

Question

3 How the market system answers four fundamental questions.

Answered: 1 week ago

Question

5 The mechanics of the circular flow model.

Answered: 1 week ago

Question

1 The difference between a command system and a market system.

Answered: 1 week ago