友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
喜书网 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

C语言实例教程(PDF格式)-第95章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!






}  



6。 为了确定用户选择了哪一个单选按钮,重载CDialog::OnOK()。  



void CArrayRemoveDlg::OnOK()   



{  


…………………………………………………………Page 602……………………………………………………………

// TODO: Add extra validation here  



UINT nRadio=GetCheckedRadioButton(IDC_REMOVE0;IDC_REMOVE1);  



switch(nRadio)  



{  



case IDC_REMOVE0:  



m_radio=0;  



break;  



case IDC_REMOVE1:  



m_radio=1;  



break;  



default:  



break;  



}  



CDialog::OnOK();  



}  



现在编译并运行这个程序,首先在窗口中显示一个有十个元素的数 

组,如图10。3所示。单击左键弹出如图10。1所示的对话框,你可以选 

择三种数组操作:设置、插入和添加。单击右键弹出如图10。2所示的 

对话框,你可以选择两种删除操作:删除全部元素和删除指定元素。  



                                                                



                              图10。 3 程序运行初始窗口  



    


…………………………………………………………Page 603……………………………………………………………

                   第二节 列表类  



列表类象是有特殊功能的数组。列表的元素被称为节点。列表使用指 

针来连结它的节点。如果你希望快速的插入和删除数组元素,列表类 

是一个比较好的选择。但是在列表中查找一个元素要比在数组中慢, 

因为列表需要按照指针顺序从一个节点到另外一个节点。  



通常我们称列表中第一个节点为列表的头,列表中最后一个节点是列 

表的尾。  



列表类有以下成员函数:  



Clist   



Clist类的构造函数,其中的参数指定分配内存的基本单元。  



GetHead( )  



获得列表的第一个元素的值。  



GetTail( )  



获得列表的最后一个元素的值。  



RemoveHead( )  



删除列表中第一个元素  



RemoveTail( )  



删除列表中最后一个元素。  



AddHead   



在列表的头部添加一个节点,使这个节点成为列表的新的头。  



AddTail   



在列表的尾部添加一个节点,使这个节点成为列表的新的尾。  



RemoveAll()  



删除节点中所有的元素。  


…………………………………………………………Page 604……………………………………………………………

GetHeadPosition( )  



获得列表的头节点的位置。  



GetTailPosition( )  



获得列表中尾节点的位置。  



GetNext()  



获得指定位置下一个节点处的值。  



GetPrev()  



获得指定位置上一个节点处的值。  



GetAt()  



获得指定位置处节点的值。  



SetAt()  



设置指定位置处节点的值。  



RemoveAt()  



删除指定位置处的节点。  



InsertBefore()  



在指定位置的前面插入一个节点。  



InsertAfter()  



在指定位置的后面插入一个节点。  



Find()  



按照列表顺序搜索给定的对象指针,返回一个POSITION类型的量。  



FindIndex()  



按照列表顺序搜索指定的下标。  



GetCount()  


…………………………………………………………Page 605……………………………………………………………

获得列表中包含的节点个数。  



IsEmpty()  



检查一个列表是否不含有任何节点。  



下面的程序将允许用户添加和删除节点,按照以下步骤进行:  



1。 使用MFC AppWizard创建一个单文档应用程序List。  



2。   添加一个对话框类CAddStudentDlg,其对应的对话框如图10。4所 

示。  



                                          



                       图10。 4 添加节点对话框  



3。 为两个文本框映射两个变量m_name,m_score。  



4。  添加一个对话框类CRemoveStudentDlg,其对应的对话框如图10。5 

所示。两个单选按钮的ID为IDC_REMOVE0和IDC_REMOVE1。  



                                          



                       图10。 5 删除节点对话框  



5。 为对话框类添加一个UINT类型的成员变量m_radio。  



6。 在OnInitDialog()函数中设置单选按钮的初始状态。  



BOOL CRemoveStudentDlg::OnInitDialog()   



{  



CDialog::OnInitDialog();  


…………………………………………………………Page 606……………………………………………………………

// TODO: Add extra initialization here  



((CButton*)GetDlgItem(IDC_REMOVE0))…》SetCheck(1);  



return TRUE; // return TRUE unless you set the focus to a control  



// EXCEPTION: OCX Property Pages should return FALSE  



}  



7。   为了知道用户选择了哪一个单选按钮,在OnOk()函数中添加下面 

的代码。  



void CRemoveStudentDlg::OnOK()   



{  



// TODO: Add extra validation here  



UINT nRadio=GetCheckedRadioButton(IDC_REMOVE0;IDC_REMOVE1);  



switch(nRadio)  



{  



case IDC_REMOVE0:  



m_radio=0;  



break;  



case IDC_REMOVE1:  



m_radio=1;  



break;  



default:  



break;  



}  



CDialog::OnOK();  



}  



8。  在ListView。h中CListView类的声明之前添加如下代码,用来定义 

一个结构体CStudent,包含两个变量m_name和m_score,分别用于存 

放学生的姓名和成绩。  


…………………………………………………………Page 607……………………………………………………………

struct CStudent  



{  



CString m_name;  



int m_score;  



};  



9。 为ClistView添加一个类型为CptrList的成员变量m_list。  



10。 在ListView。cpp中添加下列语句:  



#include 〃AddStudentDlg。h〃  



#include 〃RemoveStudentDlg。h〃  



11。  当用户单击左键后,弹出如图10。4所示的对话框,可以添加一个 

节点。对应的OnLButtonDown 函数代码如下:  



void CMyListView::OnLButtonDown(UINT nFlags; CPoint point)   



{  



// TODO: Add your message handler code here and/or call default  



CAddStudentDlg dialog;  



dialog。m_name = 〃〃;  



dialog。m_score = 0 ;  



// Display the dialog box。  



int result = dialog。DoModal();  



if (result == IDOK)  



{  



// Create and initialize the new node。  



CStudent* m_pStudent = new CStudent;  



m_pStudent…》m_name = dialog。m_name;  



m_pStudent…》m_score = dialog。m_score;  



// Add the node to the list。  


…………………………………………………………Page 608……………………………………………………………

m_list。AddTail(m_pStudent);  



// Repaint the window。  



Invalidate();  



}  



CView::OnLButtonDown(nFlags; point);  



}  



12。  当用户单击右键后,弹出如图10。5所示的对话框,可以删除一个 

节点。对应的OnRButtonDown 函数代码如下:  



void CMyListView::OnRButtonDown(UINT nFlags; CPoint point)   



{  



// TODO: Add your message handler code here and/or call default  



CRemoveStudentDlg dialog;  



dialog。m_radio = 0;  



// Display the dialog box。  



int result = dialog。DoModal();  



// If the user clicked the OK button。。。  



if (result == IDOK)  



{  



CStudent* m_pStudent=new CStudent;  



// Make sure the list isn't empty。  



if (m_list。IsEmpty())  



MessageBox(〃节点已经全部删除!〃);  



else  



{  



// Remove the specified node。  



if (dialog。m_radio == 0)  


…………………………………………………………Page 609……………………………………………………………

m_pStudent = (CStudent*)m_list。RemoveHead();  



else  



m_pStudent = (CStudent*)m_list。RemoveTail();  



// Delete the node object and repaint the window。  



delete m_pStudent;  



Invalidate();  



}  



}  



CView::OnRButtonDown(nFlags; point);  



}  



13。 最后设置OnDraw 函数用来响应Invalidate()。  



void CMyListView::OnDraw(CDC* pDC)  



 {  



CListDoc* pDoc = GetDocument();  



ASSERT_VALID(pDoc);  



// TODO: add draw code for native data here  



TEXTMETRIC textMetric;  



pDC…》GetTextMetrics(&textMetric);  



int fontHeight = textMetric。tmHeight;  



// Initialize values used in the loop。  



POSITION pos = m_list。GetHeadPosition();  



int displayPosition = 10;  



// Iterate over the list; displaying each node's values。  



while (pos != NULL)  



 {  



CStudent* m_pStudent = (CStudent*)m_list。GetNext(pos);  


…………………………………………………………Page 610……………………………………………………………

char s'81';  



wsprintf(s; 〃 的成绩是 %d。〃;m_pStudent…》m_score);  



CString m_string=m_pStudent…》m_name+s;  



pDC…》TextOut(10; displayPosition; m_string);  



displayPosition += fontHeight;  



}  



}  



                                                               



                             图10。 6 程序运行的初始窗口  



14。 最后在CListView的析构函数中删除数组中所有的节点。  



CMyListView::~CMyListView()  



{  



while (!m_list。IsEmpty())  



{  



CStudent* m_pStudent = (CStudent*)m_list。RemoveHead();  



del
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!