public static string toRPN(string infixString) { if (!CheckInput.checkInputString(infixString)) { return(""); } Stack <char> operationStack = new Stack <char>(); //Stack for all operations char lastOperation; string resultString = ""; //String with result infixString = infixString.Replace(" ", ""); for (int i = 0; i < infixString.Length; i++) { //If it is number, than add to resultString if (SignChecker.isVariable(infixString[i])) { resultString += infixString[i]; } else if (SignChecker.isOperation(infixString[i])) { if (infixString[i] == '-' && (((i == 0) || (infixString[i - 1] == '(')) && (SignChecker.isVariable(infixString[i + 1])))) { resultString += 0; } if (operationStack.Count != 0) { lastOperation = operationStack.Peek(); if (SignChecker.getOperationPriority(lastOperation) < SignChecker.getOperationPriority(infixString[i])) { operationStack.Push(infixString[i]); resultString += " "; } else { resultString += operationStack.Pop(); operationStack.Push(infixString[i]); resultString += " "; } } else { resultString += " "; operationStack.Push(infixString[i]); } } //If current symbol is (, then add it to stack else if (infixString[i] == '(') { operationStack.Push(infixString[i]); } //If current symbol is ), then return all symbols from stack, until ( else if (infixString[i] == ')') { while (operationStack.Peek() != '(') { resultString += operationStack.Pop(); } operationStack.Pop(); } } //In the end, return all the values from stack while (operationStack.Count != 0) { resultString += operationStack.Pop() + " "; } return(resultString); }
public static string toRPN(string infixString) { infixString = infixString.Replace(" ", ""); //Replacing all spaces if (!CheckInput.checkInputString(infixString)) //If we have wrong input string { throw new ArgumentException(); //Than throw exception } Stack <char> operationStack = new Stack <char>(); //Stack for all operations char lastOperation; //Keeping last operation string resultString = ""; //String with result for (int i = 0; i < infixString.Length; i++) { //If it is number, than add to resultString if (SignChecker.isVariable(infixString[i])) { resultString += infixString[i]; } //If it is operation else if (SignChecker.isOperation(infixString[i])) { //If it is unary minus< then add 0 to result string if (infixString[i] == '-' && (((i == 0) || (infixString[i - 1] == '(')) && (SignChecker.isVariable(infixString[i + 1])))) { resultString += 0; } //If we have operations in stack if (operationStack.Count != 0) { //Taking last operation of stack lastOperation = operationStack.Peek(); //If priority of operation from stack is higher, then pirority of current operation if (SignChecker.getOperationPriority(lastOperation) < SignChecker.getOperationPriority(infixString[i])) { //Add current operation to stack operationStack.Push(infixString[i]); resultString += " "; } else { //Add operation from stack to result string resultString += operationStack.Pop(); //Add current operation to stack operationStack.Push(infixString[i]); resultString += " "; } } else { //Add current operation to stack resultString += " "; operationStack.Push(infixString[i]); } } //If current symbol is (, then add it to stack else if (infixString[i] == '(') { operationStack.Push(infixString[i]); } //If current symbol is ), then return all symbols from stack, until ( else if (infixString[i] == ')') { while (operationStack.Peek() != '(') { resultString += operationStack.Pop(); } operationStack.Pop(); } } //In the end, return all the values from stack while (operationStack.Count != 0) { resultString += operationStack.Pop() + " "; } return(resultString); }