//--------------------------------------------------------------------------- // @function: // CStackTest::EresUnittest_Pop // // @doc: // This test should assert. // //--------------------------------------------------------------------------- GPOS_RESULT CStackTest::EresUnittest_Pop() { // create memory pool CAutoMemoryPool amp; IMemoryPool *pmp = amp.Pmp(); ULONG rgsz[] = {1, 2, 3, 4}; CStack<ULONG> *pstk = GPOS_NEW(pmp) CStack<ULONG> (pmp, 4); CAutoP<CStack<ULONG> > cAP; cAP = pstk; // add elements incl trigger resize of array for (ULONG i = 0; i < 4; i++) { pstk->Push(&rgsz[i]); GPOS_ASSERT( (* pstk->Peek()) == 4 - i); GPOS_ASSERT(pstk->Pop() == &rgsz[i]); } // now deliberately pop when the stack is empty pstk->Pop(); return GPOS_FAILED; }
GPOS_RESULT CStackTest::EresUnittest_PushPop() { // create memory pool CAutoMemoryPool amp; IMemoryPool *pmp = amp.Pmp(); ULONG rgul[4] = {1,2,3,4}; CStack<ULONG> *pstk = GPOS_NEW(pmp) CStack<ULONG> (pmp, 4); // scope for auto trace { CAutoTrace trace(pmp); IOstream &oss(trace.Os()); oss << "Pushing " << rgul[0] << " and " << rgul[1] << std::endl; pstk->Push(&rgul[0]); // stack is 1 pstk->Push(&rgul[1]); // stack is 1,2 ULONG ul = *(pstk->Peek()); // ul should be 2 oss << "Top of stack is " << ul << std::endl; GPOS_ASSERT(ul == rgul[1]); oss << "Popping stack" << std::endl; ul = *(pstk->Pop()); //ul should be 2 oss << "Popped element is " << ul << std::endl; GPOS_ASSERT(ul == rgul[1]); oss << "Pushing " << rgul[2] << std::endl; pstk->Push(&rgul[2]); // stack is 1,3 ul = *(pstk->Peek()); // ul is 3 oss << "Top of stack is " << ul << std::endl; GPOS_ASSERT(ul == rgul[2]); oss << "Popping stack" << std::endl; ul = *(pstk->Pop()); // ul is 3 oss << "Popped element is " << ul << std::endl; GPOS_ASSERT(ul == rgul[2]); oss << "Popping stack " << std::endl; ul = *(pstk->Pop()); // ul is 1 oss << "Popped element is " << ul << std::endl; GPOS_ASSERT(ul == rgul[0]); } GPOS_DELETE(pstk); return GPOS_OK; }
int main(void) { int iNum = 2196; int iD = 16; int iLen = 10; cout<<iNum<<"的"<<iD<<"进制数为: "<<Conversion(iNum, iD)<<endl; CStack<float> cfs; for(int i=0; i<iLen; i++) cfs.Push(i*3.02+2.08); cfs.Display(); cout<<"=====================Hanoi move game======================"<<endl; Hanoi(5, 'x', 'y', 'z'); CStack<float> cft( cfs ); cout<<"复制构造:"<<endl; cft.Display(); cft.Clear(); cft.Push( 3.9 ); cft.Display(); cfs.Display(); cout<<"赋值运算符:"<<endl; CStack<float> cfb = cfs; cfb.Display(); cout<<"cfb pop: "<<cfb.Pop()<<endl; cfb.Display(); cfb = cfs; cfb.Display(); return 0; }
//请写一个栈的模板类,要求栈的长度可以动态改变。使用如下代码进行测试: int main() { //调用普通模版 int i; CStack<int> stInt; for (i = 1; i <= 20; i++) { stInt.Push(i); } cout << "调用CStack<int>普通模版, 元素个数为: " << stInt.Size() << endl; while (stInt.IsNotEmpty()) { int iData; stInt.Pop(iData); cout << iData << " "; } cout << endl << endl; ///* //调用特化模版 CStack<char *> stCharP; for (i = 1; i <= 15; i++) { char sz[10]; sprintf(sz, "s%d", i); stCharP.Push(sz); } cout << "调用CStack<char *>特化模版, 元素个数为: " << stCharP.Size() << endl; while (stCharP.IsNotEmpty()) { char * pData; stCharP.Pop(pData); cout << pData << " "; } cout << endl << endl; //*/ //返回 system("pause"); return 0; }
//--------------------------------------------------------------------------- // @function: // CStackTest::EresUnittest_Basic // // @doc: // Basic stack test // //--------------------------------------------------------------------------- GPOS_RESULT CStackTest::EresUnittest_Basic() { // create memory pool CAutoMemoryPool amp; IMemoryPool *pmp = amp.Pmp(); // test with CHAR array CHAR rgsz[][9] = {"abc", "def", "ghi", "qwe", "wer", "wert", "dfg", "xcv", "zxc"}; CStack<CHAR> *pstk = GPOS_NEW(pmp) CStack<CHAR> (pmp, 4); // add elements incl trigger resize of array for (ULONG i = 0; i < 9; i++) { pstk->Push(rgsz[i]); } ULONG idx = 0; while (!pstk->FEmpty()) { GPOS_ASSERT(idx <= 8 && "Index out of range. Problem with FEmpty."); #ifdef GPOS_DEBUG GPOS_ASSERT(pstk->Pop() == rgsz[8 - idx] && "Incorrect pop value"); #else pstk->Pop(); #endif idx++; } GPOS_ASSERT(idx == 9 && "Stack is not empty!"); GPOS_DELETE(pstk); return GPOS_OK; }
int _tmain(int argc, _TCHAR* argv[]) { CStack st; int list[5]={1,2,3,4,5}; for(int i=0;i<5;i++) { st.Push(list[i]); } for(int i=0;i<5;i++) { cout <<st.Pop()<<endl; } for(int i=0;i<5;i++) { st.Push(list[i]); } st.Clear(); if(st.IsEmpty()) cout<<"stack is empty!"; return 0; }
/* 利用栈求解数制转换问题,这里求解10进制数p的d进制数。*/ string Conversion(int p, int d) { CStack<int> csiMem; int iN = p; while( iN ) { csiMem.Push( iN % d); iN /= d; } string sRes; while( !csiMem.IsEmpty() ) { int iCur = csiMem.Pop(); char c = ' '; if( iCur >= 10) c = 'A'+iCur-10; if( c == ' ') { char cNum[5] = ""; sprintf( cNum, "%d", iCur ); sRes += cNum; } else sRes += c; } return sRes; }
BOOL CMatrixDoc::GetResult(const CString &data,CArrayMatrix & matrix) { //在这可以通过修改data字符串和m_VariablePtrList变量链表来实现 //具体方法是首先假如data是包含多个运算符的 CString sDataString=data; sDataString.TrimLeft("\n"); sDataString.TrimRight("\n"); CString *pVar; CArrayMatrix result; bool ok; int VarNum=GetVariableNum(sDataString,pVar); if(VarNum==0) return FALSE; CTypedPtrList<CObList,CArrayMatrix *> tpVarList; if(!StringsToMatrixs(pVar,tpVarList,VarNum)) { if(pVar!=NULL) delete []pVar; return FALSE; } TurnString(sDataString); //表达式求值 { sDataString=sDataString+"#"; CStack<TCHAR> OPTR; CStack<CArrayMatrix *> OPND; OPTR.Push('#'); int index=0; TCHAR c=sDataString[index]; int nVarIndex=0; while(c!=TCHAR('#')||OPTR.GetTop()!=TCHAR('#')) { if(!IsOperator(c)) { POSITION pos=tpVarList.GetHeadPosition(); CArrayMatrix * pt=NULL; for(int i=0;i<=nVarIndex&&i<tpVarList.GetCount();i++) { pt=tpVarList.GetAt(pos); tpVarList.GetNext(pos); } if(pt==NULL) return FALSE; OPND.Push(pt); nVarIndex++; index++; c=sDataString[index]; } else { switch(Precede(OPTR.GetTop(),c)) { case -1: { OPTR.Push(c); index++; c=sDataString[index]; break; } case 0: { TCHAR x; OPTR.Pop(x); index++; c=sDataString[index]; break; } case 1: { TCHAR theta; OPTR.Pop(theta); CArrayMatrix * b=NULL; CArrayMatrix * a=NULL; OPND.Pop(b); OPND.Pop(a); OPND.Push(Operate(a,theta,b,ok)); break; } } } } result=*(OPND.GetTop()); } //销毁tpvarlist变量里面的东西 { POSITION pos=tpVarList.GetHeadPosition(); int len=tpVarList.GetCount(); for(int i=0;i<len;i++) { CArrayMatrix * tp=tpVarList.GetAt(pos); delete tp; tpVarList.GetNext(pos); } tpVarList.RemoveAll(); } if(pVar!=NULL) delete []pVar; if(!ok) return FALSE; matrix=result; return TRUE; }