数据结构课程设计实习报告

  1. 复习MFC的相关操作。

  2. 巩固《数据结构》的基础知识并实现:List, Stack. Sort

  3. 熟练调用系统类库,加深对软件本质的理解。

  4. 理解多态,宏的意义,并运用。

二.设计任务

根据《数据结构》课程设计的结构体系,基于MFCSDI应用程序。利用树状图实现各级菜单,并实现对应的消息响应。线性表,一元多项式相加,栈的应用,排序都基于对话框。

三.程序结构

1.程序结构图:


2.类图:


3.类关系图


4.FileViewTreeView

      FileView显示数据,TreeView处理消息响应;有代表性的代码如下:

voidCFileView::FillFileView()

{

   HTREEITEM hRoot = m_wndFileView.InsertItem(_T("数据结构"),0, 0);

   m_wndFileView.SetItemState(hRoot,TVIS_BOLD,TVIS_BOLD);

 

   HTREEITEM hSrc = m_wndFileView.InsertItem(_T("绪论"),0, 0, hRoot);

 

   m_wndFileView.InsertItem(_T("什么是数据结构"),2,2, hSrc);

   m_wndFileView.InsertItem(_T("基本概念和术语"),2, 2, hSrc);

   m_wndFileView.InsertItem(_T("抽象数据类型的表示和实现"),2,2 ,hSrc);

   m_wndFileView.InsertItem(_T("算法和算法分析"),2, 2, hSrc);

 

   m_wndFileView.Expand(hRoot,TVE_EXPAND);

   m_wndFileView.Expand(hSrc,TVE_EXPAND);

……

}

voidCViewTree::OnNMClick(NMHDR *pNMHDR,LRESULT *pResult)

   {

   DWORD dwpos = GetMessagePos();

   TVHITTESTINFO ht = { 0 };

   ht.pt.x =GET_X_LPARAM(dwpos);

   ht.pt.y =GET_Y_LPARAM(dwpos);

   ::MapWindowPoints(HWND_DESKTOP,pNMHDR->hwndFrom,&ht.pt, 1);

   TreeView_HitTest(pNMHDR->hwndFrom, &ht);

   CString sel =this->GetItemText(ht.hItem);

#defineJUDGE(x)if(sel==x)

   JUDGE(_T("图的定义和术语"))

       AfxMessageBox(_T("图:是一种较线性表和树更为复杂的数据结构。结点直接的关系可以是任意的,图中任意两个数据元素之间都可能相关。\n顶点:数据元素。\n弧:两个顶点之间的关系。\n弧头:弧的终端点。\n弧尾:弧的初始点。\n有向图:有指向的图。\n无向图:没有指向的图。\n连通图:任意两个顶点都连通。"));

   JUDGE(_T("线性表的顺序表示和实现"))

       {

       CListDLg*m_listDlg =newCListDLg();

       m_listDlg->DoModal();

       }

   JUDGE(_T("线性表的链式表示和实现"))

       {

       CListDLg*m_SqListDlg =newCListDLg();

       m_SqListDlg->DoModal();

       }

   JUDGE(_T("一元多项式的表示及相加"))

       {

       CPolynomialDlg*m_pPolynomialDlg =newCPolynomialDlg();

       m_pPolynomialDlg->DoModal();

 

       }

   JUDGE(_T("栈的应用举例"))

       {

       CUSESTACK*m_stackDlg =newCUSESTACK();

       m_stackDlg->DoModal();

JUDGE(_T("插入排序"))

       {

       CSortDlg*sortDlg =newCSortDlg();

       sortDlg->DoModal();

       }

……

}

5.ListDlg

voidCListDLg::OnBnClickedButtonCreate()

   {

   UpdateData(TRUE);

   GetDlgItemTextW(IDC_EDIT_ITEMOFCREATE, m_itemOfCreate);

   for (int i = 0; i < m_itemOfCreate.GetLength(); i=i+2)

       {

       CStringstr(m_itemOfCreate.GetAt(i));

       List.push_back(_wtoi(str));

       }

 

   //length

   if (m_numOfCreate != List.size())

       {

       AfxMessageBox(_T("Error!"));

       }

   else

       ListSize = List.size();

 

   for (int i = 0; i < ListSize; i++)

       {

 

       CStringtemp;

       temp.Format(_T("%d"),List.front());

       m_show.AppendFormat(temp);

       m_show.AppendFormat(_T(" "));

       List.pop_front();

       }

   SetDlgItemTextW(IDC_EDIT_SHOW,m_show);

 

 

   for (int i = 0; i <m_show.GetLength(); i = i + 2)

       {

       CStringstr(m_show.GetAt(i));

       List.push_back(_wtoi(str));

       }

   //TODO: 在此添加控件通知处理程序代码

}

voidCListDLg::OnBnClickedButtonChange()

   {

   UpdateData(TRUE);

   m_show =_T("");

   std::list<int>changTemp;

   for (int i = ListSize; i >m_sitOfChange; i--)

       {

       changTemp.push_back(List.back());

       List.pop_back();

       }

   changTemp.pop_back();

   List.push_back(m_itemOfChange);

   for (int i =m_sitOfChange+1; i < ListSize; i++)

       {

       List.push_back(changTemp.back());

       changTemp.pop_back();

       }

 

   std::list<int>showTemp;

   for (int i = 0; i <ListSize; i++)

       {

       showTemp.push_back(List.front());

       List.pop_front();

       }

 

   for (int i = 0; i < ListSize; i++)

       {

       CStringtemp;

       temp.Format(_T("%d"),showTemp.front());

       List.push_back(showTemp.front());

       m_show.AppendFormat(temp);

       m_show.AppendFormat(_T(" "));

       showTemp.pop_front();

       }

 

   SetDlgItemTextW(IDC_EDIT_SHOW,m_show);

   //TODO: 在此添加控件通知处理程序代码

}

voidCListDLg::OnBnClickedButtonAdd()

   {

   //TODO: 在此添加控件通知处理程序代码

   UpdateData(TRUE);

   m_show =_T("");

   std::list<int>addTemp;

   ListSize = List.size();

   for (int i = ListSize; i >m_sitOfAdd ; i--)

       {

       addTemp.push_back(List.back());

       List.pop_back();

       }

   List.push_back(m_itemOfAdd);

   ListSize++;

   for (int i = List.size(); i < ListSize; i++)

       {

       List.push_back(addTemp.back());

       addTemp.pop_back();

       }

   std::list<int>showTemp;

   for (int i = 0; i <ListSize; i++)

       {

       showTemp.push_back(List.front());

       List.pop_front();

       }

 

   for (int i = 0; i < ListSize; i++)

       {

       CStringtemp;

       temp.Format(_T("%d"),showTemp.front());

       List.push_back(showTemp.front());

       m_show.AppendFormat(temp);

       m_show.AppendFormat(_T(" "));

       showTemp.pop_front();

       }

 

   SetDlgItemTextW(IDC_EDIT_SHOW,m_show);

 

}

voidCListDLg::OnBnClickedButtonDelete()

   {

   UpdateData(TRUE);

   std::list<int>deleteTemp;

   m_show =_T("");

   for (int i = ListSize; i >m_sitOfDelete; i--)

       {

       deleteTemp.push_back(List.back());

       List.pop_back();

       }

   deleteTemp.pop_back();

   ListSize--;

   for (int i = m_sitOfDelete; i <ListSize; i++)

       {

       List.push_back(deleteTemp.back());

       deleteTemp.pop_back();

       }

   std::list<int>showTemp;

   for (int i = 0; i <ListSize; i++)

       {

       showTemp.push_back(List.front());

       List.pop_front();

       }

 

   for (int i = 0; i < ListSize; i++)

       {

       CStringtemp;

       temp.Format(_T("%d"),showTemp.front());

       List.push_back(showTemp.front());

       m_show.AppendFormat(temp);

       m_show.AppendFormat(_T(" "));

       showTemp.pop_front();

       }

 

   SetDlgItemTextW(IDC_EDIT_SHOW,m_show);

   //TODO: 在此添加控件通知处理程序代码

}

6.PolynomialDlg的主要实现

voidCPolynomialDlg::OnBnClickedButtonAdd()

   {

   GetDlgItemTextW(IDC_EDIT_POLYNOMIAL1, m_polynomial1);

   for (int i = 0; i<m_polynomial1.GetLength();i=i+2)

       {

       CStringstr1(m_polynomial1.GetAt(i));

       p1.push_back(_wtoi(str1));

       }

 

   GetDlgItemTextW(IDC_EDIT_POLYNOMIAL2, m_polynomial2);

   for (int i = 0; i<m_polynomial2.GetLength(); i = i + 2)

       {

       CStringstr2(m_polynomial2.GetAt(i));

       p2.push_back(_wtoi(str2));

       }

   

   int p1s = p1.size();

   int p2s = p2.size();

   for (int i = 0; i < ((p1s < p2s) ? p1s : p2s); i++)

       {

       result.push_back(p1.front() +p2.front());

       p1.pop_front();

       p2.pop_front();

       }

   if (p1s<p2s)

       for (int i =p1s; i < p2s; i++)

           {

           result.push_back(p2.front());

           p2.pop_front();

           }

   if (p1s>p2s)

       for (int i = p2s; i < p1s; i++)

           {

           result.push_back(p1.front());

           p1.pop_front();

           }

   int resultSize = result.size();

   for (int i = 0; i < resultSize; i++)

       {

       CStringtemp;

       temp.Format(_T("%d"),result.front());

       m_result.AppendFormat(temp);

       m_result.AppendFormat(_T(" "));

       result.pop_front();

       }

   AfxMessageBox(m_result);

   

   SetDlgItemTextW(IDC_EDIT_RESULT,m_result);

   UpdateData(TRUE);

   //TODO: 在此添加控件通知处理程序代码

   }

7.USESTACK的主要实现

voidCUSESTACK::OnBnClickedButtonTypetransfer16()

   {

   //TODO: 在此添加控件通知处理程序代码

   m_typeTransferOutput =_T("");

   int temp16 = m_typeTransferInput;

   while (temp16 != 0)

       {

       t16.push(temp16 % 16);

       temp16 = temp16 / 16;

       }

   while (!t16.empty())

       {

       if(t16.top()<10)

           {

           m_typeTransferOutput.AppendFormat(_T("%d"), t16.top());

           }

       else

           {

           switch(t16.top())

               {

                   case 10:

                       m_typeTransferOutput.AppendFormat(_T("A"));

                       break;

                   case 11:

                       m_typeTransferOutput.AppendFormat(_T("B"));

                       break;

                   case 12:

                       m_typeTransferOutput.AppendFormat(_T("C"));

                       break;

                   case 13:

                       m_typeTransferOutput.AppendFormat(_T("D"));

                       break;

                   case 14:

                       m_typeTransferOutput.AppendFormat(_T("E"));

                       break;

                   case 15:

                       m_typeTransferOutput.AppendFormat(_T("F"));

                       break;

                   default:

                       break;

               }

           }

       t16.pop();

       }

   SetDlgItemTextW(IDC_EDIT_TYPETRANSFEROUTPUT, m_typeTransferOutput);

   }

其他进制同理。

voidCUSESTACK::OnBnClickedButtonEvaluation()

   {

   //TODO: 在此添加控件通知处理程序代码

   m_evaluationOutput = 0;

 

   for (int i = m_evaluationInput.GetLength(); i>=0; i--)

       {

       CStringevaluationTemp(m_evaluationInput.GetAt(i));

       if(evaluationTemp ==_T("+"))

           {

           operate.push('+');

           }

       if(evaluationTemp ==_T("-"))

           {

           operate.push('-');

           }

       if(evaluationTemp ==_T("*"))

           {

           operate.push('*');

           }

       if(evaluationTemp ==_T("/"))

           {

           operate.push('/');

           }

       if(evaluationTemp ==_T("1"))

           {

           stdvalue.push(1);

           }

       if(evaluationTemp ==_T("2"))

           {

           stdvalue.push(2);

           }

       if(evaluationTemp ==_T("3"))

           {

           stdvalue.push(3);

           }

       if(evaluationTemp ==_T("4"))

           {

           stdvalue.push(4);

           }

       if(evaluationTemp ==_T("5"))

           {

           stdvalue.push(5);

           }

       if(evaluationTemp ==_T("6"))

           {

           stdvalue.push(6);

           }

       if(evaluationTemp ==_T("7"))

           {

           stdvalue.push(7);

           }

 

       if(evaluationTemp ==_T("8"))

           {

           stdvalue.push(8);

           }

       

       if(evaluationTemp ==_T("9"))

           {

           stdvalue.push(9);

           }

       

       if(evaluationTemp ==_T("0"))

           {

           stdvalue.push(0);

           }

       }

   std::stack<int>valueTemp;

   std::stack<char>operateTemp;

   while (!operate.empty())

       {

       if(!operate.empty()&&(operate.top() =='*' ||operate.top() =='/'))

           {

           valueTemp.push(stdvalue.top());

           operateTemp.push(operate.top());

           stdvalue.pop();

           operate.pop();

           if(operateTemp.top() =='*')

               m_evaluationOutput =valueTemp.top()*stdvalue.top();

           if(operateTemp.top() =='/')

               {

               m_evaluationOutput =valueTemp.top()/stdvalue.top();

               }

           stdvalue.pop();

           valueTemp.pop();

           valueTemp.push(m_evaluationOutput);

           operateTemp.pop();

           }

       if(!operate.empty()&&(operate.top() =='+' ||operate.top() =='-'))

           {

           operateTemp.push(operate.top());

           operate.pop();

           if(valueTemp.empty())

               {

               valueTemp.push(stdvalue.top());

               stdvalue.pop();

               }

           if(!operate.empty()&&(operate.top() =='*' ||operate.top() =='/'))

               {

               valueTemp.push(stdvalue.top());

               stdvalue.pop();

               if(operate.top() =='*')

                   {

                   m_evaluationOutput=valueTemp.top()*stdvalue.top();

                   valueTemp.pop();

                   valueTemp.push(m_evaluationOutput);

                   }

               if(operate.top() =='/')

                   {

                   m_evaluationOutput =valueTemp.top()/stdvalue.top();

                   valueTemp.pop();

                   valueTemp.push(m_evaluationOutput);

                   operate.pop();

                   }

               }

           else

               {

               if(operateTemp.top() =='+')

                   {

                   m_evaluationOutput =valueTemp.top() + stdvalue.top();

                   valueTemp.pop();

                   stdvalue.pop();

                   valueTemp.push(m_evaluationOutput);

 

                   }

 

               if(operateTemp.top() =='-')

                   {

                   m_evaluationOutput =valueTemp.top() - stdvalue.top();

                   valueTemp.pop();

                   stdvalue.pop();

                   valueTemp.push(m_evaluationOutput);

                   }

               operateTemp.pop();

               }

           }

       }

   m_evaluationOutput = valueTemp.top();

 

   SetDlgItemInt(IDC_EDIT_EVALUATIONOUTPUT, m_evaluationOutput);

   }

voidCUSESTACK::OnBnClickedButtonBracematch()

   {

   //TODO: 在此添加控件通知处理程序代码

   for (int i = 0; i < m_BackEmachInput.GetLength(); i++)

       {

       CStringbackTemp(m_BackEmachInput.GetAt(i));

       if(backTemp==_T("("))

           {

           back.push(i);

           }

       if(backTemp ==_T(")"))

           {

           if(back.empty())

               {

               AfxMessageBox(_T("括号不匹配!"));

               break;

               }

 

           back.pop();

           

           }

 

       }

   if (back.empty())

       AfxMessageBox(_T("括号匹配!"));

   else

       {

   AfxMessageBox(_T("括号不匹配!"));

 

       }

   }

8.SortDlg的主要实现

#defineRESULT SetDlgItemTextW(IDC_EDIT_OUTPUTSORT,m_outputSort)

 

 

voidCSortDlg::OnBnClickedButtonDirectinsertsort()

   {

   GetDlgItemTextW(IDC_EDIT_INPUTSORT, m_inputSort);

   int sortSize = m_inputSort.GetLength() / 2 + 1;

   int sortArray[20];

   for (int i= 0; i<m_inputSort.GetLength() ; i=i+2)

       {

       CStringtempSort(m_inputSort.GetAt(i));

       if(tempSort !=_T(""))

           {

           sortArray[i/2] = _wtoi(tempSort);

           }

       }

   std::sort(sortArray, sortArray+sortSize);

   for (int i = 0; i <sortSize; i++)

       {

       m_outputSort.AppendFormat(_T("%d"),sortArray[i]);

       m_outputSort.AppendFormat(_T(" "));

       }

   //TODO: 在此添加控件通知处理程序代码

 

   RESULT;

   }

 

三.运行结果

1.  线性表的表示和实现

2.  一元多项式的相加


3.  栈的应用举例USESTACT

4.  排序SortDlg实现

五.总结报告

      这次课程设计,让我对数据结构,MFC有了更深的认识,同时可以熟练地使用相关类库。



发布了28 篇原创文章 · 获赞 3 · 访问量 5万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览