static void PlayMenuSong (void) { int h, i; CStack<char*> m (MAX_NUM_SONGS + 2); CFile cf; char szSongTitles [2][14] = {"- Descent 2 -", "- Descent 1 -"}; CListBox lb; m.Push (szSongTitles [0]); for (i = 0; i < songManager.TotalCount (); i++) { if (cf.Open (reinterpret_cast<char*> (songManager.SongData (i).filename), gameFolders.szDataDir, "rb", i >= songManager.Count (0))) { cf.Close (); if (i == songManager.Count (0)) m.Push (szSongTitles [1]); m.Push (songManager.SongData (i).filename); } } for (;;) { h = lb.ListBox (TXT_SELECT_SONG, m); if (h < 0) return; if (!strstr (m [h], ".hmp")) continue; for (i = 0; i < songManager.TotalCount (); i++) if (songManager.SongData (i).filename == m [h]) { songManager.Play (i, 0); return; } } }
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; }
static void PlayMenuMovie (void) { int h, i, j; CStack<char*> m; char* ps; CListBox lb; i = movieManager.m_nLibs; for (h = j = 0; j < i; j++) if (j != 2) //skip robot movies h += movieManager.m_libs [j].m_nMovies; if (!h) return; if (!m.Create (h)) return; for (i = j = 0; i < h; i++) if ((ps = movieManager.Cycle (i == 0, 0))) { if (j && !strcmp (ps, m [0])) break; m.Push (ps); } i = lb.ListBox (TXT_SELECT_MOVIE, m); if (i > -1) { SDL_ShowCursor (0); if (strstr (m [i], "intro")) subTitles.Init ("intro.tex"); else if (strstr (m [i], ENDMOVIE)) subTitles.Init (ENDMOVIE ".tex"); movieManager.Play (m [i], 1, 1, gameOpts->movies.bResize); subTitles.Close (); SDL_ShowCursor (1); } songManager.PlayCurrent (1); }
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; }
//--------------------------------------------------------------------------- // @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; }
int main() { CStack* myStack = new CStack(); cout << "myStack->Top(): " << myStack->Top() << endl; Node* NodeA = new Node(); NodeA->data = 99; myStack->Push(NodeA); Node* NodeB = myStack->Top(); cout << "myStack->Top(): " << NodeB->data << endl; }
//请写一个栈的模板类,要求栈的长度可以动态改变。使用如下代码进行测试: 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; }
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; }
int SelectAndLoadMission (int bMulti, int *bAnarchyOnly) { int i, j, nMissions, nDefaultMission, nNewMission = -1; CStack<char*> msnNames (MAX_MISSIONS); CListBox lb; static const char *menuTitles [4]; menuTitles [0] = TXT_NEW_GAME; menuTitles [1] = TXT_NEW_D1GAME; menuTitles [2] = TXT_NEW_D2GAME; menuTitles [3] = TXT_NEW_GAME; if (bAnarchyOnly) *bAnarchyOnly = 0; do { msnNames.Reset (); nMissions = BuildMissionList (1, nNewMission); if (nMissions < 1) return -1; nDefaultMission = 0; for (i = 0; i < nMissions; i++) { msnNames.Push (gameData.missions.list [i].szMissionName); j = MsnHasGameVer (msnNames [i]) ? 4 : 0; if (!stricmp (msnNames [i] + j, gameConfig.szLastMission)) nDefaultMission = i; } gameStates.app.nExtGameStatus = bMulti ? GAMESTAT_START_MULTIPLAYER_MISSION : GAMESTAT_SELECT_MISSION; gameOpts->app.nVersionFilter = NMCLAMP (gameOpts->app.nVersionFilter, 1, 3); nNewMission = lb.ListBox (bMulti ? TXT_MULTI_MISSION : menuTitles [gameOpts->app.nVersionFilter], msnNames, nDefaultMission); GameFlushInputs (); if (nNewMission == -1) return -1; //abort! } while (!gameData.missions.list [nNewMission].nDescentVersion); strcpy (gameConfig.szLastMission, msnNames [nNewMission]); if (!LoadMission (nNewMission)) { MsgBox (NULL, NULL, 1, TXT_OK, TXT_MISSION_ERROR); return -1; } gameStates.app.bD1Mission = (gameData.missions.list [nNewMission].nDescentVersion == 1); gameData.missions.nLastMission = nNewMission; if (bAnarchyOnly) *bAnarchyOnly = gameData.missions.list [nNewMission].bAnarchyOnly; return nNewMission; }
/* 利用栈求解数制转换问题,这里求解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; }
//--------------------------------------------------------------------------- // @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; }
void LegacyNewGameMenu (void) { int nNewLevel, nHighestPlayerLevel; int nMissions; CStack<char*> m (MAX_MISSIONS); int i, choice = 0, nFolder = -1, nDefaultMission = 0; CListBox lb; static int nMission = -1; static const char *menuTitles [4]; menuTitles [0] = TXT_NEW_GAME; menuTitles [1] = TXT_NEW_D1GAME; menuTitles [2] = TXT_NEW_D2GAME; menuTitles [3] = TXT_NEW_GAME; gameStates.app.bD1Mission = 0; gameStates.app.bD1Data = 0; SetDataVersion (-1); if ((nMission < 0) || gameOpts->app.bSinglePlayer) gameFolders.szMsnSubDir [0] = '\0'; hogFileManager.UseMission (""); do { nMissions = BuildMissionList (0, nFolder); if (nMissions < 1) return; for (i = 0; i < nMissions; i++) { m.Push (gameData.missions.list [i].szMissionName); if (!stricmp (m [i], gameConfig.szLastMission)) nDefaultMission = i; } nMission = lb.ListBox (menuTitles [gameOpts->app.nVersionFilter], m, nDefaultMission); GameFlushInputs (); if (nMission == -1) return; //abort! nFolder = nMission; } while (!gameData.missions.list [nMission].nDescentVersion); strcpy (gameConfig.szLastMission, m [nMission]); if (!LoadMission (nMission)) { MsgBox (NULL, NULL, 1, TXT_OK, TXT_ERROR_MSNFILE); return; } gameStates.app.bD1Mission = (gameData.missions.list [nMission].nDescentVersion == 1); gameData.missions.nLastMission = nMission; nNewLevel = 1; PrintLog (" getting highest level allowed to play\n"); nHighestPlayerLevel = GetHighestLevel (); if (nHighestPlayerLevel > gameData.missions.nLastLevel) nHighestPlayerLevel = gameData.missions.nLastLevel; if (nHighestPlayerLevel > 1) { CMenu m (2); char szInfo [80]; char szNumber [10]; try_again: sprintf (szInfo, "%s %d", TXT_START_ANY_LEVEL, nHighestPlayerLevel); m.AddText (szInfo, 0); strcpy (szNumber, "1"); m.AddInput (szNumber, 10, ""); choice = m.Menu (NULL, TXT_SELECT_START_LEV); if ((choice == -1) || !m [1].m_text [0]) return; nNewLevel = atoi (m [1].m_text); if ((nNewLevel <= 0) || (nNewLevel > nHighestPlayerLevel)) { m [0].SetText (const_cast<char*> (TXT_ENTER_TO_CONT)); MsgBox (NULL, NULL, 1, TXT_OK, TXT_INVALID_LEVEL); goto try_again; } } SavePlayerProfile (); if (!DifficultyMenu ()) return; paletteManager.DisableEffect (); if (!StartNewGame (nNewLevel)) SetFunctionMode (FMODE_MENU); }
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; }