stack.cpp

来自「栈的应用—Polish Calculator」· C++ 代码 · 共 76 行

CPP
76
字号
#include "stack.h"



Error_code MyStack::push(const Stack_entry &item)

/*

Pre:  None.

Post: If the Stack is not full, item is added to the top

      of the Stack.  If the Stack is full,

      an Error_code of overflow is returned and the Stack is left unchanged.

*/

{

   Error_code outcome = success;

   if (count >= maxstack)

      outcome = overflow;

   else

      entry[count++] = item;

   return outcome;

}



Error_code MyStack::pop()

/*

Pre:  None.

Post: If the Stack is not empty, the top of

      the Stack is removed.  If the Stack

      is empty, an Error_code of underflow is returned.

*/



{

   Error_code outcome = success;

   if (count == 0)

      outcome = underflow;

   else --count;

   return outcome;

}



Error_code MyStack::top(Stack_entry &item) const

/*

Pre:  None.

Post: If the Stack is not empty, the top of

      the Stack is returned in item.  If the Stack

      is empty an Error_code of underflow is returned.

*/



{  

   Error_code outcome = success;

   if (count == 0)

      outcome = underflow;

   else

      item = entry[count - 1];

   return outcome;

}





bool MyStack::empty() const //can't modify data of class, can't call fuc without const

/*

Pre:  None.

Post: If the Stack is empty, true is returned.

      Otherwise false is returned.

*/



{

   bool outcome = true;

   if (count > 0) outcome = false;

   return outcome;

}





MyStack::MyStack()

/*

Pre:  None.

Post: The stack is initialized to be empty.

*/

{

   count = 0;

}



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?