1、 /后继指针 Node() pre=NULL; next=NULL; Node(element e) ele=e; Node * MakeNode(element e)/传入参数返回一个节点指针,实现参数的封装。 Node *temp=new Node(e); return temp;MyListStack链栈class MyListStack Node *base; *top; int index; MyListStack() /初始化链表 base=new Node(); top=base; index=0;void push(element n) /push *temp=new Node
2、next=temp; temp-pre=top; top=temp; index+; void pop(element & out) /pop out=top-ele; top=top-pre; delete top-next;next=NULL; index-; BOOL isEmpty(); /返回栈是否为空 if(index) return FALSE; else return TRUE; virtual MyListStack() /析构链栈,释放空间。 *p=base; *q=p- while(p-next!=NULL) delete p; p=q; q=p- delete p;(2
3、)涉及的操作void CKuohaopipeiDlg:OnButtonClear() /清空窗口控件。OnButtonSlowShow( /慢速显示运行过程。OnOK() /进行括号匹配过程。OnSelchangeListInfo() /此函数响应列表框中光标改变的消息,定位错误的位置。2.3实现注释(此部分见源代码中注释)2.4详细设计表示(1)流程示意图图表 1 匹配流程示意图(2)功能示意图图表 2 功能示意图3用户手册 (1)在编辑框中输入表达式串。 (2)点击开始匹配测试则进行快速匹配。 (3)点击慢速运行。 (4)点击错误信息,定位错误字符。4调试报告 输入:1+1+(A+d) 结
4、果:正确) 结果:错误() 结果:() 结果:5.源代码及运行结果截图(1)源代码OnOK() / TODO: Add extra validation here UpdateData(TRUE); /MyListStack ls; m_list_info.ResetContent(); /清空list m_list_stack.ResetContent(); BOOL isNum=0; BOOL isError=0; int inputlength=m_cstring_input.GetLength(); if (inputlength=0) MessageBox(输入值不能为空!,提示);
5、 /如果字符串为空 else for(int j=0;jinputlength;j+) /获取输入字串的长度 char str_in=m_cstring_input.GetAt(j); if (str_in=(|str_in=) isNum=1; ls.push(str_in); /若为左括号则入栈 CString temp; temp.Format(push %c into stack rn,str_in); m_list_info.AddString(temp); else if(str_in=) /若为右括号则出栈匹配 char str_out; if (!ls.isEmpty() ls
6、.pop(str_out); BOOL flag=0; if (str_in=&str_out=) flag=1; if (!flag /如果匹配不成功,则isError变量为1,并更新提示信息。 isError=1; CString temp; temp.Format(push %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
7、_list_info.AddString(temp); else /如果当前栈为空,且输入为右括号时,isError为1,并更新控件信息。 isError=1; CString temp; temp.Format(can not match %c m_list_info.AddString(temp); m_list_info.RedrawWindow(); UpdateData(FALSE); if (!ls.isEmpty()/如果栈最后不为空,且栈中还有左括号,则提示栈中左括号没有匹配完成。 char temp; ls.pop(temp); ls.push(temp); if (temp
8、=|temp= temp=stack is not empty SetFocus();SetSel(0,-1);OnButtonSlowShow() /慢速显示运行过程 Add your control notification handler code here / TODO: m_list_info.SetRedraw(); m_list_stack.SetRedraw(); /判断是否出现括号 /判断是否出现错误 /获取输入字串长度j+) /逐次获取字符) /如果为左括弧,则入栈。 m_list_info.SetCurSel(m_list_info.GetCount()-1);%c m_
9、list_stack.AddString(temp);) /如果为右括弧 /pop括号,判断是否匹配flag) /如果不匹配,则此变量为真,便于函数末尾判断。 /将信息传入控件 m_list_stack.DeleteString(m_list_stack.GetCount()-1); m_list_info.SetCurSel(m_list_info.GetCount()-1); else /如果匹配成功,将信息显示到控件。 m_list_stack.DeleteString(m_list_stack.GetCount()-1); m_list_info.SetCurSel(m_list_in
10、fo.GetCount()-1); else /如果栈中无括号了,且输入括号为右括号。 m_list_info.SetCurSel(m_list_info.GetCount()-1); /重画控件 m_list_stack.RedrawWindow(); Sleep(600);ls.isEmpty() /如果栈中还有左括号,说明右括号的数量小于左括号的数量。 /提示栈中还有未匹配的左括号。while (ls.index!=0) /清空栈中的内容OnButtonClear() SetWindowText( /此函数中清空所有编辑框和listbox中的记录 m_list_info.RedrawWindow(); /重画所有的控件 m_list_stack.RedrawWindow();OnSelchangeListInfo() /此函数响应列表框中光标改变的消息,定位错误的位置。 int i; i=m_list_info.GetCurSel(); if(i!=LB_ERR&i /定位错误 (CEdit*)GetDlgItem(IDC_EDIT_INPUT)-SetSel(i,i+1);(2)运行截图图表 3 运行效果图1图表 4 运行效果图2
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1