// Stephen Bassoli // CSE 124 Section 01 // Lab 4 #include #include #include #include #include const int MaxStackSize = 50; #ifndef OPERATOR_H #define OPERATOR_H template class Operator { private: bool StackFull(void)const; // Input: None // Precond: None // Process: Checks whether stack is full // Output: Returns true if stack is full // Postcond: None S* stacklist; int top; int ArraySize; public: Operator(); // Initial Values: None // Process: Initializes the top of the stack Operator(const Operator& elm); ~Operator(); // Process: Deallocates memory stacklist void Resize(); // Input: None // Precond: Top must equal ArraySize or Top must be 6 less than ArraySize // Process: Resizes the array // Output: Resized array // Postcond: None void Push(const S& newItem); // Input: An item to be added to the stack // Precond: None // Process: Store the item at the top of the stack // Output: None // Postcond: Item has been placed at the top of the stack S Pop(void); // Input: None // Precond: Stack is not empty // Process: Remove item from the top of the stack // Output: Returns the element removed // Postcond: Element from the top is removed void ClearStack(void); // Input: None // Precond: None // Process: Sets top to -1 // Output: None // Postcond: Stack is reset to initial values S Peek(void); // Input: None // Precond: Stack is not empty // Process: Retrieve value from the top of the stack // Output: Return the value of the item // Postcond: Stack is unchanged bool StackEmpty(void)const; // Input: None // Precond: None // Process: Checks whether stack is empty // Output: Returns true if stack is empty // Postcond: None char* Convert(ofstream& outfile); // Input: External file which as a infix equation in it // Precond: Must have correct operators // Process: Converts infix to postfix // Output: Character string containing post fix equation // Postcond: None char* AppendStr(char string[100], char Token[50], int length, bool first); // Input: Base string to be modified, string to be added, length of string to be added // Precond: None // Process: Adds on token to the string // Output: Returns character string of the converted char aurgument // Postcond: None bool Prec(char First, char Second); // Input: Two operators // Precond: Must be +, -, *, /, or % // Process: Assigns a precidence to each operator // Output: If the precidence of the first operator is great, return true // Postcond: None }; template bool Operator::StackFull()const { return (top == MaxStackSize-1); } template Operator::Operator() { top = -1; ArraySize = 2; stacklist = new S[ArraySize]; } template Operator::Operator(const Operator& elm) { cout << "COPY CONSTRUCTOR\n"; top = elm.top; ArraySize = elm.ArraySize; S* newlist = new T[ArraySize]; S* source = elm.stacklist; S* destination = newlist; int n = ArraySize; while (n--) *destination++ = *source++; stacklist = newlist; } template Operator::~Operator() { cout << "DESTRUCTOR\n"; stacklist = new S[0]; // needed in case of empty array delete [] stacklist; } template void Operator::Resize() { if (top < ArraySize-6) { int n = ArraySize; S* newlist = new T[ArraySize-3]; S* source = stacklist; S* destination = newlist; while (n--) *destination++ = *source++; delete [] stacklist; stacklist = newlist; ArraySize -= 3; } else if (top >= ArraySize) { int n = ArraySize; S* newlist = new T[ArraySize+3]; S* source = stacklist; S* destination = newlist; while (n--) *destination++ = *source++; delete [] stacklist; stacklist = newlist; ArraySize += 3; } } template void Operator::Push(const S& item) { if (StackFull()) { cerr << "Stack Overflow!\n"; exit(1); } top++; stacklist[top] = item; } template bool Operator::StackEmpty()const { return (top == -1); } template S Operator::Pop() { S temp; if (StackEmpty()) { cerr << "Attempt to pop an empty stack!" << endl; exit(1); } temp = stacklist[top]; top--; return temp; } template S Operator::Peek() { if (StackEmpty()) { cerr << "Attempt to peek an empty stack!" << endl; exit(1); } return stacklist[top]; } template void Operator::ClearStack() { top = -1; } template char* Operator::Convert(ofstream& outfile) { outfile.close(); ifstream infile("equation.txt"); assert(infile); int index = 0; int Length; int NumberTokens; char StTop[50]; bool first = true; char Token[50][50]; static char PostExpr[100]; bool Done = false; while (infile >> (Token[index])) { index++; } NumberTokens = index; for (int i = 0 ; i < NumberTokens ; i++) { Length = strlen(Token[i]); if (Token[i][0] == '(') Push(Token[i][0]); else if (isalnum(Token[i][0])) { Token[i][Length] = '\0'; strcpy(PostExpr,AppendStr(PostExpr, Token[i], Length, first)); first = false; } else if (Token[i][0] == ')') { StTop[0] = Pop(); StTop[1] = '\0'; while (StTop[0] !='(') { strcpy(PostExpr,AppendStr(PostExpr, StTop, 1, first)); StTop[0] = Pop(); StTop[1] = '\0'; } } else { while (!StackEmpty()&&!Done) { StTop[0] = Pop(); if (Prec(StTop[0],Token[i][0])) { StTop[1] = '\0'; strcpy(PostExpr,AppendStr(PostExpr, StTop, 1, first)); } else { Push(StTop[0]); Done = true; } } Push(Token[i][0]); Done = false; } } while (!StackEmpty()) { StTop[0] = Pop(); StTop[1] = '\0'; strcpy(PostExpr, AppendStr(PostExpr, StTop, 1, first)); } return PostExpr; } template char* Operator::AppendStr(char string[100], char Token[50], int length, bool first) { static int Location = 0; if (first) Location = 0; int i = 0; int NewLength = Location + length; for ( ; Location < NewLength ; Location++) { string[Location] = Token[i]; i++; } string[Location] = ' '; Location++; string[Location] = '\0'; return string; } template bool Operator::Prec(char First, char Second) { int PrecFirst, PrecSecond; if ((First == '(')||(First == ')')) PrecFirst = 3; else if ((Second == '(')||(Second == ')')) PrecSecond = 3; if ((First == '*')||(First == '/')||(First == '%')) PrecFirst = 1; if ((Second == '*')||(Second == '/')||(Second == '%')) PrecSecond = 1; if ((First == '+')||(First == '-')) PrecFirst = 2; if ((Second == '+')||(Second == '-')) PrecSecond = 2; if (PrecFirst <= PrecSecond) return true; else return false; } #endif