示例#1
0
// 追加スタック数2
MyStack sortStack(MyStack stack) {
    MyStack ans(10), tmp(10);
    // 最小値を見つける
    while (!stack.isEmpty()) {
        int min = stack.peek();
        while (!stack.isEmpty()) {
            int t = stack.peek();
            stack.pop();
            if (t <= min) {
                min = t;
            }
            tmp.push(t);
        }

        // 最小値を入れる
        while (!tmp.isEmpty()) {
            if (tmp.peek() == min) {
                ans.push(tmp.peek());
            } else {
                stack.push(tmp.peek());
            }
            tmp.pop();
        }
    }
    return ans;
}
/* Function: sortTokenByStacks()
 * Usage: is called by formulaStringScanning() for single token;
 * -----------------------------------------------------------------------------------------//
 * Sort this token through the stacks. If token is number - push it to stackNumbers.
 * If token is valid operator token - process it due to Shunting-Yard conditions.
 *
 * @param token             Current token in formula string
 * @param stackNumbers      Stack of number values for current recursion
 * @param stackOperators    Stack of operators for current recursion */
void sortTokenByStacks(string token,
                       MyStack<double> &stackNumbers,
                       MyStack<string> &stackOperators) {
    if(stringIsDouble(token)) { //Token is number
        double num = stringToDouble(token);
        stackNumbers.push(num);//Just save token to stack
    } else { // Token is operator
        /* Main operators process */
        if(stackOperators.isEmpty()) { //Empty - push there without conditions
            stackOperators.push(token);
        } else { //If there are some operators in stack
            string topOper = stackOperators.peek();//Get top operator
            if(getOperPrecedence(topOper) < getOperPrecedence(token)) {
                /* Top operator precednce is
                 * weaker then this token operator - just save this token */
                stackOperators.push(token);
            } else {
                /* Top operator precednce is higher - evaluate two top numbers
                 * with top operator, and sort current token again */
                if(!failFlag) { //If there some fails - break this function
                    /* Main calculation for top numbers and top operator  */
                    twoNumsProcess(stackNumbers, stackOperators);
                    /* Call sorting again to process current token operator  */
                    sortTokenByStacks(token, stackNumbers, stackOperators);
                }
            }
        }
    }
}
示例#3
0
void Driver(MyStack<T> &listObject)
{
	T max;
	cout << "Welcome to MyStack , Lets Get you started \n\n ";
	cout << "Ok , Lets get one thing straight how big must the Stack be : ";
	cin >> max;
	cout << "\n\nThank you ... Ready Steady Go .. \n\n";
	//instruc();
	int choice;
	T value;
	
	listObject.setMax(max);
		do
		{
			instruc();
			cin >> choice;

			switch (choice)
			{
			case 1:
				cout << "Enter Your Value : ";
				cin >> value;
				listObject.push(value);
				system("cls");
				listObject.peek();
				break;
			case 2:

				listObject.pop(value);
				system("cls");
				listObject.peek();
				
				break;
			}


		} while (choice < 3);


}
示例#4
0
// 追加スタック数1
MyStack sortStack1(MyStack stack) {
    MyStack ans(10);
    while(!stack.isEmpty()) {
        int t = stack.peek(); stack.pop();
        /// 要素がansのtopより小さい間,ansからstackに戻す
        while (!ans.isEmpty() && ans.peek() < t) {
            stack.push(ans.peek());
            ans.pop();
        }
        ans.push(t);
    }

    return ans;
}
//Driver code
int main()
{
    MyStack s;
    s.push(3);
    s.push(5);
    s.getMin();
    s.push(2);
    s.push(1);
    s.getMin();
    s.pop();
    s.getMin();
    s.pop();
    s.peek();

    return 0;
}