void CObjectManager::SortByID(int leftIndex , int rightIndex ) { int left = leftIndex; int right = rightIndex; CObject* temp; int pivot; if(!m_objectList[left] || !m_objectList[right]) return; //pivot = OID_NUMBER/2; pivot = m_objectList[(left+right)/2]->GetObjectID(); while( left <= right ) { //find GetObjectID() which is equal or bigger than pivot while ( m_objectList[left] && (m_objectList[left]->GetObjectID() < pivot)) { ++left; } //find GetObjectID() which is equal or bigger than pivot while ( !m_objectList[right] || (m_objectList[right]->GetObjectID() > pivot)) { --right; } if( left <= right ) { if(!m_objectList[left] || !m_objectList[right]) { temp = m_objectList[left]; m_objectList[left] = m_objectList[right]; m_objectList[right] = temp; } else if(m_objectList[left]->GetObjectID() != m_objectList[right]->GetObjectID()) { temp = m_objectList[left]; m_objectList[left] = m_objectList[right]; m_objectList[right] = temp; } left++; right--; } } if(leftIndex < right) SortByID(leftIndex, right); if(left < rightIndex) SortByID(left, rightIndex); }
void CObjectManager::RemoveObject(void) { //get object list's begin and end with empty space in the object list. std::vector<CObject*>::iterator iter = m_objectList.begin(), end = m_objectList.end() - m_emptySpace; int emptySpace = m_emptySpace; bool deleted = false; //to store whether the object will be delete or not while( iter != end && (*iter != NULL) ) { // if the object will be destroyed, change Object GetObjectID() into OID_NONE // and move its position to tomb! and increase number of empty space. if( (*iter)->IsDestroyed() ) { (*iter)->SetObjectID(OID_NONE); m_emptySpace++; deleted = true; } ++iter; } // if one of objects in the list will be deleted, if(deleted) { // sort by Object GetObjectID() SortByID(0, m_objectList.size() - 1 - emptySpace ); iter = m_objectList.end() - 1; // loop for removing the object which has true value in IsDestroyed(). for( unsigned int i = emptySpace ; i < m_emptySpace ; ++i ) { if(*(iter-i)) { delete *(iter-i); *(iter-i) = NULL; } } } }
void Clink::cstudent() { CStudent * pStu = new CStudent; pStu->Next = NULL; InitializeStruct(pStu); OutPutStudent(pStu); int nchoose; while (true){ cout << "请输入指令编码:" << endl; cout << "1,从链表中删除某个学生;" << endl; cout << "2,从链表中增加某个学生;" << endl; cout << "3,根据名字查找某个学生;" << endl; cout << "4,根据学号查找某个学生;" << endl; cout << "5,将学生按照姓名排列;" << endl; cout << "6,将学生按照学号排列;" << endl; cout << "7,将学生按照成绩排列;" << endl; cin >> nchoose; switch (nchoose){ case 1:{ char name[20]; cout << "请输入姓名:" << endl; cin >> name; DeleteStu(pStu, name); OutPutStudent(pStu); break; } case 2:{ InsertStu(pStu, name_a_student()); OutPutStudent(pStu); break; } case 3:{ char name[10]; cout << "输入学生名字:" << endl; cin >> name; FindStudentByName(pStu, name); break; } case 4:{ char id[20]; cout << "输入学生ID:" << endl; cin >> id; FindStudentByID(pStu, id); break; } case 5:{ bool b; cout << "正序排列请输入1,倒序排列请输入0:" << endl; cin >> b; SortByName(pStu, b); OutPutStudent(pStu); break; } case 6:{ bool b; cout << "正序排列请输入1,倒序排列请输入0:" << endl; cin >> b; SortByID(pStu, b); OutPutStudent(pStu); break; } case 7:{ int choose; bool b; cout << "正序排列请输入1,倒序排列请输入0:" << endl; cin >> b; cout << "请输入科目代号:1,语文;2,英语;3,数学:" << endl; cin >> choose; /*EnumScoreType c, e, m;*/ switch (choose){ case 1:{ SortByScore(pStu, c, b); break; } case 2:{ SortByScore(pStu, e, b); break; } case 3:{ SortByScore(pStu, m, b); break; } default:{ cout << "指令错误!" << endl; } } OutPutStudent(pStu); break; } default:{ cout << "指令错误!" << endl; } } } }
void CObjectManager::Sort(void) { // sort by Object GetObjectID() SortByID(0, m_objectList.size() - 1 - m_emptySpace ); }