/* ---------------------------------------------------------------------------- function: myfloor_() author: Andrew Cave creation date: 08-Oct-1987 last modification: ##-###-#### arguments: none . description: See PostScript reference manual page 157. ---------------------------------------------------------------------------- */ Bool myfloor_(ps_context_t *pscontext) { int32 ssize ; OBJECT *theo ; UNUSED_PARAM(ps_context_t *, pscontext) ; ssize = theStackSize( operandstack ) ; if ( EmptyStack( ssize )) return error_handler( STACKUNDERFLOW ) ; theo = TopStack( operandstack , ssize ) ; if ( oType(*theo) == OREAL ) { int32 iarg ; SYSTEMVALUE arg ; arg = ( SYSTEMVALUE )oReal(*theo) ; if ( !intrange(arg) ) return TRUE ; iarg = ( int32 )arg ; if ( iarg <= 0 && arg < 0.0 && ( SYSTEMVALUE )iarg - arg != 0.0 ) --iarg ; oReal(*theo) = ( USERVALUE )iarg ; theLen(*theo) = 0 ; return TRUE ; } else if ( oType(*theo) == OINTEGER || oType(*theo) == OINFINITY ) return TRUE ; else return error_handler( TYPECHECK ) ; }
double Executor::run(const grammar::Program& program, runtime::Dispatcher& dispatcher, const expression::Context& context) { std::stack<double> stack; for (grammar::Program::const_iterator i = program.begin(); i != program.end(); ++i) { const std::string g = *i; assert(!g.empty()); if (g[0] == '@') { const std::string func = g.substr(1); int nb_args = dispatcher.nb_args(func); function::Arguments args(nb_args); for (function::Arguments::reverse_iterator j = args.rbegin(); j != args.rend(); ++j) { if (stack.empty()) throw EmptyStack(func); *j = stack.top(); stack.pop(); } stack.push(dispatcher.call(func, args)); } else if (g[0] == '#') { const std::string name = g.substr(1); expression::Context::const_iterator j = context.find(name); double value = j != context.end() ? j->second : 0.0; stack.push(value); } else { stack.push(fromString<double>(g)); } } assert(stack.size() == 1); double result = stack.top(); stack.pop(); return result; }
ItemType StackType::Top() // Returns a copy of the top item in the stack. // Pre: Stack has been initialized. // Post: If stack is empty, EmptyStack exception is thrown; // else a copy of the top element is returned. { if (IsEmpty()) throw EmptyStack(); else return topPtr->info; }
void StackType::Pop() // Removes top item from Stack and returns it in item. // Pre: Stack has been initialized. // Post: If stack is empty, EmptyStack exception is thrown; // else top element has been removed. { if (IsEmpty()) throw EmptyStack(); else { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } }
void InOrder_Non_Recursion(BiTree *bt) { Stack *S; BiTree *p; S = Initstack(); p = bt; while (p || !EmptyStack(S)) { while (p) { push(S, p); p = p->lchild; } pop(S, &p); printf("%c", p->data); p = p->rchild; } }
Bool string_(ps_context_t *pscontext) { register int32 ssize ; register OBJECT *theo ; UNUSED_PARAM(ps_context_t *, pscontext) ; ssize = theStackSize( operandstack ) ; if ( EmptyStack( ssize )) return error_handler( STACKUNDERFLOW ) ; theo = TopStack( operandstack , ssize ) ; if ( oType(*theo) != OINTEGER ) return error_handler( TYPECHECK ) ; ssize = oInteger(*theo) ; /* Setup string and initialise strings elements to zero. */ return ps_string(theo, NULL, ssize) ; }
/*---------------------------------------------------------------------------------------------- ${IActionHandler#Commit} ----------------------------------------------------------------------------------------------*/ STDMETHODIMP ActionHandler::Commit() { BEGIN_COM_METHOD; HRESULT hr = E_FAIL; HRESULT hrFinal = S_OK; if (m_vquact.Size()) { // JohnT 10/22/01: this can happen, typically if the only thing in the sequence is a // position mark. We could try to check for that but it doesn't seem worth it. Nothing // actually depends on having at least one sequence start here. //Assert(m_viSeqStart.Size() > 0); // Commit all actions. for (int i = 0; i <= m_iuactCurr; i++) { // If for some reason, we cannot commit an action that is part of a sequence, it's // just too bad. We can't undo a commited action so we might as well just amble on // and hope for the best. IgnoreHr(hr = m_vquact[i]->Commit()); if (FAILED(hr)) { hrFinal = hr; } } EmptyStack(); } #ifdef DEBUG_ACTION_HANDLER StrAnsi sta; sta.Format("Commit:"); sta.FormatAppend(" m_iCurrSeq=%d, m_viSeqStart=%d, m_iuactCurr=%d, m_vquact=%d, m_viMarks=%d, m_nDepth=%d\n", m_iCurrSeq, m_viSeqStart.Size(), m_iuactCurr, m_vquact.Size(), m_viMarks.Size(), m_nDepth); ::OutputDebugStringA(sta.Chars()); #endif//DEBUG_ACTION_HANDLER return hrFinal; END_COM_METHOD(g_factActh, IID_IActionHandler); }
main() { int i; int temp; int arr[8] = {49,38,65,97,76,13,27,49}; int pivotloc; SqStack sta;//初始化栈 InitStack(&sta); Data data={0,7}; Data get; Push(&sta,(Elem)data);//0,7进栈 while(EMPTY!=EmptyStack(&sta)) { GetTop(&sta,(pElem)(&get)); Pop(&sta); pivotloc = Partition(arr,get.low,get.high); temp = get.high; get.high = pivotloc-1; if(get.low<get.high) { Push(&sta,(Elem)get); } get.low = pivotloc+1; get.high = temp; if(get.low<get.high) { Push(&sta,(Elem)get); } } for(i=0;i<8;i++) { printf("%d ",arr[i]); } printf("\n"); }
/* ---------------------------------------------------------------------------- function: mysqrt_() author: Andrew Cave creation date: 08-Oct-1987 last modification: ##-###-#### arguments: none . description: See PostScript reference manual page 224. ---------------------------------------------------------------------------- */ Bool mysqrt_(ps_context_t *pscontext) { register int32 ssize ; register OBJECT *theo ; register SYSTEMVALUE arg ; UNUSED_PARAM(ps_context_t *, pscontext) ; ssize = theStackSize( operandstack ) ; if ( EmptyStack( ssize )) return error_handler( STACKUNDERFLOW ) ; theo = TopStack( operandstack , ssize ) ; /* Do NOT use object_get_numeric, because of different behaviour with OINFINITY. */ switch ( oType(*theo)) { case OREAL : arg = ( SYSTEMVALUE )oReal(*theo) ; break ; case OINTEGER : arg = ( SYSTEMVALUE )oInteger(*theo) ; break ; case OINFINITY : return TRUE ; default: return error_handler( TYPECHECK ) ; } /* Check for validity of argument. */ if ( arg < 0.0 ) return error_handler( RANGECHECK ) ; theTags(*theo) = OREAL | LITERAL ; oReal(*theo) = ( USERVALUE )sqrt(( double )arg ) ; theLen(*theo) = 0 ; return TRUE ; }
symbol StackType::Top() const { if(IsEmpty()) throw EmptyStack(); return items[top]; }
void StackType::Pop() { if(IsEmpty()) throw EmptyStack(); top--; }
ItemType StackType::Top() { if (IsEmpty()) throw EmptyStack(); return items[top]; }
/*---------------------------------------------------------------------------------------------- ${IActionHandler#Redo} ----------------------------------------------------------------------------------------------*/ STDMETHODIMP ActionHandler::Redo(UndoResult * pures) { BEGIN_COM_METHOD; ChkComArgPtr(pures); *pures = kuresSuccess; // Do not allow actions to be redone if they have not been undone. Assert(m_viSeqStart.Size() > 0); Assert(m_iCurrSeq <= m_viSeqStart.Size() - 1); m_fCanContinueTask = false; // Can't 'continue' the previous task from before the Undo. m_fUndoOrRedoInProgress = true; HRESULT hrFinal = S_OK; int iSeqToRedo = m_iCurrSeq + 1; try // to ensure in progress gets cleared. { int handle; // Start a 'transaction' if possible to group together all the stuff we're going to Undo. if (m_qundg) { m_qundg->BeginGroup(&handle); } // Determine the last action to be redone; The last action to redo is the action // before the next redo sequence. Or the last existing action if there is not a // redo sequence after the sequence we are about to redo. int iLastRedoAct = m_vquact.Size() - 1; if (iSeqToRedo <= m_viSeqStart.Size() - 2) iLastRedoAct = m_viSeqStart[iSeqToRedo + 1] - 1; // Loop through all of the actions that are about to be redone and see if any of them // require a refresh. for (int i = m_viSeqStart[iSeqToRedo]; i <= iLastRedoAct; i++) { CheckHr(m_vquact[i]->put_SuppressNotification(true)); } // Redo all the actions in the next action sequence that change data. hrFinal = CallRedo(pures, true, iSeqToRedo, iLastRedoAct); // Now do all the PropChangeds for (int i = m_viSeqStart[iSeqToRedo]; i <= iLastRedoAct; i++) { CheckHr(m_vquact[i]->put_SuppressNotification(false)); } // REVIEW (TimS/EberhardB): What do we do if the call to CallRedo above failed somewhere? // Should we do this at all? Or just until the failed action? Or everything? UndoResult uresTmp = kuresSuccess; // Redo all the actions in the next action sequence that don't change data. HRESULT hr; IgnoreHr(hr = CallRedo(&uresTmp, false, iSeqToRedo, iLastRedoAct)); if (FAILED(hr)) { hrFinal = hr; *pures = uresTmp; } else if (uresTmp == kuresFailed) { *pures = uresTmp; } // dlh testing if (*pures == kuresError || * pures == kuresFailed) { if (m_qundg) m_qundg->CancelGroup(handle); EmptyStack(); } else // normal success case { // Set the current sequence and action. m_iCurrSeq++; m_iuactCurr = m_vquact.Size() - 1; if (m_iCurrSeq < m_viSeqStart.Size() - 1) { m_iuactCurr = m_viSeqStart[m_iCurrSeq + 1] - 1; } if (m_qundg) m_qundg->EndGroup(handle); } #ifdef DEBUG_ACTION_HANDLER StrAnsi sta; sta.Format("Redo:"); sta.FormatAppend(" m_iCurrSeq=%d, m_viSeqStart=%d, m_iuactCurr=%d, m_vquact=%d, m_viMarks=%d, m_nDepth=%d\n", m_iCurrSeq, m_viSeqStart.Size(), m_iuactCurr, m_vquact.Size(), m_viMarks.Size(), m_nDepth); ::OutputDebugStringA(sta.Chars()); #endif //DEBUG_ACTION_HANDLER } catch(...) { m_fUndoOrRedoInProgress = false; throw; } m_fUndoOrRedoInProgress = false; return hrFinal; END_COM_METHOD(g_factActh, IID_IActionHandler); }
/*---------------------------------------------------------------------------------------------- ${IActionHandler#Undo} ----------------------------------------------------------------------------------------------*/ STDMETHODIMP ActionHandler::Undo(UndoResult * pures) { BEGIN_COM_METHOD; ChkComArgPtr(pures); *pures = kuresSuccess; Assert(m_iCurrSeq >= 0); Assert(m_vquact.Size() > 0); HRESULT hr = E_FAIL; HRESULT hrFinal = S_OK; m_fCanContinueTask = false; // Can't 'continue' the previous task from before the Undo. m_fUndoOrRedoInProgress = true; try // to ensure in progress gets cleared. { int handle; // Start a 'transaction' if possible to group together all the stuff we're going to Undo. if (m_qundg) { m_qundg->BeginGroup(&handle); } // Loop through all of the actions that are about to be undone and see if any of them // require a refresh. Vector<long> vhvoCreatedObjects; for (int i = m_iuactCurr; (m_iCurrSeq >= 0) && (i >= m_viSeqStart[m_iCurrSeq]); i--) { CheckHr(m_vquact[i]->put_SuppressNotification(true)); } // Undo all actions from the current action back to the first action of the current action // sequence. bool fRedoable = true; IgnoreHr(hrFinal = CallUndo(pures, fRedoable, true)); // Now do all the PropChangeds for (int i = m_iuactCurr; (m_iCurrSeq >= 0) && (i >= m_viSeqStart[m_iCurrSeq]); i--) { CheckHr(m_vquact[i]->put_SuppressNotification(false)); } // REVIEW (TimS/EberhardB): What do we do if the call to CallUndo above failed somewhere? // Should we do this at all? Or just until the failed action? Or everything? UndoResult uresTmp; IgnoreHr(hr = CallUndo(&uresTmp, fRedoable, false)); if (FAILED(hr)) { hrFinal = hr; *pures = uresTmp; } else if (uresTmp == kuresFailed) { *pures = uresTmp; } // dlh testing if (*pures == kuresError || * pures == kuresFailed) { if (m_qundg) m_qundg->CancelGroup(handle); EmptyStack(); } else // normal success case { // Set the current action and start of the current action sequence. if (m_iCurrSeq >= 0) { m_iuactCurr = m_viSeqStart[m_iCurrSeq] - 1; m_iCurrSeq = m_iCurrSeq - 1; } if (m_qundg) m_qundg->EndGroup(handle); } // Delete any marks that now point after current undo action CleanUpMarks(); if (!fRedoable) CleanUpRedoActions(true); #ifdef DEBUG_ACTION_HANDLER StrAnsi sta; sta.Format("Undo:"); sta.FormatAppend(" m_iCurrSeq=%d, m_viSeqStart=%d, m_iuactCurr=%d, m_vquact=%d, m_viMarks=%d, m_nDepth=%d\n", m_iCurrSeq, m_viSeqStart.Size(), m_iuactCurr, m_vquact.Size(), m_viMarks.Size(), m_nDepth); ::OutputDebugStringA(sta.Chars()); #endif//DEBUG_ACTION_HANDLER } catch(...) { CleanUpMarks(); m_fUndoOrRedoInProgress = false; throw; } m_fUndoOrRedoInProgress = false; return hrFinal; END_COM_METHOD(g_factActh, IID_IActionHandler); }
//输入表达式并计算结果 double CalculateExp(void) { double result, tempNum1, tempNum2; double data = 0, expn; char ch, topSign, point = 'n', num = 'n'; OPTR *sign; OPRD *number; InitStack(OPTR, &sign); InitStack(OPRD, &number); PushStack(sign, '#'); printf("请输入表达式:"); ch = getchar(); GetStackTop(sign, &topSign); while(ch != '#' || topSign != '#'){ if ('0' <= ch && ch <= '9' || ch == '.'){ if (ch == '.' && point == 'y'){ printf("表达式输入有误!\n"); exit(-1); } else if (ch == '.' && point == 'n'){ point = 'y'; expn = 0.1; } else{ if (point == 'y'){ data = data + expn * (ch - '0'); expn *= 0.1; } else{ data = data * 10 + (ch - '0'); } num = 'y'; } ch = getchar(); } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')' || ch == '#'){ if (num == 'y'){ PushStack(number, data); num = 'n'; point = 'n'; data = 0; } GetStackTop(sign, &topSign); switch(compare(ch, topSign)){ case '<': //扫描运算符优先级小于栈顶元素 PopStack(sign, &topSign); PopStack(number, &tempNum1); PopStack(number, &tempNum2); switch(topSign){ case '+': result = tempNum1 + tempNum2; break; case '-': result = tempNum1 - tempNum2; break; case '*': result = tempNum1 * tempNum2; break; case '/': result = tempNum2 / tempNum1; break; } PushStack(number, result); break; case '>': //扫描运算符优先级大于栈顶元素 PushStack(sign, ch); ch = getchar(); break; case '=': //扫描运算符为右括号,匹配到了左括号 PopStack(sign, &topSign); ch = getchar(); break; } } else if (ch == '\n'){ ch = '#'; } else{ printf("输入的表达式有误!\n"); exit(-1); } GetStackTop(sign, &topSign); } PopStack(number, &result); //将结果从栈中取出来 if (!EmptyStack(number)){ //如果取出后栈不为空则表示输入的表达式不正确 printf("表达式有误!\n"); exit(-1); } return result; }
void ResetNeighbourUpdates(NeighbourUpdate* neighbourUpdate) { EmptyStack(neighbourUpdate->m_Pointers); EmptyStack(neighbourUpdate->m_Old); }