IN C++ PLEASE
postfixEval.cpp file
#include "stack.hpp"
using namespace std;
int main(){freopen("input_postfixEval.txt", "r", stdin);string s;int solution;int line_counter = 0;while(cin>>solution){cin>>s;Stack stack;// The input file is in the format "expected_solutionpostfix_expression"
// We assume that all operands in the input are one digit (sothey range from 0 to 9)for(int i=0; i // WRITE CODE HERE to evaluate the postfixexpression in s// At the end of the loop, stack.pop() should contain the value ofthe postfix expression}
// Checking whether the value you calculated is correct...int value = stack.pop();
if(solution == value){cout << "line " << line_counter << ": OK ["<< solution << " " << value << "]" <<endl;}else{cout << "line " << line_counter << ": ERROR ["<< solution << " " << value << "]" <<endl;}line_counter++;}}
stack.cpp file
#include "stack.hpp"
using namespace std;
templatevoid Stack::push(T c){if(topIndex > MAXSIZE-1){cout<<"Stack overflow"< return;}arr[topIndex + 1] = c;topIndex++;}
templateT Stack::pop(){if(topIndex < 0){cout<<"Cannot delete. Stack empty."< }
return arr[topIndex--];}
templateT Stack::peek(){if(topIndex < 0){cout<<"Cannot peek. Stack empty."< }return arr[topIndex];}
templateint Stack::size(){return topIndex+1;}
templatevoid Stack::display(){for(int i=topIndex; i>=0; --i){cout< }cout< }
template class Stack;template class Stack;
stack.hpp file
//#include#include#include#include#include
// Ideally this would not be a huge number, you could also use avector#define MAXSIZE 100000
using namespace std;templateclass Stack{private:T arr[MAXSIZE]; // the actual stackint topIndex; // index of the top element
public:Stack(){topIndex = -1; // constructor};~Stack(){}; // destructorvoid push(T c); // push c to the listT pop(); // return and remove the top element in the stackT peek(); // return the top element in the stackint size(); // returns the size of the stackvoid display(); // display the stack in stdout};
input_postfixEval.txt file
6 33+12 62*1 65-2 37+2*64+/
Need help evaluating the postfix
The test cases follow this format: expected_solution input.Given input, your job is to implementcode that gets the expected_solution. Obviously, you need tocalculate expected_solution, not justprint it.
IN C++ PLEASE postfixEval.cpp file #include "stack.hpp" using namespace std; int main(){ freopen("input_postfixEval.txt"
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am