版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、计算机专业软件类课程实验报告课程名称:编译原理实验题目:中缀表达式转为后缀表达式实验小组成员:实验小组组长:任课教师:专业名称:计算机科学与技术班级名称:计科1班实验起止时间:2014-6-22014-6-3一、实验目的1、要求设计交互界面,能输入能转换能输出,形式和风格自定。2、掌握栈“后进先出”的特点。 3、掌握栈的典型应用中缀表达式转后缀表达式,并利用后缀表达式求值。 2、 实验内容1、 设计一个计算器,能够进行界面交互2、 能够将输入的中缀表达式转换为正确的后缀表达式,3、 根据得到的后缀表达式,求出表达式的值三、实验需求1、界面部分:可以在界面上单击任何一个按钮,
2、将在中缀表达式文本框中时刻显示输入的表达式,后缀表达式文本框显示后缀表达式,结果文本框显示计算结果2、 算法部分:将中缀表达式文本框中的字符串读出,并转换成后缀表达式,转换后,可根据后缀表达式求出表达式的值4、 主要数据结构介绍1、 程序中创建两个队列Queue,一个队列用来存放中缀表达式,另一个队列用来存放后缀表达式;2、同时,程序中需要创建两个栈Stack,一个在表达式转换时,用来存放运算符,一个在后缀表达式求值时,用来存放操作数5、 主要模块算法介绍1、中缀表达式转为后缀表达式(1)若取出的字符是数字,则分析出完整的运算数,该运算数直接送入S2栈 (2)若取出的字符是运算符,则将该运算符
3、与S1栈栈顶元素比较,如果该运算符优先级大于S1栈栈顶运算符优先级,则将该运算符进S1栈,否者,将S1栈的栈顶运算符弹出,送入S2栈中,直至S1栈栈顶运算符低于(不包括等于)该运算符优先级,则将该运算符送入S1栈。 (3)若取出的字符是“(”,则直接送入S1栈栈顶。 (4)若取出的字符是“)”,则将距离S1栈栈顶最近的“(”之间的运算符,逐个出栈,依次送入S2栈,此时抛弃“(”。 (5) 重复上面的14步,直至处理完所有的输入字符 (6)若所有的字符都以取出,则将S1栈内所有运算符,逐个出栈,依次送入S2栈。 完成以上步骤,S2栈便为后缀表达式输出结果。2、 后缀表达式求值(1)定义一个dou
4、ble型的运算数栈,将中缀表达式转换得到的后缀表达式字符串自左向右依次读入。 (2)如果读入的是操作数,将该操作数(将自动类型转换为double型)直接进入运算数栈。 (3)如果读入的是运算符,则立即从运算数栈中弹出两个运算数,计算两个运算数运算后的值(运算时先出栈的元素放在运算符后面,后出栈的元素放在运算符前面),并将计算结果存回运算数栈。 (4)重复、步,直到后缀表达式结束,最后栈中保存的那个数即为该后缀表达式的计算结果。 6、 程序实现环境及使用说明本次实验采用Eclipse进行代码的编写、编译及运行;编写语言为java语言;程序的运行环境为jdk
5、1.8;系统为windows 8.17、 实验测试用例设计说明1、 简单的测试,没有括号,负数,小数输入1+2*3输出123*+计算结果7.02、 错误的测试,除数为0输入10/0输出100/计算结果Infinity3、复杂的测试,含有括号,负数,小数输入-9.5+(-3-1)*3+10/2输出-9.5-31-3*+102/+计算结果-16.5八、实验结果测试情况 9、 小组对实验结果的自我评价通过这次实验,我对中缀表达式和后缀表达式有了一定的了解,进一步的巩固了这部分的知识。懂得了中缀表达式转为后缀表达式的工作原理。在编程过程中,多多少少遇到了一些小问题,比如,负号的处理,到底是将
6、负号与操作数作为一个整体进行处理,还是将负号与运算符做一样的处理。本人经过多次测试,还是将负号与操作数作为一个整体进行处理,这样比较方便一些。在程序上虽然实现了要求的功能,但是对输入非法的处理可能有一些欠缺。由于编程能力和时间的不足,这个计算器还有待完善,功能相对较少。望老师多多指教。十、任课教师对实验结果的评分源码MyFrame.javapackage view;import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.GridLayout;import java.awt.event.ActionEvent
7、;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;import operation.Conversion;public class MyFrame extends JFrame private JPanel contentPan
8、e;private JTextField textField;private JTextField textField_1;private JTextField textField_2;String expression=""/* * Launch the application. */public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try MyFrame frame = new MyFrame();frame.setTitle(&quo
9、t;中后缀表达式转换");frame.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create the frame. */public MyFrame() setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 259, 364);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5);setContentPane(conte
10、ntPane);contentPane.setLayout(new GridLayout(8, 1, 0, 0);JPanel panel = new JPanel();contentPane.add(panel);panel.setLayout(new BorderLayout(0, 0);JLabel lblNewLabel = new JLabel("中缀表达式:");panel.add(lblNewLabel, BorderLayout.WEST);textField = new JTextField();textField.setEditable(false);p
11、anel.add(textField, BorderLayout.CENTER);textField.setColumns(10);JPanel panel_1 = new JPanel();contentPane.add(panel_1);panel_1.setLayout(new BorderLayout(0, 0);JLabel label = new JLabel("后缀表达式:");panel_1.add(label, BorderLayout.WEST);textField_1 = new JTextField();textField_1.setEditable
12、(false);panel_1.add(textField_1, BorderLayout.CENTER);textField_1.setColumns(10);JPanel panel_2 = new JPanel();contentPane.add(panel_2);panel_2.setLayout(new BorderLayout(0, 0);JLabel label_1 = new JLabel("计算结果是:");panel_2.add(label_1, BorderLayout.WEST);textField_2 = new JTextField();text
13、Field_2.setEditable(false);panel_2.add(textField_2, BorderLayout.CENTER);textField_2.setColumns(10);JPanel panel_3 = new JPanel();contentPane.add(panel_3);panel_3.setLayout(new GridLayout(0, 4, 0, 0);JButton button = new JButton("1");button.addActionListener(new ActionListener() public voi
14、d actionPerformed(ActionEvent arg0) expression += button.getText();textField.setText(expression););panel_3.add(button);JButton button_1 = new JButton("2");button_1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_1.getText();textFiel
15、d.setText(expression););panel_3.add(button_1);JButton button_2 = new JButton("3");button_2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_2.getText();textField.setText(expression););panel_3.add(button_2);JButton button_3 = new JBut
16、ton("+");button_3.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_3.getText();textField.setText(expression););panel_3.add(button_3);JPanel panel_4 = new JPanel();contentPane.add(panel_4);panel_4.setLayout(new GridLayout(1, 0, 0, 0);
17、JButton button_4 = new JButton("4");button_4.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_4.getText();textField.setText(expression););panel_4.add(button_4);JButton button_5 = new JButton("5");button_5.addActionListener(
18、new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_5.getText();textField.setText(expression););panel_4.add(button_5);JButton button_6 = new JButton("6");button_6.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expres
19、sion += button_6.getText();textField.setText(expression););panel_4.add(button_6);JButton button_7 = new JButton("-");button_7.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_7.getText();textField.setText(expression););panel_4.add(bu
20、tton_7);JPanel panel_5 = new JPanel();contentPane.add(panel_5);panel_5.setLayout(new GridLayout(1, 0, 0, 0);JButton button_8 = new JButton("7");button_8.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_8.getText();textField.setText(e
21、xpression););panel_5.add(button_8);JButton button_9 = new JButton("8");button_9.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_9.getText();textField.setText(expression););panel_5.add(button_9);JButton button_10 = new JButton("
22、9");button_10.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_10.getText();textField.setText(expression););panel_5.add(button_10);JButton button_11 = new JButton("*");button_11.addActionListener(new ActionListener() public void
23、 actionPerformed(ActionEvent arg0) expression += button_11.getText();textField.setText(expression););panel_5.add(button_11);JPanel panel_6 = new JPanel();contentPane.add(panel_6);panel_6.setLayout(new GridLayout(1, 0, 0, 0);JButton button_12 = new JButton("0");button_12.addActionListener(n
24、ew ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_12.getText();textField.setText(expression););panel_6.add(button_12);JButton button_13 = new JButton("(");button_13.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) exp
25、ression += button_13.getText();textField.setText(expression););panel_6.add(button_13);JButton button_14 = new JButton(")");button_14.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_14.getText();textField.setText(expression););panel_
26、6.add(button_14);JButton button_15 = new JButton("/");button_15.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_15.getText();textField.setText(expression););panel_6.add(button_15);JPanel panel_7 = new JPanel();contentPane.add(panel_
27、7);panel_7.setLayout(new GridLayout(1, 0, 0, 0);JButton button_16 = new JButton(".");button_16.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression += button_16.getText();textField.setText(expression););panel_7.add(button_16);JButton btnNewButton
28、= new JButton("=");btnNewButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) /System.out.println(expression);Conversion cv = new Conversion(expression);cv.calculate();textField.setText(expression);textField_1.setText(cv.houzhui);textField_2.setText(c
29、v.resultstr););panel_7.add(btnNewButton);JButton btnC = new JButton("C");btnC.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) expression = ""textField.setText(expression);textField_1.setText("");textField_2.setText(""););pa
30、nel_7.add(btnC);Conversion.javapackage operation;import java.util.Iterator;import java.util.LinkedList;import java.util.Queue;import java.util.Stack;public class Conversion char ch;public Conversion(String infix)ch=infix.toCharArray();Queue<String> readin=new LinkedList<String>();Queue&l
31、t;String> writeout=new LinkedList<String>();Stack<String> fuhao=new Stack<String>();public String houzhui=""public String resultstr=""public void init() /String infix="-9.5+(-3-1)*3+10/2"/中缀表达式/String suffix="-9.5 -3 1-3*+10 2/+"/后缀表达式/cha
32、r ch=infix.toCharArray();String temp=""int t=0;/对第一字符是'-'进行特殊处理if(ch0='-'&&ch1>='0'&&ch1<='9')temp=temp+String.valueOf(ch0);t=1;while(cht>='0'&&cht<='9'|cht='.')temp=temp+String.valueOf(cht);t+;read
33、in.add(temp);temp=""for (int i = t; i < ch.length; i+) /对负数进行处理if(chi='-'&&(chi-1='+'|chi-1='-'|chi-1='*'|chi-1='/'|chi-1='('|chi-1=')')temp=temp+String.valueOf(chi);i+;while(chi>='0'&&chi<='9'
34、|chi='.')temp=temp+String.valueOf(chi);i+;if(!temp.equals("")readin.add(temp);temp=""/负数后的符号进行处理if(chi='+'|chi='-'|chi='*'|chi='/'|chi='('|chi=')')i-;elseif(chi='+'|chi='-'|chi='*'|chi='/'|chi
35、='('|chi=')')if(!temp.equals("")readin.add(temp);temp=""readin.add(String.valueOf(chi);elsetemp=temp+String.valueOf(chi);/最后一个字符串进行处理if(!temp.equals("")readin.add(temp);temp=""/Iterator<String> it=readin.iterator();/while(it.hasNext()/Sys
36、tem.out.println(it.next();/public void convert() init();Iterator<String> it=readin.iterator();while(it.hasNext()/System.out.println(it.next();String temp=it.next();/数字直接进栈if(!(temp.equals("+")|temp.equals("-")|temp.equals("*")|temp.equals("/")|temp.equal
37、s("(")|temp.equals(")")writeout.add(temp);else/栈为空时if(fuhao.isEmpty()fuhao.push(temp);else if(temp.equals("(")fuhao.push(temp);else if(temp.equals(")")while(!fuhao.isEmpty()&&(!fuhao.peek().equals("(")writeout.add(fuhao.pop();if(!fuhao.isEmpt
38、y()fuhao.pop();else/第一个参数是队列里的符号,第二个参数是栈里的符号/if(priority(temp, fuhao.peek()/fuhao.push(temp);/else/writeout.add(fuhao.pop();/while(!fuhao.isEmpty()&&(!priority(temp, fuhao.peek()writeout.add(fuhao.pop();fuhao.push(temp);while(!fuhao.isEmpty()writeout.add(fuhao.pop();Iterator<String> ou=writeout.iterator();while(ou.hasNext()houzhui+=ou.next();public void calculate()convert();String temp=""double shu1=0;double shu2=0;double result=0;char ch=new char1;Stack<Double> middleresult=new Stack<Double>();Iterator<String> out=writeout.iterator();while(ou
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 IEC 60601-2-52:2009 EN/FR Medical electrical equipment - Part 2-52: Particular requirements for the basic safety and essential performance of medical beds
- 【正版授权】 IEC 60601-2-37:2007/AMD1:2015 EN-FR Amendment 1 - Medical electrical equipment - Part 2-37: Particular requirements for the basic safety and essential performance of ultrasonic medica
- 【正版授权】 IEC 60601-2-33:2010+AMD1:2013+AMD2:2015 CSV EN-FR Medical electrical equipment - Part 2-33: Particular requirements for the basic safety and essential performance of magnetic resona
- 【正版授权】 IEC 60601-1-9:2007/AMD1:2013 EN-FR Amendment 1 - Medical electrical equipment - Part 1-9: General requirements for basic safety and essential performance - Collateral Standard: Requir
- 【正版授权】 IEC 60599:2022 CMV EN Mineral oil-filled electrical equipment in service - Guidance on the interpretation of dissolved and free gases analysis
- 【正版授权】 IEC 60598-2-22:2014 EN-FR Luminaires - Part 2-22: Particular requirements - Luminaires for emergency lighting
- 【正版授权】 IEC 60584-3:2021 EN-FR Thermocouples - Part 3: Extension and compensating cables - Tolerances and identification system
- 【正版授权】 IEC 60512-13-5:2006 EN-FR Connectors for electronic equipment - Tests and measurements - Part 13-5: Mechanical operation tests - Test 13e: Polarizing and keying method
- 【正版授权】 IEC 60512-11-12:2002 EN-FR Connectors for electronic equipment - Tests and measurements - Part 11-12: Climatic tests - Test 11m: Damp heat,cyclic
- 【正版授权】 IEC 60471:1977/AMD1:1980 EN-FR Amendment 1 - Dimensions of clevis and tongue couplings of string insulator units
- 2022-2023学年湖北省武汉市华中师大一附中高一(下)期末数学试卷
- 2024年中林集团雷州林业局限公司公开招聘岗位重点基础提升难、易点模拟试题(共500题)附带答案详解
- 2024年黑龙江省龙东地区部分学校结业三模地理试卷+
- 2024年湖北武汉市法院系统雇员制审判辅助人员招聘245人历年高频考题难、易错点模拟试题(共500题)附带答案详解
- 不离婚各过各的协议书
- 体育统计学(山东联盟)智慧树知到期末考试答案章节答案2024年齐鲁师范学院
- 2024疏浚淤泥固化筑堤技术规范
- 2024年山西运城护理职业学院招聘25人历年公开引进高层次人才和急需紧缺人才笔试参考题库(共500题)答案详解版
- 2024年四川凉山宁南县招聘事业单位工作人员33人公开引进高层次人才和急需紧缺人才笔试参考题库(共500题)答案详解版
- 中国与非洲文明交流互鉴智慧树知到期末考试答案章节答案2024年云南大学
- 安全检查操作手册
评论
0/150
提交评论