ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:86.14KB ,
资源ID:4219351      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4219351.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据结构括号匹配实验讲义.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

数据结构括号匹配实验讲义.docx

1、数据结构括号匹配实验讲义括号的匹配1. 需求和规格说明(1)实现括号的是否匹配的判定。(2)实现匹配错误的提示。(3)实现栈内容的动态显示。2设计2.1设计思想(1)对于括号匹配的判定,首先输入字符串到缓冲区。逐个字符读取字串,遇到的是左括号则入栈,若是右括号,则出栈。出栈的左括号如果和右括号匹配,则一对括号匹配成功;否则,这对括号匹配失败,并给出错误提示。(2)分析括号匹配错误出现的情况,主要有三种:左括号数大于右括号数,左括号与右括号不匹配,右括号数大于左括号数。根据栈的存储情况就能判定出这三种情况,并且实时的将信息放映到可视化控件上。(3) 对于匹配过程和栈内容的动态显示,可以用list

2、box控件实时的显示和更新。窗口上有两个listbox控件,第一个动态显示push和pop动作以及提示错误信息;第二个listbox则动态模拟栈内的存储情况。2.2设计表示(1)存储结构Node节点template class Node public: element ele; Node *pre; /前驱指针 Node *next; /后继指针 Node() pre=NULL; next=NULL; Node(element e) ele=e; pre=NULL; next=NULL; Node * MakeNode(element e)/传入参数返回一个节点指针,实现参数的封装。 Node

3、 *temp=new Node(e); return temp; ;MyListStack链栈template class MyListStack public: Node *base; Node *top; int index; MyListStack() /初始化链表 base=new Node(); top=base; index=0; void push(element n) /push Node *temp=new Node(n); top-next=temp; temp-pre=top; top=temp; index+; void pop(element & out) /pop

4、out=top-ele; top=top-pre; delete top-next; top-next=NULL; index-; BOOL isEmpty(); /返回栈是否为空 if(index) return FALSE; else return TRUE; virtual MyListStack() /析构链栈,释放空间。 Node *p=base; Node *q=p-next; while(p-next!=NULL) delete p; p=q; q=p-next; delete p; ;(2)涉及的操作void CKuohaopipeiDlg:OnButtonClear() /清

5、空窗口控件。void CKuohaopipeiDlg:OnButtonSlowShow( /慢速显示运行过程。void CKuohaopipeiDlg:OnOK() /进行括号匹配过程。void CKuohaopipeiDlg:OnSelchangeListInfo() /此函数响应列表框中光标改变的消息,定位错误的位置。2.3实现注释(此部分见源代码中注释)2.4详细设计表示(1)流程示意图 图表 1 匹配流程示意图(2)功能示意图 图表 2 功能示意图3用户手册 (1)在编辑框中输入表达式串。 (2)点击开始匹配测试则进行快速匹配。 (3)点击慢速运行。 (4)点击错误信息,定位错误字符。

6、4调试报告 输入:1+1+(A+d) 结果:正确 输入:) 结果:错误 输入:() 结果:错误 输入:() 结果:错误 5.源代码及运行结果截图(1)源代码void CKuohaopipeiDlg:OnOK() / TODO: Add extra validation here UpdateData(TRUE); /MyListStack ls; m_list_info.ResetContent(); /清空list m_list_stack.ResetContent(); /清空list BOOL isNum=0; BOOL isError=0; int inputlength=m_cstr

7、ing_input.GetLength(); if (inputlength=0) MessageBox(输入值不能为空!,提示); /如果字符串为空 else for(int j=0;jinputlength;j+) /获取输入字串的长度 char str_in=m_cstring_input.GetAt(j); if (str_in=(|str_in=|str_in=) isNum=1; ls.push(str_in); /若为左括号则入栈 CString temp; temp.Format(push %c into stack rn,str_in); m_list_info.AddStr

8、ing(temp); else if(str_in=)|str_in=|str_in=) /若为右括号则出栈匹配 isNum=1; char str_out; if (!ls.isEmpty() ls.pop(str_out); BOOL flag=0; if (str_in=)&str_out=() flag=1; if (str_in=&str_out=) flag=1; if (str_in=&str_out=) flag=1; if (!flag /如果匹配不成功,则isError变量为1,并更新提示信息。 isError=1; CString temp; temp.Format(pu

9、sh %c but can not match %c -error!,str_out,str_in); m_list_info.AddString(temp); else /如果匹配成功,更新提示信息。 CString temp; temp.Format(pop %c out of stack rn,str_out); m_list_info.AddString(temp); else /如果当前栈为空,且输入为右括号时,isError为1,并更新控件信息。 isError=1; CString temp; temp.Format(can not match %c -error!,str_in

10、); m_list_info.AddString(temp); m_list_info.RedrawWindow(); UpdateData(FALSE); if (!ls.isEmpty()/如果栈最后不为空,且栈中还有左括号,则提示栈中左括号没有匹配完成。 char temp; ls.pop(temp); ls.push(temp); if (temp=(|temp=|temp=) CString temp; temp=stack is not empty SetFocus(); (CEdit*)GetDlgItem(IDC_EDIT_INPUT)-SetSel(0,-1);void CK

11、uohaopipeiDlg:OnButtonSlowShow() /慢速显示运行过程 / TODO: Add your control notification handler code here / TODO: Add extra validation here m_list_info.SetRedraw(); m_list_stack.SetRedraw(); UpdateData(TRUE); m_list_info.ResetContent(); m_list_stack.ResetContent(); BOOL isNum=0; /判断是否出现括号 BOOL isError=0; /

12、判断是否出现错误 int inputlength=m_cstring_input.GetLength(); /获取输入字串长度 if (inputlength=0) MessageBox(输入值不能为空!,提示); else for(int j=0;jinputlength;j+) /逐次获取字符 char str_in=m_cstring_input.GetAt(j); if (str_in=(|str_in=|str_in=) /如果为左括弧,则入栈。 isNum=1; ls.push(str_in); CString temp; temp.Format(push %c into stac

13、k rn,str_in); m_list_info.AddString(temp); m_list_info.SetCurSel(m_list_info.GetCount()-1); temp.Format(%c,str_in); m_list_stack.AddString(temp); else if(str_in=)|str_in=|str_in=) /如果为右括弧 isNum=1; char str_out; if (!ls.isEmpty() ls.pop(str_out); /pop括号,判断是否匹配 BOOL flag=0; if (str_in=)&str_out=() fla

14、g=1; if (str_in=&str_out=) flag=1; if (str_in=&str_out=) flag=1; if (!flag) isError=1; /如果不匹配,则此变量为真,便于函数末尾判断。 CString temp; temp.Format(push %c but can not match %c -error!,str_out,str_in); /将信息传入控件 m_list_info.AddString(temp); m_list_stack.DeleteString(m_list_stack.GetCount()-1); m_list_info.SetCu

15、rSel(m_list_info.GetCount()-1); else /如果匹配成功,将信息显示到控件。 CString temp; temp.Format(pop %c out of stack rn,str_out); m_list_info.AddString(temp); m_list_stack.DeleteString(m_list_stack.GetCount()-1); m_list_info.SetCurSel(m_list_info.GetCount()-1); else /如果栈中无括号了,且输入括号为右括号。 isError=1; CString temp; tem

16、p.Format(can not match %c -error!,str_in); m_list_info.AddString(temp); m_list_info.SetCurSel(m_list_info.GetCount()-1); m_list_info.RedrawWindow(); /重画控件 m_list_stack.RedrawWindow(); Sleep(600); UpdateData(FALSE); if (!ls.isEmpty() /如果栈中还有左括号,说明右括号的数量小于左括号的数量。 char temp; ls.pop(temp); ls.push(temp)

17、; if (temp=(|temp=|temp=) CString temp; temp=stack is not empty SetFocus(); (CEdit*)GetDlgItem(IDC_EDIT_INPUT)-SetSel(0,-1); void CKuohaopipeiDlg:OnButtonClear() / TODO: Add your control notification handler code here (CEdit*)GetDlgItem(IDC_EDIT_INPUT)-SetWindowText(); /此函数中清空所有编辑框和listbox中的记录 m_lis

18、t_info.ResetContent(); m_list_stack.ResetContent(); m_list_info.RedrawWindow(); /重画所有的控件 m_list_stack.RedrawWindow();void CKuohaopipeiDlg:OnSelchangeListInfo() /此函数响应列表框中光标改变的消息,定位错误的位置。 / TODO: Add your control notification handler code here int i; i=m_list_info.GetCurSel(); if(i!=LB_ERR&iSetFocus(); /定位错误 (CEdit*)GetDlgItem(IDC_EDIT_INPUT)-SetSel(i,i+1); else (CEdit*)GetDlgItem(IDC_EDIT_INPUT)-SetFocus(); /定位错误 (CEdit*)GetDlgItem(IDC_EDIT_INPUT)-SetSel(0,-1); (2)运行截图 图表 3 运行效果图1图表 4 运行效果图2

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1