• [手游项目4]C++封装的排行榜管理


    设计思想,部分代码

    
    //排行榜类
    class CRanking
    {
    	vector<CRankingItem*>			m_ItemList;				//保存所有的列表
    }
    
    //排行榜管理类
    vector<CRanking*>					m_RankingList;			 //排行榜的列表
    
    
    这个排行榜的核心就是数据变动后的自动排序的效率。
    最核心的排序函数,根据变动值的正负,上下移动。
    
    /*
    * Comments:把该项插到排行榜中,向上更新位置
    * Param CRankingItem * pItem:
    * @Return int:返回在排行榜中的位置
    */
    int CRanking::UpdateUpPos(CRankingItem* pItem)
    {
    	int start = (pItem->nIndex >= m_RankMax) ? m_RankMax : (pItem->nIndex);
    
    	for (int i = (int)(start - 1); i >= 0; i--)
    	{
    		CRankingItem* ri = m_ItemList[i];
    
    		if (CompareGrather(pItem,ri))//ri->nPoint < pItem->nPoint)
    		{
    			//换个位置
    			SwapItem(ri, pItem);
    			m_boModify = true;
    		}
    		else
    		{
    			break;
    		}
    	}
    
    	return pItem->nIndex;
    }
    
    /*
    * Comments:把该项插到排行榜中,向下更新位置
    * Param CRankingItem * pItem:
    * @Return int:返回在排行榜中的位置
    */
    int CRanking::UpdateDownPos(CRankingItem* pItem)
    {
    	if (pItem->nIndex < m_RankMax)
    	{
    		int i = 0, nCount = m_ItemList.size();
    
    		for (i = pItem->nIndex + 1; i < m_RankMax && i < nCount; i++)
    		{
    			CRankingItem* ri = m_ItemList[i];
    
    			if (CompareGrather(ri,pItem))//ri->nPoint > pItem->nPoint)
    			{
    				//换个位置
    				SwapItem(ri, pItem);
    				m_boModify = true;
    			}
    			else
    			{
    				break;
    			}
    		}
    
    		if (i >= m_RankMax)
    		{
    			//看看榜外有没有比它大的
    			CRankingItem* pMax = FindMaxItem();
    
    			if (pMax && CompareGrather(pMax,pItem))//pMax->nPoint > pItem->nPoint)
    			{
    				//换个位置
    				SwapItem(pMax, pItem);
    				m_boModify = true;
    			}
    		}
    	}
    
    	return pItem->nIndex;
    }
    
    
    

    完整源码  https://download.csdn.net/download/q277055799/11636878

  • 相关阅读:
    Qt判断文件夹是否存在并新建文件夹
    QFileDialog的使用
    C++11 std::chrono库详解
    disconnected no supported authentication methods available(server sent: publickey)
    connect函数的第5参数Qt::ConnectionType
    在C++ 中检查一个文件是否存在的几种方法
    win10打开便签
    1024. Palindromic Number (25)
    1023. Have Fun with Numbers (20)
    1021. Deepest Root (25)
  • 原文地址:https://www.cnblogs.com/byfei/p/14104134.html
Copyright © 2020-2023  润新知