示例#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
文件: main.cpp 项目: krotovss/oop
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;
}