Exemple #1
0
void generate(myStack &number, int lengthOfNumber){
	srand(time(NULL));
	//Kristian: Истинате е, че тук нещата не са съвсем наред. Тук ще можеш да генерираш такива последователности : 0123, което принципно няма да е проблем кто сумираш, но от потребителска гледна точка е некоректно.
	for(int i = 0;i < lengthOfNumber;i++){
		number.push( rand() % 10 );
	}
}
Exemple #2
0
/**
 * 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);
        }
    }
}