myStack add(myStack numberOne,myStack numberTwo){ myStack result; int remainder = 0,numOneDigit,numTwoDigit; while (!numberOne.isEmpty() || !numberTwo.isEmpty()) { if (numberOne.isEmpty()) { numOneDigit = 0; } else { numOneDigit = numberOne.pop(); } if (numberTwo.isEmpty()) { numOneDigit = 0; } else { numTwoDigit = numberTwo.pop(); } remainder = (numOneDigit + numTwoDigit + remainder); result.push( remainder%10); remainder/=10; } if(remainder != 0){ result.push(remainder); } return result; }
/** * If token is operator. Compare current token with top sign * in stack, if sign in stack have bigger priority then put him in * queue, and token put in stack * @param operandStack - Stack of operators * @param postfixNotat - Queue. Postfix notation of formula * @param token - Token for user's string */ void addOperatorToPostfixNotation(myStack<string>& operandStack, Queue<string>& postfixNotation, string token) { if(operandStack.isEmpty()) { //if stack is empty then we add first sign to stack operandStack.push(token); } else if(!operandStack.isEmpty()) { if(getPriority(token[0])/* op1 */ <= getPriority((operandStack.peek())[0])/* op2 */) { //compare current token with top sign in stack, if sign in stack have bigger priority than put it in //queue, and token put in stack string z = operandStack.pop(); if(z != "(") { postfixNotation.enqueue(z); } operandStack.push(token); /** * if in stack we have more than 1 element than comparing * them and push some of them to queue. in this way we have * always one element in stack */ if(operandStack.size() >= 2) { if(!isOperator(postfixNotation.back()[0])) { string firstOperand = operandStack.pop(); string secondOperand = operandStack.pop(); if(getPriority(firstOperand[0]) <= getPriority(secondOperand[0])) { if(secondOperand != "(" & firstOperand != "(") { postfixNotation.enqueue(secondOperand); operandStack.push(firstOperand); } } else { if(secondOperand != "(" && firstOperand != "(") { postfixNotation.enqueue(firstOperand); operandStack.push(secondOperand); } } } } } else { operandStack.push(token); } } }