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

📄 cslistdlg.cpp

📁 是数据结构中单链表的VC++程序
💻 CPP
字号:
// SListDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Ex_DLB.h"
#include "CSListDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSListDlg dialog

#include "Slist.CPP"
SList<int> m_slist;

CSListDlg::CSListDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSListDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSListDlg)
	m_createno = 0;
	m_radiohead = -1;
	m_radiobefore = -1;
	m_data = 0;
	m_nodeaddress = _T("");
	m_nodedata = _T("");
	m_nodenext = _T("");
	//}}AFX_DATA_INIT
}


void CSListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSListDlg)
	DDX_Text(pDX, IDC_EDIT4, m_createno);
	DDX_Radio(pDX, IDC_RADIO1, m_radiohead);
	DDX_Radio(pDX, IDC_RADIO3, m_radiobefore);
	DDX_Text(pDX, IDC_EDIT6, m_data);
	DDX_Text(pDX, IDC_EDIT9, m_nodeaddress);
	DDX_Text(pDX, IDC_EDIT7, m_nodedata);
	DDX_Text(pDX, IDC_EDIT8, m_nodenext);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSListDlg, CDialog)
	//{{AFX_MSG_MAP(CSListDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSListDlg message handlers

void CSListDlg::OnButton1() //初始化
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	if(m_createno<=99&&m_createno>=0)
	{
		m_slist.MakeEmpty();
		if(m_radiohead==1)
    		m_slist.Create(m_createno,true);
		else
			m_slist.Create(m_createno,false);
		DrawSList();	
    }
}

void CSListDlg::OnButton4() //下一个节点
{
	// TODO: Add your control notification handler code here
	m_slist.Next();
	DrawSList();
}

void CSListDlg::DrawSList()
{
	SListnode<int> *cp;
	cp=m_slist.GetCurrent();

	COLORREF m_ForeColor;
	m_ForeColor=RGB(0,0,255);

    CWnd *pWnd=GetDlgItem(IDC_DRAW_SLIST);
	CDC *pDC=pWnd->GetDC();
   
	COLORREF BKColor,BKColor0,BKColor1,BKColor2;
	CBrush BKBrush,BKBrush0,BKBrush1,BKBrush2,DrawBrush;
	BKColor=RGB(255,255,255);BKBrush.CreateSolidBrush(BKColor);//背景色
	BKColor0=RGB(255,0,0);BKBrush0.CreateSolidBrush(BKColor0);//头节点颜色
	BKColor1=RGB(0,255,255);BKBrush1.CreateSolidBrush(BKColor1);//一般节点颜色
	BKColor2=RGB(0,255,0);BKBrush2.CreateSolidBrush(BKColor2);//当前节点颜色
	
	DrawBrush.CreateHatchBrush(7,m_ForeColor);
	CBrush *pOldBrush=pDC->SelectObject(&BKBrush);
	CRect rcClient;
	pWnd->GetClientRect(rcClient);
	pDC->Rectangle(rcClient);
	pDC->SelectObject(&DrawBrush);
	
	CRect rc;
	int cw,ch,cwd,chd;
	CString str;

	CPen Pen(0,0,RGB(255,0,0));
	CPen *oldPen=pDC->SelectObject(&Pen);

    int n;
	n=m_slist.Length();
    int p=rcClient.Width();
	int *lx=new int[n+2];//记录尾节点的x坐标
    int *ly=new int[n+2];//记录尾节点的y坐标

	cw=rcClient.Width()/15;//矩形的宽度
	cwd=(rcClient.Width()-(rcClient.Width()/15*10))/11;//两个矩形间的宽度
	ch=rcClient.Height()/15;//矩形的高度
	chd=(rcClient.Height()-(rcClient.Height()/15*10))/11;//两行矩形间的空隙高度
    str.Format("%dt%dt%d",cw,cwd,p);

	//MessageBox(str);
	for(int i=0;i<=n;i++)
    {
        //判断当前节点的位置
		if(m_slist.GetCurrent()==m_slist.Getnode(i)) 
           pDC->SelectObject(&BKBrush2);
		
		
		int *tem=m_slist.Getdata(i);
		//如果是奇数行则,从左到右画图
		if((i/10)%2!=1)
		{
		  rc.top=chd*((i/10)+1)+ch*(i/10);
		  rc.left=cwd+(cw+cwd)*(i%10);
		  rc.right=cwd+cw+(cw+cwd)*(i%10);
		  rc.bottom=(chd+ch)*((i/10)+1);
		  pDC->Rectangle(rc);
		  pDC->SelectObject(&BKBrush1);
		  
		  //使用str存储每个结点的data值	Type *Getdata(int i);//取第i号结点的值
		   lx[i]=rc.right;
		   ly[i]=(rc.top+rc.bottom)/2;
			
			if(i>=1)
			  {
				  str.Format("%d",*tem);
			      pDC->SetBkMode(TRANSPARENT);
				  pDC->TextOut(rc.left+6,rc.top+5,str,strlen(str));
			
			  }
		}
		//如果是偶数行,则从右到左画图
		else
		{
            rc.top=chd*((i/10)+1)+ch*(i/10);
		    rc.left=10*cwd+9*cw-(i%10)*(cwd+cw);
			rc.bottom=(chd+ch)*((i/10)+1);
            rc.right=10*(cwd+cw)-(i%10)*(cwd+cw);
		    pDC->Rectangle(rc);
			pDC->SelectObject(&BKBrush1);
		   lx[i]=rc.right;
		   ly[i]=(rc.top+rc.bottom)/2;
			if(i>=1)
			  {
				  str.Format("%d",*tem);
				  pDC->SetBkMode(TRANSPARENT);
				  pDC->TextOut(rc.left+6,rc.top+5,str,strlen(str));
		
			  } 
		}
    }
    for(i=0;i<=n;i++)
    {	 
		     //画连接线
		     if(((i/10)+1)%2==1&&i<n)
			 {  
				 pDC->MoveTo(lx[i],ly[i]);
		         pDC->LineTo(lx[i]+cwd,ly[i]);
			 }
			 if(((i/10)+1)%2==0)
			 {
			     pDC->MoveTo(lx[i],ly[i]);
		         pDC->LineTo(lx[i]+cwd,ly[i]);
			 			 			 
			 }
			 //在奇数行画指向右边的箭头
			if(((i/10)+1)%2==1&&i<n)
			{		 
			 pDC->LineTo(lx[i]+cwd-5,ly[i]-5);
			 pDC->MoveTo(lx[i]+cwd,ly[i]);
			 pDC->LineTo(lx[i]+cwd-5,ly[i]+5);
			}
			 //在偶数行画指向左边的箭头	 
			 if(((i/10)+1)%2==0)
			 {  
		        pDC->MoveTo(lx[i],ly[i]);
				pDC->LineTo(lx[i]+5,ly[i]-5);
		        pDC->MoveTo(lx[i],ly[i]);
		        pDC->LineTo(lx[i]+5,ly[i]+5);
			 }
			 //	若是奇数行 右边画竖线
		if(((i/10)+1)%2==1&&i%10==9&&i<n)
	   {	   pDC->MoveTo(lx[i]+cwd,ly[i]);
               pDC->LineTo(lx[i]+cwd,ly[i]+ch+chd);
	   }
	   //若是偶数行    左边画竖线
		if(((i/10)+1)%2==0&&i%10==9&&i<99&&i<n)
	   {
	     pDC->MoveTo(lx[i]-cw,ly[i]);
		 pDC->LineTo(lx[i]-cw-12,ly[i]);
	     pDC->LineTo(lx[i]-cw-12,ly[i]+ch+chd);
         pDC->MoveTo(lx[i]-cw-12+5,ly[i]-5);
		 pDC->LineTo(lx[i]-cw-12,ly[i]);
		 pDC->MoveTo(lx[i]-cw-12+5,ly[i]+5);
         pDC->LineTo(lx[i]-cw-12,ly[i]);
        
		 pDC->MoveTo(lx[i]-cw-5,ly[i]+ch+chd-5);
		 pDC->LineTo(lx[i]-cw, ly[i]+ch+chd);
         pDC->MoveTo(lx[i]-cw-5,ly[i]+ch+chd+5);
         pDC->LineTo(lx[i]-cw, ly[i]+ch+chd);
		 
		 pDC->MoveTo(lx[i]-cw-12,ly[i]+ch+chd);
		 pDC->LineTo(lx[i]-cw,ly[i]+ch+chd);
           
	   }
    }
	pDC->SelectObject(oldPen);
	pDC->SelectObject(pOldBrush);
	ReleaseDC(pDC);
	delete [n+1]lx;
	delete [n+1]ly;
	
	m_nodeaddress="";
	int *tt=m_slist.Current();
	str.Format("%d",*tt);
	m_nodedata=str;
	str.Format("%X",m_slist.GetCurrent());
    m_nodeaddress=str;
	
	str.Format("%X",m_slist.Getnext());
    m_nodenext=str;
	UpdateData(false);
	m_slist.SetCurrent(cp);
}

void CSListDlg::OnButton3() //头结点
{
	// TODO: Add your control notification handler code here
	m_slist.First();
	DrawSList();	
}
void CSListDlg::OnButton6() //插入
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	if(m_slist.Length()<99)
	{
		if(m_radiobefore==0)
			m_slist.Insert(m_data,false);
		else
			m_slist.Insert(m_data,true);
		DrawSList();	
	}
	else
	{
	    AfxMessageBox("对不起了您,单链表演示最多99个节点!");
	}
}

void CSListDlg::OnButton8() //删除
{
	// TODO: Add your control notification handler code here
	m_slist.Delete();	
	DrawSList();
}

void CSListDlg::OnButton7() //修改
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_slist.Modifydata(m_data);
	DrawSList();	
}

void CSListDlg::OnButton5() //清空
{
	// TODO: Add your control notification handler code here
	m_slist.MakeEmpty();
	DrawSList();
	m_createno=0;
	UpdateData(false);
}

void CSListDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	if((m_slist.Gethead())->nextnode()==NULL)
	{
	    UpdateData(true);
		if(m_radiohead==0)
    		m_slist.Create(m_createno,true);
		else
			m_slist.Create(m_createno,false);
	}

	m_slist.Invert();
	DrawSList();
}

⌨️ 快捷键说明

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