代码如下,模拟vc的tab排序 主要就是当前要排的值和选中的之间调整顺序 #include <iostream> #include <vector> using namespace std; int main(int argc, char* argv[]) { vector<int> v; int cnt = 4; v.resize(4); for(int i = 0; i < cnt; i++) { v[i] = i; } int cur = 0; int sel; while(cin >> sel) { sel--; if(sel < 0 || sel >= cnt) continue; sel = v[sel]; if(cur < sel) { for(int i = 0; i < v.size(); i++) { if(v[i] >= cur && v[i] < sel) { v[i]++; } else if(v[i] == sel) { v[i] = cur; } } cur++; } else if(cur > sel) { for(int i = 0; i < v.size(); i++) { if(v[i] > sel && v[i] <= cur) { v[i]--; } else if(v[i] == sel) { v[i] = cur; } } } vector<int>::iterator iter; for(iter = v.begin(); iter != v.end(); ++iter) { cout << (*iter) + 1<< " "; } cout << endl; } return 0; }
项目结束了,现在贴出Tab机制的代码,原理很简单:"已经被点击过的Tab和没有被点击过的Tab的内部Index变换和相互间的Index变换",比较抽象,那就看代码也许更容易些: //VS Tab mechanism
struct TABORDER{ BOOL bClicked; int iCurValue; }; //此参数从其他类调用时传过来
TABORDER TabOrder[STROKE_NUM]; class CTabMechanism { public: //初始化Tab顺序的数组
BOOL InitTabOrder(struct TABORDER (&TabOrder)[STROKE_NUM]); //调整Tab顺序
void AdjustTabOrder(int strokes,int CurNum,TABORDER (&TabOrder)[STROKE_NUM]); //找到已经被点击或没有被点击的Tab中index最大的
int FindMax(int CurTabNum,int strokes,BOOL clicked,struct TABORDER (&TabOrder)[STROKE_NUM]); //判断是否为第一次点击
BOOL IsFirstClick(int strokes,struct TABORDER (&TabOrder)[STROKE_NUM]); //修改Tab的Index并保存
BOOL ChangeTabOrder(int strokes,struct TABORDER (&TabOrder)[STROKE_NUM],int CurTabNum); CTabMechanism(); virtual ~CTabMechanism(); public: }; CTabMechanism::CTabMechanism() { } CTabMechanism::~CTabMechanism() { } BOOL CTabMechanism::ChangeTabOrder(int strokes,TABORDER (&TabOrder)[STROKE_NUM],int CurTabNum) { int iMax,i; //第一次点击Tab
if( IsFirstClick( strokes,TabOrder) ) { for(i=0;i<=CurTabNum;i++) { if(TabOrder[i].iCurValue < TabOrder[CurTabNum].iCurValue) TabOrder[i].iCurValue++; } TabOrder[CurTabNum].iCurValue = 1; TabOrder[CurTabNum].bClicked = TRUE; }//if first click
//两次以上点击Tab(不同的或相同的Tab)
else { //当前的Tab已经被点击过了
if(TRUE == TabOrder[CurTabNum].bClicked) { //find the
iMax = FindMax(CurTabNum,strokes,TabOrder[CurTabNum].bClicked,TabOrder); AdjustTabOrder(strokes,CurTabNum,TabOrder); //当前的Tab获得已经被点击过的所有Tab里面的最大值
TabOrder[CurTabNum].iCurValue = iMax; //modify the clicked tabs' value otherwise the CurTabNum
}//if
//当前的Tab从来都没有被点击过
else { iMax = FindMax(CurTabNum,strokes,TabOrder[CurTabNum].bClicked,TabOrder); AdjustTabOrder(strokes,CurTabNum,TabOrder); //当前的Tab获得已经被点击过的所有Tab里面的(最大值+1)
TabOrder[CurTabNum].iCurValue = iMax; TabOrder[CurTabNum].bClicked = TRUE; }//else
}//else
return TRUE; } BOOL CTabMechanism::IsFirstClick(int strokes,TABORDER (&TabOrder)[STROKE_NUM]) { for(int i=0;i<strokes;i++) if(TabOrder[i].bClicked) return FALSE; return TRUE; } int CTabMechanism::FindMax(int CurTabNum,int strokes,BOOL clicked,TABORDER (&TabOrder)[STROKE_NUM]) { int i,value = 0; for(i=0;i<strokes;i++) { //find in clicked tabs
if(TabOrder[i].bClicked) if(value < TabOrder[i].iCurValue) value = TabOrder[i].iCurValue; }//for
//当前的Tab已经被点击过了
if(clicked) return value; //transmit to clicked tab
else return ++value; //transmit to non-clicked tab
}//FindMax
void CTabMechanism::AdjustTabOrder(int strokes,int CurNum, TABORDER (&TabOrder)[STROKE_NUM]) { int i; if(TabOrder[CurNum].bClicked) { for(i=0;i<strokes;i++) if(TabOrder[i].bClicked && ( CurNum) ) if(TabOrder[i].iCurValue > TabOrder[CurNum].iCurValue) //比当前Tab的Index值大的都减1(范围:所有点击过的Tab)
--(TabOrder[i].iCurValue); }//if
else { for(i=0;i<strokes;i++) if( !(TabOrder[i].bClicked) && i!=CurNum ) if(TabOrder[i].iCurValue < TabOrder[CurNum].iCurValue) //比当前Tab的Index值小的都加1(范围:所有没被点击过的Tab中)
++(TabOrder[i].iCurValue); } return; } BOOL CTabMechanism::InitTabOrder(TABORDER (&TabOrder)[STROKE_NUM]) { int i; for( i=0;i<STROKE_NUM;i++ ) { TabOrder[i].bClicked = FALSE; TabOrder[i].iCurValue = i+1; } return TRUE; }
|