// Stephen Bassoli // CSE 124 Section 01 // Lab 3 #include #include #include #include #include #include "stack.h" /*bool Operator::StackFull()const { return (top == MaxStackSize-1); } Operator::Operator() { top = -1; } void Operator::Push(const char item) { if (StackFull()) { cerr << "Stack Overflow!\n"; exit(1); } top++; stacklist[top] = item; } bool Operator::StackEmpty()const { return (top == -1); } char Operator::Pop() { char temp; if (StackEmpty()) { cerr << "Attempt to pop an empty stack!" << endl; exit(1); } temp = stacklist[top]; top--; return temp; } char Operator::Peek() { if (StackEmpty()) { cerr << "Attempt to peek an empty stack!" << endl; exit(1); } return stacklist[top]; } 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]; 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)); } else if (Token[i][0] == ')') { StTop[0] = Pop(); StTop[1] = '\0'; while (StTop[0] !='(') //CAN CAUSE PROBLEM { strcpy(PostExpr, AppendStr(PostExpr, StTop, 1)); 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)); } 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)); } return PostExpr; } template char* Operator::AppendStr(char string[100], char Token[50], int length) { static int 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; } */ /* template bool Operand::StackFull()const { return (top == MaxStackSize-1); } template Operand::Operand() { top = -1; } template void Operand::Push(const T& item) { if (StackFull()) { cerr << "Stack Overflow!\n"; exit(1); } top++; strcpy(stacklist[top], item); } template bool Operand::StackEmpty()const { return (top == -1); } template T Operand::Pop() { T temp; if (StackEmpty()) { cerr << "Attempt to pop an empty stack!" << endl; exit(1); } temp = stacklist[top]; top--; return temp; } template T Operand::Peek() { if (StackEmpty()) { cerr << "Attempt to peek an empty stack!" << endl; exit(1); } return stacklist[top]; } template void Operand::ClearStack() { top = -1; }*/