#include #include #include #include #include "node.h" #include "NodeCreator.h" #include "globals.h" #include #include "VariableMap.h" #include "ProcessString.h" #include "arithmetica.h" using namespace std; typedef vector NodeList; typedef stack NodeStack; /** *NodeCreator is a class that is responsible for creating nodes from the users *string input. The nodes it creates are dependent on the operators, *If an unknown(undefined) input is entered, the string will be processed as *if it is not there. * *Written by Jessica Smith */ //constructor NodeCreator::NodeCreator(const VariableMap & VariableList) :myVariable(VariableList), mySpecialInstructions(false), myProcessorType(new Algebra()) { } NodeCreator::NodeCreator(const VariableMap & VariableList, Arithmetica * processor,bool SpecialInstructions) :myVariable(VariableList), mySpecialInstructions(SpecialInstructions), myProcessorType(processor) { } NodeStack NodeCreator::makePostfix(const NodeList & nodeList) { //Precondition: passed a vector of nodes in infix order //Postcondition: Returns a stack of nodes with the operator of highest precedence on top in postfix order NodeStack postStack;//creates stack to be returned NodeStack operatorStack;//stack to be used for operators for(int k=0;kspecialEvaluation(nodeStack.top())) { return(myProcessorType->specialTreeCreation(nodeStack)); } else if(!nodeStack.empty()) { Node * Top=nodeStack.top(); nodeStack.pop();//pops off top if(Top->precedence()==CONSTANT_PRECEDENCE)return Top;//if constant return self if(Top->precedence()==VARIABLE_PRECEDENCE)//if it is a variable { Top->changePrecedence(CONSTANT_PRECEDENCE); //if it is just a variable looks up value and creates a constant with that value if(nodeStack.empty()) { Top->Left(myVariable.lookUpVar(Top->description())); } //else pops off equal sign else if(nodeStack.top()->precedence()==EQUALS_PRECEDENCE) { nodeStack.pop(); Top->Left(makeNodeTree(nodeStack));//set leaf recursively } } else if(Top->precedence()==UNARY_PRECEDENCE)//check for unary operator { Top->Left(makeNodeTree(nodeStack));//assign leaf } //assign two leaves recursively for binary operators else { Top->Right(makeNodeTree(nodeStack));//recursively calls to set RHS Top->Left(makeNodeTree(nodeStack));//recursively calls to set LHS } return(Top); } return(new Constant(0)); } } void NodeCreator::postFixHelper( NodeStack & operatorStack, NodeStack & postStack, Node* entry) { //pushes operators onto operatorStack and pushes off all with greater precedence while((!operatorStack.empty())&& (entry->precedence()<=operatorStack.top()->precedence())&&(operatorStack.top()->precedence()!=OPEN_PAREN_PRECEDENCE)) { //checks operators on OperatorStack pushes them onto the stack to be returned if they are of equal or lesser precedence than current one postStack.push( operatorStack.top()); operatorStack.pop(); } if(entry->precedence()!=CLOSED_PAREN_PRECEDENCE&&entry->precedence()!=COMMA_PRECEDENCE) { operatorStack.push(entry); } if((!operatorStack.empty())&&operatorStack.top()->precedence()==OPEN_PAREN_PRECEDENCE &&entry->precedence()!=OPEN_PAREN_PRECEDENCE) { operatorStack.pop(); } } void NodeCreator::clearOperatorStack(NodeStack & operatorStack, NodeStack & postStack) { //clears off the rest of the operator stack while(!operatorStack.empty()) { //Pushes of any leftover signs if(operatorStack.top()->precedence()!=OPEN_PAREN_PRECEDENCE) { postStack.push(operatorStack.top()); } operatorStack.pop(); } }