Пример #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()
{
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	ArrayStack<int> s;
	//LinkedStack<int> s;

	const int N = 2;
	for (int i = 0; i < N; ++i)
		s.push(i + 1);

	for (int i = 0; i < N; ++i)
	{
		std::cout << s.peek() << std::endl;
		//s.pop();
	}

	ArrayStack<int> other = s;
	//LinkedStack<int> other = s;
	//other = s;

	s.makeEmpty();

	std::cout << (other.isEmpty() ? 0 : other.peek()) << std::endl;

	std::cout << (s.isEmpty() ? "YES" : "NO") << std::endl;

	return 0;
}
Пример #3
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
Пример #4
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());
}
Пример #5
0
void Test() {
    printf("Before:\n");
    ArrayStack<int> stack;
    stack.push(2);
    stack.push(5);
    stack.push(3);
    stack.push(1);
    stack.push(4);
    stack.push(1);
    for (int i = 0; i<=stack._top; i++)
        printf("%d ", stack._array[i]);
    printf("\n");
    Sort(stack);
    printf("After:\n");
    for (int i = 0; i<=stack._top; i++)
        printf("%d ", stack._array[i]);
    printf("\n");
}
Пример #6
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();
    }
}
Пример #7
0
/**
 * Obtains the expression from the user and enters it to the stack
 * 
 * @param pila ArrayStack which contains the numeric expression
 */
void getStackFromInput(ArrayStack<char> &pila) {
    string stringAux;
    char aux;
    cout << "Introduce a numeric expresion" << endl;
    getline (cin,stringAux);
    for(char aux: stringAux) {
        if (aux == '{' || aux == '}' || aux == '[' || aux == ']' || aux == '(' ||aux == ')') {
            pila.push(aux);
        }
    }
}
Пример #8
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();
}
Пример #9
0
int main() {
  ArrayStack<int> s;
  s.push(10);
  cout << s.peek() << endl;
  return 0;
}