中缀表达式转为后缀的计算器Word文档格式.doc

上传人:聆听****声音 文档编号:1116545 上传时间:2023-04-30 格式:DOC 页数:17 大小:244KB
下载 相关 举报
中缀表达式转为后缀的计算器Word文档格式.doc_第1页
第1页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第2页
第2页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第3页
第3页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第4页
第4页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第5页
第5页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第6页
第6页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第7页
第7页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第8页
第8页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第9页
第9页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第10页
第10页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第11页
第11页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第12页
第12页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第13页
第13页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第14页
第14页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第15页
第15页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第16页
第16页 / 共17页
中缀表达式转为后缀的计算器Word文档格式.doc_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

中缀表达式转为后缀的计算器Word文档格式.doc

《中缀表达式转为后缀的计算器Word文档格式.doc》由会员分享,可在线阅读,更多相关《中缀表达式转为后缀的计算器Word文档格式.doc(17页珍藏版)》请在冰点文库上搜索。

中缀表达式转为后缀的计算器Word文档格式.doc

将中缀表达式文本框中的字符串读出,并转换成后缀表达式,转换后,可根据后缀表达式求出表达式的值

四、主要数据结构介绍

1、程序中创建两个队列Queue,一个队列用来存放中缀表达式,另一个队列用来存放后缀表达式;

2、同时,程序中需要创建两个栈Stack,一个在表达式转换时,用来存放运算符,一个在后缀表达式求值时,用来存放操作数

五、主要模块算法介绍

1、中缀表达式转为后缀表达式

(1)若取出的字符是数字,则分析出完整的运算数,该运算数直接送入S2栈

(2)若取出的字符是运算符,则将该运算符与S1栈栈顶元素比较,如果该运算符优先级大于S1栈栈顶运算符优先级,则将该运算符进S1栈,否者,将S1栈的栈顶运算符弹出,送入S2栈中,直至S1栈栈顶运算符低于(不包括等于)该运算符优先级,则将该运算符送入S1栈。

(3)若取出的字符是“(”,则直接送入S1栈栈顶。

(4)若取出的字符是“)”,则将距离S1栈栈顶最近的“(”之间的运算符,逐个出栈,依次送入S2栈,此时抛弃“(”。

(5)重复上面的1~4步,直至处理完所有的输入字符

(6)若所有的字符都以取出,则将S1栈内所有运算符,逐个出栈,依次送入S2栈。

完成以上步骤,S2栈便为后缀表达式输出结果。

2、后缀表达式求值

(1)定义一个double型的运算数栈,将中缀表达式转换得到的后缀表达式字符串自左向右依次读入。

(2)如果读入的是操作数,将该操作数(将自动类型转换为double型)直接进入运算数栈。

(3)如果读入的是运算符,则立即从运算数栈中弹出两个运算数,计算两个运

算数运算后的值(运算时先出栈的元素放在运算符后面,后出栈的元素放在运算符前面),并将计算结果存回运算数栈。

(4)重复②、③步,直到后缀表达式结束,最后栈中保存的那个数即为该后缀表达式的计算结果。

六、程序实现环境及使用说明

本次实验采用Eclipse进行代码的编写、编译及运行;

编写语言为java语言;

程序的运行环境为jdk1.8;

系统为windows 

8.1

七、实验测试用例设计说明

1、简单的测试,没有括号,负数,小数

输入1+2*3

输出123*+

计算结果7.0

2、错误的测试,除数为0

输入10/0

输出100/

计算结果Infinity

3、复杂的测试,含有括号,负数,小数

输入-9.5+(-3-1)*3+10/2

输出-9.5-31-3*+102/+

计算结果-16.5

八、实验结果测试情况

九、小组对实验结果的自我评价

通过这次实验,我对中缀表达式和后缀表达式有了一定的了解,进一步的巩固了这部分的知识。

懂得了中缀表达式转为后缀表达式的工作原理。

在编程过程中,多多少少遇到了一些小问题,比如,负号的处理,到底是将负号与操作数作为一个整体进行处理,还是将负号与运算符做一样的处理。

本人经过多次测试,还是将负号与操作数作为一个整体进行处理,这样比较方便一些。

在程序上虽然实现了要求的功能,但是对输入非法的处理可能有一些欠缺。

由于编程能力和时间的不足,这个计算器还有待完善,功能相对较少。

望老师多多指教。

十、任课教师对实验结果的评分

源码

MyFrame.java

packageview;

importjava.awt.BorderLayout;

importjava.awt.EventQueue;

importjava.awt.GridLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JPanel;

importjavax.swing.JTextField;

importjavax.swing.border.EmptyBorder;

importoperation.Conversion;

publicclassMyFrameextendsJFrame{

privateJPanelcontentPane;

privateJTextFieldtextField;

privateJTextFieldtextField_1;

privateJTextFieldtextField_2;

Stringexpression="

"

;

/**

*Launchtheapplication.

*/

publicstaticvoidmain(String[]args){

EventQueue.invokeLater(newRunnable(){

publicvoidrun(){

try{

MyFrameframe=newMyFrame();

frame.setTitle("

中后缀表达式转换"

);

frame.setVisible(true);

}catch(Exceptione){

e.printStackTrace();

}

}

});

}

*Createtheframe.

publicMyFrame(){

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100,100,259,364);

contentPane=newJPanel();

contentPane.setBorder(newEmptyBorder(5,5,5,5));

setContentPane(contentPane);

contentPane.setLayout(newGridLayout(8,1,0,0));

JPanelpanel=newJPanel();

contentPane.add(panel);

panel.setLayout(newBorderLayout(0,0));

JLabellblNewLabel=newJLabel("

中缀表达式:

panel.add(lblNewLabel,BorderLayout.WEST);

textField=newJTextField();

textField.setEditable(false);

panel.add(textField,BorderLayout.CENTER);

textField.setColumns(10);

JPanelpanel_1=newJPanel();

contentPane.add(panel_1);

panel_1.setLayout(newBorderLayout(0,0));

JLabellabel=newJLabel("

后缀表达式:

panel_1.add(label,BorderLayout.WEST);

textField_1=newJTextField();

textField_1.setEditable(false);

panel_1.add(textField_1,BorderLayout.CENTER);

textField_1.setColumns(10);

JPanelpanel_2=newJPanel();

contentPane.add(panel_2);

panel_2.setLayout(newBorderLayout(0,0));

JLabellabel_1=newJLabel("

计算结果是:

panel_2.add(label_1,BorderLayout.WEST);

textField_2=newJTextField();

textField_2.setEditable(false);

panel_2.add(textField_2,BorderLayout.CENTER);

textField_2.setColumns(10);

JPanelpanel_3=newJPanel();

contentPane.add(panel_3);

panel_3.setLayout(newGridLayout(0,4,0,0));

JButtonbutton=newJButton("

1"

button.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventarg0){

expression+=button.getText();

textField.setText(expression);

panel_3.add(button);

JButtonbutton_1=newJButton("

2"

button_1.addActionListener(newActionListener(){

expression+=button_1.getText();

panel_3.add(button_1);

JButtonbutton_2=newJButton("

3"

button_2.addActionListener(newActionListener(){

expression+=button_2.getText();

panel_3.add(button_2);

JButtonbutton_3=newJButton("

+"

button_3.addActionListener(newActionListener(){

expression+=button_3.getText();

panel_3.add(button_3);

JPanelpanel_4=newJPanel();

contentPane.add(panel_4);

panel_4.setLayout(newGridLayout(1,0,0,0));

JButtonbutton_4=newJButton("

4"

button_4.addActionListener(newActionListener(){

expression+=button_4.getText();

panel_4.add(button_4);

JButtonbutton_5=newJButton("

5"

button_5.addActionListener(newActionListener(){

expression+=button_5.getText();

panel_4.add(button_5);

JButtonbutton_6=newJButton("

6"

button_6.addActionListener(newActionListener(){

expression+=button_6.getText();

panel_4.add(button_6);

JButtonbutton_7=newJButton("

-"

button_7.addActionListener(newActionListener(){

expression+=button_7.getText();

panel_4.add(button_7);

JPanelpanel_5=newJPanel();

contentPane.add(panel_5);

panel_5.setLayout(newGridLayout(1,0,0,0));

JButtonbutton_8=newJButton("

7"

button_8.addActionListener(newActionListener(){

expression+=button_8.getText();

panel_5.add(button_8);

JButtonbutton_9=newJButton("

8"

button_9.addActionListener(newActionListener(){

expression+=button_9.getText();

panel_5.add(button_9);

JButtonbutton_10=newJButton("

9"

button_10.addActionListener(newActionListener(){

expression+=button_10.getText();

panel_5.add(button_10);

JButtonbutton_11=newJButton("

*"

button_11.addActionListener(newActionListener(){

expression+=button_11.getText();

panel_5.add(button_11);

JPanelpanel_6=newJPanel();

contentPane.add(panel_6);

panel_6.setLayout(newGridLayout(1,0,0,0));

JButtonbutton_12=newJButton("

0"

button_12.addActionListener(newActionListener(){

expression+=button_12.getText();

panel_6.add(button_12);

JButtonbutton_13=newJButton("

("

button_13.addActionListener(newActionListener(){

expression+=button_13.getText();

panel_6.add(button_13);

JButtonbutton_14=newJButton("

)"

button_14.addActionListener(newActionListener(){

expression+=button_14.getText();

panel_6.add(button_14);

JButtonbutton_15=newJButton("

/"

button_15.addActionListener(newActionListener(){

expression+=button_15.getText();

panel_6.add(button_15);

JPanelpanel_7=newJPanel();

contentPane.add(panel_7);

panel_7.setLayout(newGridLayout(1,0,0,0));

JButtonbutton_16=newJButton("

."

button_16.addActionListener(newActionListener(){

expression+=button_16.getText();

panel_7.add(button_16);

JButtonbtnNewButton=newJButton("

="

btnNewButton.addActionListener(newActionListener(){

//System.out.println(expression);

Conversioncv=newConversion(expression);

cv.calculate();

textField_1.setText(cv.houzhui);

textField_2.setText(cv.resultstr);

panel_7.add(btnNewButton);

JButtonbtnC=newJButton("

C"

btnC.addActionListener(newActionListener(){

expression="

textField_1.setText("

textField_2.setText("

panel

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2