⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demolistview.cpp

📁 师兄做的算法可视化演示程序
💻 CPP
字号:
// DemoListView.cpp : implementation file
//

#include "stdafx.h"
#include "AlgorithmicDesign.h"
#include "DemoListView.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDemoListView

IMPLEMENT_DYNCREATE(CDemoListView, CListView)

CDemoListView::CDemoListView()
{
	memset(m_price,0,sizeof(m_price));
	memset(m_weight,0,sizeof(m_weight));
    m_number=0;
	m_totalweight=0;
	m_top=310;
	m_serize=0;
	m_Maxbotton=630;
	m_first=1;

}

CDemoListView::~CDemoListView()
{
}


BEGIN_MESSAGE_MAP(CDemoListView, CListView)
	//{{AFX_MSG_MAP(CDemoListView)
	ON_WM_SIZE()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemoListView drawing

void CDemoListView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CDemoListView diagnostics

#ifdef _DEBUG
void CDemoListView::AssertValid() const
{
	CListView::AssertValid();
}

void CDemoListView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDemoListView message handlers

void CDemoListView::OnInitialUpdate() 
{
	 CListView::OnInitialUpdate();
	 CListCtrl& theCtrl = GetListCtrl();
	 theCtrl.InsertColumn(0,"  演示  ",LVCFMT_CENTER,699);
	 DWORD dwStype=GetWindowLong(theCtrl.GetSafeHwnd(),GWL_STYLE);
	 dwStype&=~LVS_TYPEMASK;		//Remove the current stype flags
	 dwStype|=LVS_REPORT;		//Add the List stype
	 dwStype|=LVS_SHOWSELALWAYS;
	 dwStype|=LVS_NOLABELWRAP;
	 SetWindowLong(theCtrl.GetSafeHwnd(),GWL_STYLE,dwStype);	//Set it back into the list view

     COLORREF	clrBk	=RGB(150, 175, 230);
	 theCtrl.SetBkColor( clrBk);
     theCtrl.SetTextBkColor(clrBk);
 
	  //RECT	rect;
 	  //memset(&rect, 0, sizeof(RECT));
	  //this->GetClientRect(&rect);
 
	// TODO: Add your specialized code here and/or call the base class

	
}

void CDemoListView::OnSize(UINT nType, int cx, int cy) 
{
	CListView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
}

void CDemoListView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
     CRect recttone;
	 CPen pen;
	 pen.CreatePen(PS_SOLID,1,RGB(0,0,0));
	 CBrush br2(RGB(150, 175, 230));
	 CPen*p=dc.SelectObject(&pen);
	 //this->GetClientRect(&recttone);
     //dc.FillRect(&recttone,&br2);
     dc.MoveTo(100,350);
     dc.LineTo(100,650);
	 dc.MoveTo(600,350);
     dc.LineTo(600,650);
	 dc.MoveTo(100,650);
     dc.LineTo(600,650);
	 //CRect recttone;
	 recttone.top=650;
	 recttone.bottom=680;
	 recttone.left=200;
	 recttone.right=230;
     CBrush*old;
	 old=dc.SelectObject(&br2);
	 dc.Ellipse(&recttone);
	 recttone.left=470;
	 recttone.right=500;
	 dc.Ellipse(&recttone);
     DrawObject();
	
	// Do not call CListView::OnPaint() for painting messages
}

void CDemoListView::AddDemoData(int n, double m, double p[100], double w[100])
{
	 memset(m_price,0,sizeof(m_price));
	 memset(m_weight,0,sizeof(m_weight));
	 m_number=n;
     m_totalweight=m;
	 for(int i=0;i<m_number;i++)
	 {
		 m_price[i]=p[i];
		 m_weight[i]=w[i];
	 }

	// memcpy(m_price,p,sizeof(p));
	 //memcpy(m_weight,w,sizeof(w));

     /*int i,flag = 1;
	 double temp;
	 char *pdest;
	 int ch=',';
	 int k=0;
	 for(i=0;i<n-1;i++)
	 {
		 pdest = strchr(p, ch);
		 if( pdest != NULL )
		 {
			 *pdest='\0';
			 m_price[k]=atof(p);
			 k++;
			 strcpy(p,pdest+1);
		 }
	 }
	 m_price[k]=atof(p);
	 if((k+1)!=n)
	 {
		MessageBox("    有物体没有价格!","0/1背包问题的贪心算法",MB_OK);
	 }
	 k=0;
	 for(i=0;i<n-1;i++)
	 {
		 pdest = strchr(w, ch);
		 if( pdest != NULL )
		 {
			 *pdest='\0';
			 m_weight[k]=atof(w);
			 k++;
			 strcpy(w,pdest+1);
		 }
	 }
	 m_weight[k]=atof(w);
	 if((k+1)!=n)
	 {
		MessageBox("    有物体没有重量!","0/1背包问题的贪心算法",MB_OK);
	 }
    //排序,初始化
	 while (flag != 0) {
        flag = 0;
        for (i = 0; i < n-1; i++) {
            if (m_price[i]/m_weight[i] < m_price[i+1]/m_weight[i+1]) 	{
                temp = m_price[i];
                m_price[i] = m_price[i+1];
                m_price[i+1] = temp;
                temp = m_weight[i];
                m_weight[i] = m_weight[i+1];
                m_weight[i+1] = temp;
                flag = 1;
            }
        }
    }
*/
   CRect recttone;
   CBrush br2(RGB(150, 175, 230));
   //this->GetClientRect(&recttone);
   recttone.left=105;
   recttone.right=595;
   recttone.top=20;
   recttone.bottom=650;
   CDC*pdc=GetDC();
   pdc->FillRect(&recttone,&br2);
   OnPaint();
   m_top=310;
   m_serize=0;
   m_Maxbotton=630;
}

void CDemoListView::DrawObject()
{
	CDC*pdc=GetDC();
	CBrush br3;
    br3.CreateSolidBrush(RGB(0,0,0));
    pdc->SetBkColor(RGB(150, 175, 230));
	CString m_str;
    CRect recttone;
	recttone.top=310;
	recttone.bottom=330;
	int m_height=311;
	recttone.left=105;
	recttone.right=595;
	for(int i=0;i<m_number;i++)
	  {
		  pdc->FillRect(&recttone,&br3);
	      m_str.Format("No%d.  p[%d]/w[%d]: %f/%f",i+1,i+1,i+1,m_price[i],m_weight[i]);
          pdc->TextOut( 230,m_height,m_str);
          recttone.top-=21;
          recttone.bottom-=21;
          m_height-=21;
	  }
}

 

void CDemoListView::DrawOnTimer()
{
       // OnPaint();
		CDC*pdc=GetDC();
	    CBrush br2;
	    CBrush br3;
	    br2.CreateSolidBrush(RGB(150, 175, 230));
        br3.CreateSolidBrush(RGB(0,0,0));
        pdc->SetBkColor(RGB(150, 175, 230));
     	CString m_str;
        CRect recttone;
		//m_top=650;
	    recttone.top=m_top;
	    recttone.bottom=m_top+20;
	    recttone.left=105;
	    recttone.right=595;
		bool tfalse=1;
		if(m_first)
		{
			m_first=0;
			if(m_serize<m_number&&(m_totalweight-m_weight[m_serize])<0)
			{
	             m_str.Format("No%d.  p[%d]/w[%d]: %f/%f",m_serize+1,m_serize+1,m_serize+1,m_price[m_serize]-m_totalweight/m_weight[m_serize]*m_price[m_serize],m_weight[m_serize]-m_totalweight);
                 pdc->TextOut( 230,recttone.top+1,m_str);
                 price=m_totalweight/m_weight[m_serize]*m_price[m_serize];
				 weight=m_totalweight;
				 tfalse=0;
			}
			else
			{
				price=m_price[m_serize];
		        weight=m_weight[m_serize];
			}
		}
		if(tfalse)
		{
           pdc->FillRect(&recttone,&br2);
		}
	    recttone.top+=20;
	    recttone.bottom+=20;
        pdc->FillRect(&recttone,&br3);
	    m_str.Format("No%d.  p[%d]/w[%d]: %f/%f",m_serize+1,m_serize+1,m_serize+1,price,weight);
        pdc->TextOut( 230,recttone.top+1,m_str);
		m_top=m_top+20;

}

void CALLBACK  CDemoListView::OnTimerExcute(HWND hWnd,UINT nMsg,UINT nIDEvent,DWORD dwTime)
{
    CMainFrame*	pMainFrame	=(CMainFrame*)::AfxGetMainWnd();
	CDemoListView *pWnd;
    pWnd=(CDemoListView*)(pMainFrame->pPackgreedyFrame->p_DemolistView);
    if((pWnd->m_serize)<(pWnd->m_number)&&(pWnd->m_totalweight)>=(pWnd->m_weight[pWnd->m_serize]))
	{
	    pWnd->DrawOnTimer();
	    if((pWnd->m_top)>=(pWnd->m_Maxbotton))
		{
			pWnd->m_first=1;
		    pWnd->m_top-=341;
		    pWnd->m_Maxbotton-=21;
            pWnd->m_totalweight-=pWnd->m_weight[pWnd->m_serize];
			pWnd->m_serize++;
			pWnd->KillTimer(nIDEvent);
			pMainFrame->pPackgreedyFrame->p_AlgorithmiclistView->SetArithmeticTimer();
		}
		else
		{
			pWnd->m_first=0;
		}
	    if(pWnd->m_serize==pWnd->m_number)
	    	pWnd->KillTimer(nIDEvent);
	}
	else if((pWnd->m_serize)<(pWnd->m_number)&&(pWnd->m_totalweight)>0) //剩余部分
	{
         pWnd->DrawOnTimer();  
		 pWnd->m_first=0;
		 if((pWnd->m_top)>=(pWnd->m_Maxbotton))
		{
		    pWnd->m_top-=341;
		    pWnd->m_Maxbotton-=21;
            pWnd->m_totalweight-=pWnd->m_weight[pWnd->m_serize];
			pWnd->m_serize++;
			pWnd->KillTimer(nIDEvent);
			pMainFrame->pPackgreedyFrame->p_AlgorithmiclistView->SetArithmeticTimer();
		}
	    if(pWnd->m_serize==pWnd->m_number)
	    	pWnd->KillTimer(nIDEvent);
	}
	else
	{

		pWnd->KillTimer(nIDEvent);
	}

}


void CDemoListView::OnExecute()
{
	m_first=1;
	SetTimer(1,100,OnTimerExcute);
 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -