Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "CStrTokenizer.h"
- #include "Stack.cpp"
- int operatorPrecedence(string); //function prototypes
- string convertToPostfix(string);
- /********
- Convert an infix expression to reverse polish notation.
- * You do not have to have the program evaluate the reverse polish notation
- * Variable names should be able to be used in the expression
- We want to make a new .cpp rather than adding to an old .cpp for reusability.
- I will be heavily documenting this code to better understand template classes (hopefully) this time around.
- ********/
- //input string infix. Function that will convert infix to rpn
- string convertToPostfix(string infix)
- {
- string postfix; //output string. we're going to keep adding to this as the program goes on. Eventually returning this.
- bool unary = true; //a unary flag set to true. Otherwise the compiler will say 'huh'
- //create an empty stack of string. create the stack itself that is a string.
- Stack<string> *newStack = new Stack<string>();
- CStrTokenizer cs = CStrTokenizer(infix, "()^+-/%* ", true); //creating an object of type CStringTokenizer
- //used to tokenize the infix string^^ with the delimiters "()^+-/%* " (space included) and set flag for including delimiter to true.
- //token is holding the value. Run some checks using loops to determine what it is and what to do with it.
- //5. while our CStringTokenizer object has more tokens. Checking if there are more tokens.
- while (cs.hasMoreTokens() )
- {
- //string token;
- //token.getNextToken();
- //The code above does not make sense because the string class does not have a getNextToken function
- //our new object "cs" has access to the getNextToken function.
- string token = cs.getNextToken(); //create a token string holding the next token with any trailing whitespaces trimmed
- //if token is equal to "" do nothing. Is this supposed to be a space?
- if (token == "")
- {
- //do nothing
- }
- else
- {
- //postfix += (" |" + token + "|");
- //any operator after an operand is binary, not unary
- unary = false;
- }
- //C.
- //if token is a ( then push the token onto the stack
- if (token == "(")
- {
- newStack->push(token);
- }
- //ii. if token is a ) then create a string holding the operator character
- else if (token == ")")
- {
- //a. string to hold the operator
- string op;
- //while operator popped is not equal to "(" perform the operation to postfix
- while ((op = newStack->pop()) != "(")
- {
- postfix += (" " + op);
- }
- }
- //C.iii
- else if (token == "*" || token == "^" || token == "+" || token == "-" || token == "%" ||token == "/" )
- {
- if (unary == true)
- {
- token = "u" + token;
- newStack->push(token);
- }
- }
- //iv else
- else
- {
- //create an integer variable, p, holding the operatorPrecedence(token).
- //but you can't convert strings to ints directly.
- int p = operatorPrecedence(token);
- //p is the operator precendence of an operator
- //while the stack isn't empty, while the peek is not "(" and while operator precedence value is greater than or equal to p
- while ((!(newStack->isEmpty()) && (newStack->peek() != "(")) && (operatorPrecedence(newStack->peek()) >= p))
- {
- //create a string holding the operator with the return value of stack.pop();
- //Is this correct???
- string ops = newStack->pop();
- postfix += " " + ops;
- } newStack->push(token);
- //push token onto stack
- //v. if an operator is after this one, it has to be unary.
- }
- unary = true;
- }
- //6. while the newStack isn't empty
- while (!newStack->isEmpty())
- {
- //create a string holding the operator with the return value of stack.pop();
- string opers = newStack->pop();
- //
- postfix += " " + opers;
- }
- return postfix;
- }
- //operatorPrecedence function
- int operatorPrecedence(string token)
- {
- if (token == "u-" || token == "u+")
- {
- //highest level operator precedence
- return 3;
- }
- else if(token == "^")
- {
- //if the operator is exponent, it's second highest precedence.
- return 2;
- }
- else if(token == "*"||token== "/"||token== "%")
- {
- //if the operator is multiply, divide, or modulus, it's third highest precedence.
- return 1;
- }
- else if(token == "-" || token == "+")
- {
- //if the operator is + or -, it's lowest highest precedence.
- return 0;
- }
- else
- {
- //if none of the operands seem to math those above, return 0 anyway. No precedence.
- return 0;
- }
- }
- int main()
- {
- string str; //new variable to hold a string
- cout << "\n Enter an infix expression to convert: "; //prompt the user for a string.
- getline (cin, str); //string that the user inputs
- string result = convertToPostfix(str); //put the string into the convertToPostFix function to get the result.
- cout << "\nThis is your result" << result << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment