示例#1
0
void transformToInt(char *prefix, int l)
{
  ArrayStack s;
  int id = 0;
  while (id < l)
  {
    char c = prefix[id];
    if (c == ' ')
    {
      id++;
      continue;
    }
    if (isOperator(c))
    {
      double b = s.pop();
      double a = s.pop();
      s.push(calc(a, b, c));
      id++;
    }
    else
    {
      int number = 0;
      c = prefix[id];
      while (isNumber(c))
      {
        number *= 10;
        number += (c - '0');
        id++;
        c = prefix[id];
      }
      s.push(number);
    }
  }
  printf("%d\n", s.pop());
}
示例#2
0
int main()
{
    int  num;       // the number to be converted
    string numStr;  // string used for input
    int  remainder; // remainder when num is divided by 2
    ArrayStack <int> stackOfRemainders; // remainders
    char response;           // user response
    do
    {
        cout << "Enter positive integer to convert: ";
        cin >> numStr;
        num = atoi(numStr.c_str());
        while (num > 0)
        {
            remainder = num % 2;
            stackOfRemainders.push(remainder);
            num /= 2;
        }

        cout << "Base-two representation: ";
        while ( !stackOfRemainders.isEmpty() )
        {
            remainder = stackOfRemainders.peek();
            stackOfRemainders.pop();
            cout << remainder;
        }
        cout << endl;
        cout << "\nMore (Y or N)? ";
        cin >> response;
    }
    while (response == 'Y' || response == 'y');

    return EXIT_SUCCESS;

}  // end main
示例#3
0
void Sort(ArrayStack<int> &stack) {
    ArrayStack<int> helper;
    while (!stack.empty()) {
        int cur = stack.pop();
        int count = 0;
        while (!helper.empty() && helper.peek() < cur) {
            count++;
            stack.push(helper.pop());
        }
        helper.push(cur);
        while (count--)
            helper.push(stack.pop());
    }

    while (!helper.empty())
        stack.push(helper.pop());
}
示例#4
0
/**
 * Checks if a sequence of characters have the correct order to do an arithmetic operation.
 * 
 * @param pila The stack that is going to be analyzed
 * @return true if only if the expression have the correct order
 */
bool comprovaExpresio (ArrayStack<char> &pila) {
    //If the stack is empty, it returns true because there isn't elements to analyze.
    if(!pila.empty()) {
        
        /**
         * If the stack is not empty, we proceed to do the next algorithm:
         * Two elements: pila (the original stack) and pilaAux2 (an auxiliary stack
         * to do the proper operations)
         * 1) If "pilaAux2" is empty, we transfer the last char of "pila" to "pilaAux2"
         *      and we delete this char from "pila". We go directly to step 4.
         *    If "pilaAux2" is not empty, we go to the step 2:
         * 2) We check if the top of pilaAux2 is equal to the opposite of the top
         *      element of "pila" (Note that we only check the closing expression).
         *      If it is true, we delete the char from "pilaAux2" and "pila" and
         *      we go directly to step 4. If it is not true, we go to the step 3.
         * 3) We push the top element of "pila" to "pilaAux". We go to the step 4.
         * 4) If pila is not empty, we do again the process deleting always the top
         *      element of "pila".
         * 
         * We can prove that the loop finishes. If the size of the stack is n and 
         * also is a positive integer, this implies that the stack has a size of n
         * that follows the next rule, 0 <= n.
         * If n = 0 we stop. If not,  we decrement the size of stack in 1 in each
         * loop, so we have a size of n-1. If n-1 = 0 pila is empty, and the algorithm
         * stops, if not, we do again the same process until we have size=0. QED
         * 
         * If "pilaAux2" is empty, implies that the expression is correct.
         * The complexity of this algorithm is O(n). Because we have O(n) (we create
         * the first stack) + O(n) (we check all elements of first stack) + O(1) 
         * (const senteces) = 2*O(n) + O(1) = O(n)
         */
        ArrayStack<char> pilaAux2;
        while(!pila.empty()) { //Checks if "pila" is empty
            
            if(pilaAux2.empty()) { //Checks if "pilaAux2" is empty
                pilaAux2.push(pila.top()); //Adds the top element of "pila" to "pilaAux2"
            }
            else {
                if(topAparellat(pila.top() , pilaAux2.top())) { //Checks if the tops elemets of the stacks are opposite
                    pilaAux2.pop(); //Deletes the last element of "pilaAux2"
                }
                else {
                    pilaAux2.push(pila.top()); //Adds the top element of "pila" to "pilaAux2"
                }
            }
            pila.pop(); //Deletes the top element of "pila"
        }
        if (pilaAux2.empty()) { //Checks if "pilaAux2" is empty to know if the expression is well done or not
            return true;
        } 
        else {
            return false;
        }
    }
    else {
        return true;
    }
}
示例#5
0
void array_stack(){
    ArrayStack<char,100> charStack;
    for(int i = 0;i<100;i++)
        charStack.push('a');
    while(!charStack.empty()){
        char tmp = charStack.top();
        charStack.pop();
    }
}
示例#6
0
void main(void)
{
    ArrayStack s;
    try{
                if(s.isEmpty()){
                                        cout<<"Stack is empty"<<endl;
                                }
                s.push(200);
                s.push(300);
                cout<<"Size of the stack is: "<<s.size()<<endl;
                cout<<"top is "<<s.Top()<<endl;
                s.pop();
                s.pop();
                cout<<s.isEmpty();
        }
    catch(...){
                cout<<"Some exception occured!!"<<endl;
               }
    getchar();
}