Question
I'm trying to make expression tree by using binary tree with printing in postfix or infix, and calculating. I really don't know how I start
I'm trying to make expression tree by using binary tree with printing in postfix or infix, and calculating. I really don't know how I start it.
Please help me!
Thank you.
===============================================================
Btree.h
#ifndef Btree #define Btree
#include
template
#endif
==================================================================
Exptree.h
#ifndef Ex_T #define Ex_T
#include "Btree.h"
#include
using std::string;
class Exptree : private Btree
public: Exptree(); // make an empty exp. tree. Exptree(string input); //make an exp. tree from given string exp. void resetAndStoreExp(string input); // clear exp. tree and stores the given exp. in tree. double getCal(); //return result of evaluation the exp. tree. void printInOrder(std::ostream& out); // outputs the tree in order. void printPostOrder(std::ostream& out); // output the tree post order. };
#endif
=======================================================
Exptree.cpp
#include "Exptree.h"
Exptree::Exptree():Btree(){ }
Exptree::Exptree(string input):Btree(){ }
void Exptree::resetAndStoreExp(string input){ }
double Exptree::getCal(){ //for making it compile return -5; }
void Exptree::printInOrder(std::ostream& out){ }
void Exptree::printPostOrder(std::ostream& out){ }
===========================================================================
ExptreeTest.cpp
#include "Exptree.h"
#include
#include
#include
using std::cout;
using std::endl;
using std::string;
using std::ostringstream;
int tot_cases = 0;
int passed_cases = 0;
template
void testCase(string cases, T ori, U ans){
tot_cases++;
if(ori == ans){
passed_cases++;
cout << "Passed " << cases << endl;
}
else{
cout << "****No passed**** " << cases << endl;
cout << " Your output: " << ori << " Answer: " << ans << endl;
}
}
int main() {
string exp;
ostringstream op;
string result;
Exptree ex1;
ex1.printInOrder(op);
testCase("TC 1", op.str(), "");
testCase("TC 2", ex1.getCal(), 0);
op.str("");
op.clear();
Exptree *ex = new Exptree("(8+2)");
ex->printInOrder(op);
testCase("TC 3", op.str(), "8+2");
op.str("");
op.clear();
ex->printPostOrder(op);
testCase("TC 4", op.str(), "8 2 +");
testCase("TC 5", ex->getCal(), 10);
op.str("");
op.clear();
exp = "(751+352)";
ex->resetAndStoreExp(exp);
ex->printPostOrder(op);
testCase("TC 6", op.str(), "751 352 +");
testCase("TC 7", ex->getCal(), 1103);
op.str("");
op.clear();
exp = "(9-3)";
ex->resetAndStoreExp(exp);
ex->printInOrder(op);
testCase("TC 8", op.str(), "9-3");
testCase("TC 9", ex->getCal(), 6);
op.str("");
op.clear();
exp = "((7-5)-3)";
ex->resetAndStoreExp(exp);
ex->printPostOrder(op);
testCase("TC 10", op.str(), "7 5 - 3 -");
testCase("TC 11", ex->getCal(), -1);
op.str("");
op.clear();
exp = "(2^3)";
ex->resetAndStoreExp(exp);
ex->printInOrder(op);
testCase("TC 12", op.str(), "2^3");
testCase("TC 13", ex->getCal(), 8);
op.str("");
op.clear();
exp = "(7*(8/2))";
ex->resetAndStoreExp(exp);
ex->printPostOrder(op);
testCase("TC 14", op.str(), "7 8 2 / *");
testCase("TC 15", ex->getCal(), 28);
op.str("");
op.clear();
exp = "(((((7+8)-2)*10)/(5+2))^2)";
ex->resetAndStoreExp(exp);
ex->printInOrder(op);
testCase("TC 16", op.str(), "7+8-2*10/5+2^2");
testCase("TC 17", ex->getCal(), 324);
op.str("");
op.clear();
exp = "(((((5+(1*(5+(12/2))))*2)+10)/2)+(((2^3)+6)/2))";
ex->resetAndStoreExp(exp);
ex->printPostOrder(op);
testCase("TC 18", op.str(), "5 1 5 12 2 / + * + 2 * 10 + 2 / 2 3 ^ 6 + 2 / +");
testCase("TC 19", ex->getCal(), 28);
op.str("");
op.clear();
Exptree *ex2 = new Exptree(*ex);
ex2->printInOrder(op);
testCase("TC 20", op.str(), "5+1*5+12/2*2+10/2+2^3+6/2");
testCase("TC 21", ex2->getCal(), 28);
op.str("");
op.clear();
Exptree ex3 = *ex;
ex3.printPostOrder(op);
testCase("TC 22", op.str(), "5 1 5 12 2 / + * + 2 * 10 + 2 / 2 3 ^ 6 + 2 / +");
testCase("TC 23", ex3.getCal(), 28);
op.str("");
op.clear();
ex2->resetAndStoreExp("(7*(9/3))");
ex2->printInOrder(op);
testCase("TC 24", op.str(), "7*9/3");
testCase("TC 25", ex2->getCal(), 21);
op.str("");
op.clear();
ex->printPostOrder(op);
testCase("TC 26", op.str(), "5 1 5 12 2 / + * + 2 * 10 + 2 / 2 3 ^ 6 + 2 / +");
testCase("TC 27", ex->getCal(), 28);
delete ex;
ex = nullptr;
delete ex2;
ex2 = nullptr;
op.str("");
op.clear();
ex3.printInOrder(op);
testCase("TC 28", op.str(), "5+1*5+12/2*2+10/2+2^3+6/2");
testCase("TC 29", ex3.getCal(), 28);
op.str("");
op.clear();
ex3.resetAndStoreExp("");
ex3.printPostOrder(op);
testCase("TC 30", op.str(), "");
testCase("TC 31", ex3.getCal(), 0);
cout << "==========================================" << endl;
cout << "Passed " << passed_cases << "/" << tot_cases << endl;
return 0;
}
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