コード例 #1
0
ファイル: main.cpp プロジェクト: smurfolan/SDP-PCODE-REVIEW
void generate(myStack &number, int lengthOfNumber){
	srand(time(NULL));
	//Kristian: Истинате е, че тук нещата не са съвсем наред. Тук ще можеш да генерираш такива последователности : 0123, което принципно няма да е проблем кто сумираш, но от потребителска гледна точка е некоректно.
	for(int i = 0;i < lengthOfNumber;i++){
		number.push( rand() % 10 );
	}
}
コード例 #2
0
ファイル: main.cpp プロジェクト: smurfolan/SDP-PCODE-REVIEW
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;
}
コード例 #3
0
ファイル: calc.cpp プロジェクト: Maximbndrnk/cs-b-homework-1
/**
 * 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);
        }
    }
}