直接上代码
// main.h
#include "stdafx.h"
#include "DateFile.h"
int _tmain(int argc, _TCHAR* argv[])
{
WORD wNum = 4;
InitDlgList(wNum);
WORD* pWordTar = GenerateDerangeOrder(wNum);
DerangeDlglist(pWordTar);
system("pause");
return 0;
}// DateFile.h
#pragma once
#include <windows.h>
#include <list>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
typedef enum {
DONT_SORT = 0, // 不需要排序
NEED_SORT // 需要排序
} SORTTYPE;
typedef struct tagNODE{
SORTTYPE nType;
WORD wSrcIndex;
WORD wTarIndex;
const bool operator < (const tagNODE& node) const {
if (nType == DONT_SORT && node.nType == NEED_SORT) {
if (wTarIndex == node.wTarIndex) {
return true;
}
} else if (nType == NEED_SORT && node.nType == DONT_SORT) {
if (wTarIndex == node.wTarIndex) {
return false;
}
}
bool bRet = (wTarIndex < node.wTarIndex);
return bRet;
}
} NODE, *PNODE;
typedef std::list<NODE> CNodeList;
CNodeList g_lstNode;
void InitDlgList(__in WORD wNum)
{
for (WORD i = 0; i < wNum; i ++) {
NODE node;
node.nType = NEED_SORT;
if (i+1 == 3 || i+1 == 1) {
NODE node;
node.nType = DONT_SORT;
g_lstNode.push_back(node);
}
g_lstNode.push_back(node);
}
}
WORD* GenerateDerangeOrder(__in WORD wNum)
{
// 1. 初始化待分配数组
WORD * pWordSrc = new WORD[wNum];
for (WORD i = 0; i < wNum; i ++) {
pWordSrc[i] = i + 1;
}
WORD * pWordTar = new WORD[wNum];
memset(pWordTar, 0, sizeof(WORD)*wNum);
// 2. 给目标数组赋不重复的值
srand(GetTickCount());
//CDlgList::iterator it = g_lstDlg.begin();
for (WORD i = 0; i < wNum; ) {
WORD wIndex = rand() % wNum;
if (pWordSrc[wIndex] != 0) {
pWordTar[i] = pWordSrc[wIndex];
//it->wTarIndex = pWordSrc[wIndex];
pWordSrc[wIndex] = 0;
i ++;
//it ++;
}
}
// 3. dump出来
for (WORD i = 0; i < wNum; i ++) {
//cout << pWordTar[i] << "\t";
}
// 4. 析构
delete [] pWordSrc;
//delete [] pWordTar;
return pWordTar;
}
void DerangeDlglist(WORD * pOrder)
{
CNodeList::iterator it = g_lstNode.begin();
for (int i = 0,j=0; it != g_lstNode.end(); it ++) {
if (it->nType == DONT_SORT) {
it->wSrcIndex = 0; // 暂时用-1代表GROUPDLG的原序
it->wTarIndex = (j == 0 ? 1 : j);
}
j ++;
if (it->nType == NEED_SORT) {
it->wSrcIndex = i+1;
it->wTarIndex = pOrder[i];
i ++;
}
}
cout << "Befor sort:" << endl;
// dump result
it = g_lstNode.begin();
for ( ; it != g_lstNode.end(); it ++) {
if (it->nType == DONT_SORT) {
cout << "DONT_SORT:" << it->wSrcIndex << " " << it->wTarIndex << endl;
} else if (it->nType == NEED_SORT) {
cout << "NEED_SORT:" << it->wSrcIndex << " " << it->wTarIndex << endl;
}
}
g_lstNode.sort();
cout << endl << "After sort:" << endl;
// dump result
it = g_lstNode.begin();
for ( ; it != g_lstNode.end(); it ++) {
if (it->nType == DONT_SORT) {
cout << "DONT_SORT:" << it->wSrcIndex << " " << it->wTarIndex << endl;
} else if (it->nType == NEED_SORT) {
cout << "NEED_SORT:" << it->wSrcIndex << " " << it->wTarIndex << endl;
}
}
delete [] pOrder;
pOrder = NULL;
}
1. 区分排序与勿需排序的点.
list中每个节点都有一个 SORTTYPE 结构来判断当前节点是需要排序的点, 还是不需要排序的点.
2. 如何比较排序与勿需排序的点
这时需要设计list的sort函数, 我们需要知道list中所有排序与勿需排序的点之前的位置,
然后在比较的时候, 根据设定的排序值来比较.
另附一张程序过程图

2743

被折叠的 条评论
为什么被折叠?



