int main(int argc, const char * argv[]) { string inputFileName = "car_record.txt"; ifstream infs(inputFileName.c_str()); if(!infs){ cerr<<"Can't open the file "<<inputFileName<<endl; return EXIT_FAILURE; } ListStack<Car> parkingStack; //按行读取数据文件 while(!infs.eof()) { Car car; infs>>car; parkingStack.push(car); } infs.close(); while(!parkingStack.isEmpty()) { cout<<parkingStack.getTop()<<"was moved "<<endl; parkingStack.pop(); cout<<endl; } return EXIT_SUCCESS; }
void intrav() { BinTree* root = this; ListStack<BinTree*> st; while (root != 0 || !st.isEmpty()) { while (root != 0) { st.push(root); root = root -> ls; } if (!st.isEmpty()) { root = st.pop(); cout << root -> data << " "; root = root -> rs; } } }
int main(int argc, const char * argv[]) { cout<<"请输入括号序列(以0结束):"; ListStack<char> stack; char inputSymbol; do { cin>>inputSymbol; switch (inputSymbol) { case '(': stack.push(inputSymbol); break; case ')': if (stack.isEmpty()) { cout<<"Wrong!"<<endl; exit(0); } else { stack.pop(); } break; default: break; } } while (inputSymbol != '0'); if (stack.isEmpty()) { cout<<"OK!"<<endl; } else cout<<"Wrong!"<<endl; return 0; }
void Display::ShowDataStructItems() { ArrayStack<int> dsArrayStack; ListStack<int> lsListStack; ArrayQueue<int> aqArrayQueue; CycleDoublyLinkList<int> dcllCycleDoublyLinkList; BinarySearchTree<int> bstBinarySearchTree; RedBlackTree<int> rbtRedBlackTree; MergeFindSet<int> mfsMergeFindSet; HashTable<int> htHashTable; cout<<WELCOME_STRING<<endl; cout<<"This is the Implement of Data Structs"<<endl; cout<<"Please Select the Item you are Interest in:"<<endl; cout<<"1. Stack(Implement of Array);"<<endl; cout<<"2. Stack(Implement of List);"<<endl; cout<<"3. Queue(Implement of Array);"<<endl; cout<<"4. Cycle Doubly Linked List"<<endl; cout<<"5. Binary Search Tree"<<endl; cout<<"6. Red Black Tree"<<endl; cout<<"7. Merge Find Set"<<endl; cout<<"8. Hash Table"<<endl; cout<<"98. Up Layer;"<<endl; cout<<"99. Quit."<<endl; cout<<STAR_STRING<<endl; cout<<endl; int nSelect; while(1) { cin>>nSelect; if(cin.fail()) { ClearScreen(); cout<<"Input Error! Please Select Again!"<<endl; cin.clear(); cin.sync(); ShowDataStructItems(); } ClearScreen(); switch(nSelect) { case 1: cout<<dsArrayStack.GetTitle().c_str()<<endl; dsArrayStack.Description(); dsArrayStack.Test(); ShowDataStructItems(); break; case 2: cout<<lsListStack.GetTitle().c_str()<<endl; lsListStack.Description(); lsListStack.Test(); ShowDataStructItems(); break; case 3: cout<<aqArrayQueue.GetTitle().c_str()<<endl; aqArrayQueue.Description(); aqArrayQueue.Test(); ShowDataStructItems(); break; case 4: cout<<dcllCycleDoublyLinkList.GetTitle().c_str()<<endl; dcllCycleDoublyLinkList.Description(); dcllCycleDoublyLinkList.Test(); ShowDataStructItems(); break; case 5: cout<<bstBinarySearchTree.GetTitle().c_str()<<endl; bstBinarySearchTree.Description(); bstBinarySearchTree.Test(); ShowDataStructItems(); break; case 6: cout<<rbtRedBlackTree.GetTitle().c_str()<<endl; rbtRedBlackTree.Description(); rbtRedBlackTree.Test(); ShowDataStructItems(); break; case 7: cout<<mfsMergeFindSet.GetTitle().c_str()<<endl; mfsMergeFindSet.Description(); mfsMergeFindSet.Test(); ShowDataStructItems(); break; case 8: cout<<htHashTable.GetTitle().c_str()<<endl; htHashTable.Description(); htHashTable.Test(); ShowDataStructItems(); break; case 98: goto ShowWelcome; break; case 99: exit(0); break; default: ClearScreen(); cout<<"Select Error! Please Select Again!"<<endl; cin.clear(); cin.sync(); ShowDataStructItems(); break; } } ShowWelcome: Show(); return; }
void posttrav() { BinTree* root = this; ListStack<BinTree*> st; ListStack<int> stf; bool first = true; while ((root != this || first) || !st.isEmpty()) { first = false; //if it's not the first time to run the circle; while (root != 0) { st.push(root); stf.push(1); root = root -> ls; } while (!st.isEmpty() && stf.getTop() == 2) { root = st.pop(); stf.pop(); cout << root -> data << " "; } if (!st.isEmpty()) { stf.pop(); stf.push(2); root = st.getTop() -> rs; } } }
int main() { string ins = "x"; while (ins != "") { ListStack calculate; bool error = false; int start = 0; getline(cin, ins); string toPush; int size = ins.length(); for (int i=0; i<size; i++){ //loop through all of input if (error) break; if (ins[i] == ' '){ //when we find a space, we do the following char* inc = new char[i-start+1]; for (int j=0; j<(i-start+1); j++) inc[j] = 0; for (int j=start; j<i; j++) inc[j-start] = ins[j]; toPush = inc; //copy that char array to a string delete [] inc; start = i+1; if (toPush.find_first_not_of("1234567890.") == string::npos){ double n = atof(toPush.c_str()); calculate.push(n); } } else{ try{ char symbol = ins[i]; if (symbol == '+') calculate.add(); if (symbol == '-') calculate.subtract(); if (symbol == '*') calculate.multiply(); if (symbol == '/'){ try{ calculate.divide(); }catch(DivideByZero e){ e.printMessage(); error = true; } } if (symbol == '^') calculate.exp(); } catch(Invalid e) { e.printMessage(); error = true; } } } if (!error){ if (calculate.count==1) cout << calculate.pop() << endl; else{ cout << "Operator mis-match!" << endl; while (!empty) calculate.pop(); } } } return 0; }