实验三 逆波兰表达式的产生及计算

更新时间:2023-10-25 19:35:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

实验三 逆波兰表达式的产生及计算

一、实验目的

非后缀式用来表示的算术表达式转换为用逆波兰式来表示的算术表达式,并计算用逆波兰式来表示的算术表达式的值。

二、实验内容

将非后缀式用来表示的算术表达式转换为用逆波兰式来表示的算术表达式,并计算用逆波兰式来表示的算术表达式的值。

三、逆波兰表达式的产生及计算实验设计思想及算法

? 逆波兰式定义

将运算对象写在前面,而把运算符号写在后面。用这种表示法表示的表达式也称做后缀式。逆波兰式的特点在于运算对象顺序不变,运算符号位置反映运算顺序。

? 产生逆波兰式的前提 中缀算术表达式

? 逆波兰式生成的设计思想及算法

(1)首先构造一个运算符栈,此运算符在栈内遵循越往栈顶优先级越高的原则。 (2)读入一个用中缀表示的简单算术表达式,为方便起见,设该简单算术表达式的右端多加上了优先级最低的特殊符号“#”。

(3)从左至右扫描该算术表达式,从第一个字符开始判断,如果该字符是数字,则分析到该数字串的结束并将该数字串直接输出。

(4)如果不是数字,该字符则是运算符,此时需比较优先关系。

做法如下:将该字符与运算符栈顶的运算符的优先关系相比较。如果,该字符优先关系高于此运算符栈顶的运算符,则将该运算符入栈。倘若不是的话,则将此运算符栈顶的运算符从栈中弹出,将该字符入栈。

(5)重复上述操作(1)-(2)直至扫描完整个简单算术表达式,确定所有字符都得到正确处理,我们便可以将中缀式表示的简单算术表达式转化为逆波兰表示的简单算术表达式。

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

运用以上算法分析表达式(a+b*c)*d的过程如下: 当前符号 ( a + b * c ) ) ) * d 输入区 a+b*c)*d +b*c)*d *c)*d c)*d )*d *d *d *d d 符号栈 ( ( (+ (+ (+* (+* (+ ( * * 输出区 a a ab ab abc abc* abc* abc*+ abc*+ abc*+d abc*+d 江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

(1)构造一个栈,存放运算对象。

(2)读入一个用逆波兰式表示的简单算术表达式。

(3)自左至右扫描该简单算术表达式并判断该字符,如果该字符是运算对象,则将该字符入栈。若是运算符,如果此运算符是二目运算符,则将对栈顶部的两个运算对象进行该运算,将运算结果入栈,并且将执行该运算的两个运算对象从栈顶弹出。如果该字符是一目运算符,则对栈顶部的元素实施该运算,将该栈顶部的元素弹出,将运算结果入栈。

(4)重复上述操作直至扫描完整个简单算术表达式的逆波兰式,确定所有字符都得到正确处理,我们便可以求出该简单算术表达式的值。 ? 逆波兰式计算的设计思想及算法

四、实验要求

1、编程时注意编程风格:空行的使用、注释的使用、缩进的使用等。 2、如果遇到错误的表达式,应输出错误提示信息。 3、程序输入/输出实例:

? 输入以#结束的中缀表达式(包括+ - */()数字#)。

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

例:(1)(a+b) (2)(a+b*c) (3) B+(-(A))*C 输出逆波兰表达式的格式如下: (1) (a+b) ;→ab+)( (2)(a+b*c)→abc*+)(

(3)B+(-A(A)) *C→BA)(-)(C*+

? 输入中缀表达式并计算结果:a* (b+c)+(-d)#; 输出逆波兰式:abc+*d@+ 输入:a=3; b=1;c=2;d=5; 计算结果为:4

五、实验步骤

1、根据流程图编写出各个模块的源程序代码上机调试。

2、 编制好源程序后,设计若干用例对系统进行全面的上机测试,并通过所设计的逆波兰式的产生及计算程序;直至能够得到完全满意的结果。

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

3、书写实验报告 ;实验报告正文的内容:

? 描述逆波兰式的产生及计算程序的设计思想。

? 程序结构描述:函数调用格式、参数含义、返回值描述、函数功能;函数

之间的调用关系图。

? 详细的算法描述(程序执行流程图)。 ? 给出软件的测试方法和测试结果。

? 实验总结 (设计的特点、不足、收获与体会)。

六、源代码

1、操作类

package RPNAnalysis;

public class RPN_Operation {

private char[] expChar = new char[100];//表达式栈 private int expPointer = 0;//表达式栈指针

private char[] ariStack = new char[100];//运算符栈 private int top = 0;//运算符栈指针

private char[] exitChar = new char[100];//输出栈 private int exitPointer = 0;//输出栈指针

private String[][] dataStrings = new String[1000][5];//表格数据 private String tempString = new String();//临时字符串 private String result = new String();//结果字符串 private int row = 0;//表格数据行

private String compValue = new String();//计算值

private final char[] Arithmetic = {'+','-','*','/','(',')','#'};//运算符

private final char[] Letter =

{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'

,'q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L'

,'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};//字母表

private final char[] Digit =

{'1','2','3','4','5','6','7','8','9','0'};//数字表

public RPN_Operation() {

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

}

for (int i = 0; i < 1000; i++) { for (int j = 0; j < 5; j++) {

dataStrings[i][j] = new String(); dataStrings[i][j] = \; } }

//判断字符是否为字母

public boolean isLetter(char word) { int N = 0;

while (N < Letter.length) { if (word == Letter[N]) { return true; } N++; }

return false; }

//判断字符是否为数字

public boolean isDigit(char word) { int N = 0;

while (N < Digit.length) { if (word == Digit[N]) { return true; } N++; }

return false; }

//判断字符是否为算数运算符

public boolean isArithmetic(char word) { int N = 0;

while (N < Arithmetic.length) { if (word == Arithmetic[N]) { return true; } N++; }

return false; }

//输出剩余字符串

public void printExpChar() { tempString = new String();

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

for (int i = expPointer; i < expChar.length; i++) { tempString += expChar[i];

System.out.print(expChar[i]); result += expChar[i]; }

System.out.print(\); result += \; }

//输出符号栈

public void printAriStack() { tempString = new String();

for (int i = 0; i < top; i++) { tempString += ariStack[i];

System.out.print(ariStack[i]); result += ariStack[i]; }

System.out.print(\); result += \; }

//输出逆波兰式

public void printExitChar() { tempString = new String();

for (int i = 0; i < exitPointer; i++) { tempString += exitChar[i];

System.out.print(exitChar[i]); result += exitChar[i]; }

System.out.print(\); result += \; }

//检查字符

public boolean checkString(String checkString) { char ch;

for (int i = 0; i < checkString.length(); i++) { ch = checkString.charAt(i); if (isLetter(ch)) { continue;

}else if (isDigit(ch)) { continue;

}else if (isArithmetic(ch)) { continue; }else {

return false; }

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

}

return true; }

//逆波兰分析

public void RPNAnalysis(String expression) { expChar = expression.toCharArray(); char ch = expChar[expPointer++];

int temp = 0;

if (checkString(expression)) {

System.out.print(\步骤\\t当前符号\\t输入区\\t\\t运算符号栈\\t\\t输出区\\r\\n\);

result += \步骤\\t当前符号\\t输入区\\t\\t运算符号栈\\t\\t输出区\\r\\n\;

while (ch != '#') {

if (isArithmetic(ch)) { switch (ch) { case '(':

ariStack[top++] = ch; System.out.print(row+\+ch+\); result += row+\+ch+\; dataStrings[row][0] = row+\;

dataStrings[row][1] = String.valueOf(ch); printExpChar();

dataStrings[row][2] = tempString; printAriStack();

dataStrings[row][3] = tempString; printExitChar();

dataStrings[row][4] = tempString; row++; break; case ')':

while (ariStack[--top]!='(') {

exitChar[exitPointer++] = ariStack[top];

System.out.print(row+\+ch+\); result += row+\+ch+\; dataStrings[row][0] = row+\;

dataStrings[row][1] = String.valueOf(ch); printExpChar();

dataStrings[row][2] = tempString; printAriStack();

dataStrings[row][3] = tempString; printExitChar();

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

dataStrings[row][4] = tempString; row++;

}

System.out.print(row+\+ch+\); dataStrings[row][0] = row+\; result += row+\+ch+\;

dataStrings[row][1] = String.valueOf(ch); printExpChar();

dataStrings[row][2] = tempString; printAriStack();

dataStrings[row][3] = tempString; printExitChar();

dataStrings[row][4] = tempString; row++; break; case '+': case '-':

temp = top - 1;

if (temp>=0 && (ariStack[temp] == '*' || ariStack[temp] == '/')) {

exitChar[exitPointer++] = ariStack[temp]; top--;

}else if (temp>=0 && (ariStack[temp] == '-' || ariStack[temp] == '+')

&&(expChar[expPointer+1] != '*' ||expChar[expPointer+1] != '/')) {

exitChar[exitPointer++] = ariStack[temp]; top--;

}

while (top<0&&ariStack[temp]!='(') {

exitChar[exitPointer++] = ariStack[temp]; top--; }

ariStack[top++] = ch;

System.out.print(row+\+ch+\); result += row+\+ch+\; dataStrings[row][0] = row+\;

dataStrings[row][1] = String.valueOf(ch); printExpChar();

dataStrings[row][2] = tempString; printAriStack();

dataStrings[row][3] = tempString; printExitChar();

dataStrings[row][4] = tempString;

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

row++; break; case '*': case '/':

temp = top - 1;

if (temp >= 0 && (ariStack[temp] == '*' || ariStack[temp] == '/') ) {

exitChar[exitPointer++] = ariStack[temp]; top--;

} ariStack[top++] = ch;

System.out.print(row+\+ch+\); result += row+\+ch+\; dataStrings[row][0] = row+\;

dataStrings[row][1] = String.valueOf(ch); printExpChar();

dataStrings[row][2] = tempString; printAriStack();

dataStrings[row][3] = tempString; printExitChar();

dataStrings[row][4] = tempString; row++; break; default: break; }

}else if (isLetter(ch) || isDigit(ch)) {

exitChar[exitPointer++] = ch; System.out.print(row+\+ch+\); result += row+\+ch+\; dataStrings[row][0] = row+\;

dataStrings[row][1] = String.valueOf(ch); printExpChar();

dataStrings[row][2] = tempString; printAriStack();

dataStrings[row][3] = tempString; printExitChar();

dataStrings[row][4] = tempString; row++; }

ch = expChar[expPointer++]; }

while(top!=0) {

temp = top - 1;

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

}

}

}

if (ariStack[temp]=='(') { top--; continue; }

exitChar[exitPointer++] = ariStack[--top]; System.out.print(row+\+ch+\); result += row+\+ch+\; dataStrings[row][0] = row+\;

dataStrings[row][1] = String.valueOf(ch); printExpChar();

dataStrings[row][2] = tempString; printAriStack();

dataStrings[row][3] = tempString; printExitChar();

dataStrings[row][4] = tempString; row++;

//计算值

public void compValue() {

int stack[] = new int[100]; exitChar[exitPointer++] = '#'; char ch;

int i=0,top=0;

ch = exitChar[i++]; while (ch != '#') { switch (ch) { case '+':

compValue += stack[top-1] + \ + stack [top]; stack[top-1] = stack[top-1] + stack [top]; compValue += \ + stack[top-1] + \; top--; break; case '-':

compValue += stack[top-1] + \ + stack [top]; stack[top-1] = stack[top-1] - stack [top];

compValue += \ + stack[top-1] + \; top--; break; case '*':

compValue += stack[top-1] + \ + stack [top];

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

}

stack[top-1] = stack[top-1] * stack [top]; compValue += \ + stack[top-1] + \; top--; break; case '/':

compValue += stack[top-1] + \ + stack [top]; stack[top-1] = stack[top-1] / stack [top]; compValue += \ + stack[top-1] + \; top--; break; default: top++;

stack[top] = ch - '0'; break; }

ch = exitChar[i++]; }

compValue += \结果:\ + stack[top]; System.out.print(compValue);

public int getRow() { return row; }

public void setRow(int row) { this.row = row; }

public String[][] getDataStrings() { return dataStrings; }

public void setDataStrings(String[][] dataStrings) { this.dataStrings = dataStrings; }

public String getResult() { return result; }

public void setResult(String result) { this.result = result; }

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

}

public char[] getExitChar() { return exitChar; }

public void setExitChar(char[] exitChar) { this.exitChar = exitChar; }

public String getCompValue() { return compValue; }

public void setCompValue(String compValue) { this.compValue = compValue; }

2、界面类

package RPNAnalysis;

import java.io.BufferedReader; import java.io.File;

import java.io.FileReader; import java.io.FileWriter;

import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Menu;

import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.SWT;

import org.eclipse.swt.widgets.MenuItem;

import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.custom.TableEditor; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Table;

import org.eclipse.swt.widgets.TableColumn; import org.eclipse.wb.swt.SWTResourceManager; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.MouseAdapter;

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

import org.eclipse.swt.events.MouseEvent;

import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Point;

import org.eclipse.swt.graphics.Rectangle;

public class RPNSWT {

protected Shell shell; private Text text;

private Table varTable; private Text resultText; private Table resultTable;

private MessageBox messageBox; private TableEditor editor; private String result = \;

private File sourceFile = null; private RPN_Operation mOperation; private String analysisString;

String compValueString = new String(); private int varTableRow = 0; private char[] varTableVar; private char[] exitChar;

/**

* Launch the application. * @param args */

public static void main(String[] args) { try {

RPNSWT window = new RPNSWT(); window.open();

} catch (Exception e) { e.printStackTrace(); } }

/**

* Open the window. */

public void open() {

Display display = Display.getDefault(); createContents();

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

}

shell.open(); shell.layout();

while (!shell.isDisposed()) {

if (!display.readAndDispatch()) { display.sleep(); } }

/**

* Create contents of the window. */

protected void createContents() { shell = new Shell();

shell.setSize(500, 386);

shell.setText(\逆波兰表达式分析器\);

Menu menu = new Menu(shell, SWT.BAR); shell.setMenuBar(menu);

MenuItem menuItem = new MenuItem(menu, SWT.NONE);

menuItem.addSelectionListener(new SelectionAdapter() { @Override

public void widgetSelected(SelectionEvent e) { newTextTable(); } });

menuItem.setText(\);

MenuItem menuItem_1 = new MenuItem(menu, SWT.NONE);

menuItem_1.addSelectionListener(new SelectionAdapter() { @Override

public void widgetSelected(SelectionEvent e) { openFile(); } });

menuItem_1.setText(\);

MenuItem menuItem_2 = new MenuItem(menu, SWT.NONE);

menuItem_2.addSelectionListener(new SelectionAdapter() { @Override

public void widgetSelected(SelectionEvent e) { try {

analysis();

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

} catch (Exception e2) { e2.printStackTrace(); } } });

menuItem_2.setText(\);

MenuItem menuItem_6 = new MenuItem(menu, SWT.NONE);

menuItem_6.addSelectionListener(new SelectionAdapter() { @Override

public void widgetSelected(SelectionEvent e) { try {

compValue();

} catch (Exception e2) { e2.printStackTrace(); } } });

menuItem_6.setText(\);

MenuItem menuItem_3 = new MenuItem(menu, SWT.NONE);

menuItem_3.addSelectionListener(new SelectionAdapter() { @Override

public void widgetSelected(SelectionEvent e) { saveFile(); } });

menuItem_3.setText(\);

MenuItem menuItem_4 = new MenuItem(menu, SWT.NONE);

menuItem_4.addSelectionListener(new SelectionAdapter() { @Override

public void widgetSelected(SelectionEvent e) { shell.dispose(); } });

menuItem_4.setText(\);

MenuItem menuItem_5 = new MenuItem(menu, SWT.NONE);

menuItem_5.addSelectionListener(new SelectionAdapter() { @Override

public void widgetSelected(SelectionEvent e) { messageBox = new MessageBox(shell,SWT.ICON_INFORMATION); messageBox.setText(\关于\);

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

messageBox.setMessage(\逆波兰表达式分析器\\n 作者:章进兴\\n 0941901228\);

messageBox.open(); } });

menuItem_5.setText(\);

ScrolledComposite scrolledComposite = new

ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); scrolledComposite.setBounds(10, 10, 202, 119); scrolledComposite.setExpandHorizontal(true); scrolledComposite.setExpandVertical(true);

text = new Text(scrolledComposite, SWT.BORDER); scrolledComposite.setContent(text);

scrolledComposite.setMinSize(text.computeSize(SWT.DEFAULT, SWT.DEFAULT));

varTable = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); varTable.setBounds(218, 10, 127, 119); varTable.setHeaderVisible(true); varTable.setLinesVisible(true);

editor = new TableEditor(varTable); editor.horizontalAlignment = SWT.LEFT; editor.grabHorizontal = true; createEditorContents();

TableColumn tableColumn = new TableColumn(varTable, SWT.NONE); tableColumn.setWidth(61);

tableColumn.setText(\);

TableColumn tblclmnNewColumn = new TableColumn(varTable, SWT.NONE);

tblclmnNewColumn.setWidth(61);

tblclmnNewColumn.setText(\);

ScrolledComposite scrolledComposite_1 = new

ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); scrolledComposite_1.setBounds(351, 10, 123, 119); scrolledComposite_1.setExpandHorizontal(true); scrolledComposite_1.setExpandVertical(true);

resultText = new Text(scrolledComposite_1, SWT.BORDER |

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL);

resultText.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_BACKGROUND));

resultText.setEditable(false);

scrolledComposite_1.setContent(resultText);

scrolledComposite_1.setMinSize(resultText.computeSize(SWT.DEFAULT, SWT.DEFAULT));

resultTable = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); resultTable.setBounds(10, 135, 464, 183); resultTable.setHeaderVisible(true); resultTable.setLinesVisible(true);

TableColumn tblclmnNewColumn_1 = new TableColumn(resultTable, SWT.NONE);

tblclmnNewColumn_1.setResizable(false); tblclmnNewColumn_1.setWidth(59);

tblclmnNewColumn_1.setText(\);

TableColumn tblclmnNewColumn_2 = new TableColumn(resultTable, SWT.NONE);

tblclmnNewColumn_2.setWidth(100);

tblclmnNewColumn_2.setText(\);

TableColumn tblclmnNewColumn_3 = new TableColumn(resultTable, SWT.NONE);

tblclmnNewColumn_3.setWidth(100);

tblclmnNewColumn_3.setText(\);

TableColumn tblclmnNewColumn_4 = new TableColumn(resultTable, SWT.NONE);

tblclmnNewColumn_4.setWidth(100);

tblclmnNewColumn_4.setText(\);

TableColumn tblclmnNewColumn_5 = new TableColumn(resultTable, SWT.NONE);

tblclmnNewColumn_5.setWidth(100);

tblclmnNewColumn_5.setText(\);

}

public void openFile() {

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

FileDialog openFileDialog = new FileDialog(shell,SWT.OPEN); openFileDialog.setText(\选择需要分析的文件\);

openFileDialog.setFilterExtensions(new String[] {\}); openFileDialog.setFilterNames(new String[]{\文本文件(*.txt)\});

openFileDialog.setFilterPath(\);

String selectedOpenFile = openFileDialog.open(); this.result = \;

if (selectedOpenFile!=null) {

sourceFile = new File(selectedOpenFile); try {

FileReader fileReader = new FileReader(sourceFile);

BufferedReader reader = new BufferedReader(fileReader); StringBuffer stringBuffer = new StringBuffer(); String lineString = null;

while((lineString = reader.readLine())!=null) {

stringBuffer.append(lineString); stringBuffer.append(\); }

text.setText(stringBuffer.toString()); shell.setText(\逆波兰表达式分析器 - \ + openFileDialog.getFileName()); fileReader.close(); varTable.removeAll(); resultTable.removeAll(); } catch (Exception e) { e.printStackTrace(); } }else {

messageBox = new MessageBox(shell,SWT.ICON_WARNING);

messageBox.setMessage(\文件未载入!请重新打开需要分析的文件!\);

messageBox.setText(\提示\); messageBox.open(); } }

public void newTextTable() { this.result = \;

this.text.setText(\);

this.resultText.setText(\); this.varTable.removeAll(); this.resultTable.removeAll();

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

}

public void saveFile() { if (result != \) {

result += \+compValueString; FileWriter fileWriter = null; try {

fileWriter = new FileWriter(\); fileWriter.write(result); } catch (Exception e) { e.printStackTrace(); }finally{ try {

Runtime runtime = Runtime.getRuntime(); messageBox = new MessageBox(shell); messageBox.setMessage(\保存成功!\); messageBox.setText(\提示\); fileWriter.flush(); fileWriter.close(); messageBox.open();

sourceFile = new File(\);

runtime.exec(\\+sourceFile.getAbsolutePath());

} catch (Exception e2) { e2.printStackTrace(); } } }else {

messageBox = new MessageBox(shell,SWT.ICON_WARNING); messageBox.setMessage(\未分析,保存失败!\); messageBox.setText(\提示\); messageBox.open(); } }

public void analysis() {

mOperation = new RPN_Operation(); this.resultTable.removeAll(); this.varTable.removeAll(); this.resultText.setText(\);

this.analysisString = text.getText(); this.result = \;

boolean flag = false;

if (mOperation.checkString(this.analysisString)) {

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

flag = true; }

if (this.analysisString.equals(\)) {

messageBox = new MessageBox(shell,SWT.ICON_WARNING);

messageBox.setMessage(\请输入需要分析的字符串或打开需要分析的文件!\);

messageBox.setText(\提示\); messageBox.open(); }else if (flag) {

if (analysisString.charAt(analysisString.length()-1) != '#') {

analysisString += \; }

this.result = \;

mOperation.RPNAnalysis(this.analysisString); for (int i = 0; i < mOperation.getRow(); i++) {

TableItem item = new TableItem(resultTable, SWT.NULL); for (int j = 0; j < 5; j++) {

item.setText(j, mOperation.getDataStrings()[i][j]); } }

this.result = mOperation.getResult(); showVariable(); }else {

messageBox = new MessageBox(shell,SWT.ICON_WARNING); messageBox.setMessage(\输入的字符串有错误!\); messageBox.setText(\提示\); messageBox.open(); } }

public void showVariable() {

for (int i = 0; i < analysisString.length(); i++) {

if (mOperation.isLetter(analysisString.charAt(i)) || mOperation.isDigit(analysisString.charAt(i))) { varTableRow++; } }

varTableVar = new char[varTableRow]; varTableRow = 0;

for (int i = 0; i < analysisString.length(); i++) {

if (mOperation.isLetter(analysisString.charAt(i)) || mOperation.isDigit(analysisString.charAt(i))) { varTableVar[varTableRow] = analysisString.charAt(i);

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

varTableRow++; } }

boolean flag = true;

for (int i = 0; i < varTableRow; i++) {

for (int j = 0; j < varTable.getItems().length; j++) { if (varTableVar[i] ==

varTable.getItems()[j].getText().charAt(0)) { flag = false; } }

if (flag) {

TableItem item = new TableItem(varTable, SWT.NULL);

item.setText(0, String.valueOf(varTableVar[i])); if (mOperation.isDigit(varTableVar[i])) {

item.setText(1, String.valueOf(varTableVar[i])); }else {

item.setText(1, \); } }

flag = true; }

}

public void createEditorContents(){

varTable.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent event) { Control old = editor.getEditor(); if (old != null) { old.dispose(); }

Point point = new Point(event.x, event.y);

final TableItem item = varTable.getItem(point); if (item != null) { int column = -1;

for (int i = 1, n = varTable.getColumnCount(); i < n; i++) {

Rectangle rectangle = item.getBounds(i); if (rectangle.contains(point)) { column = i; break; }

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

}

} });

}

}

if (column >= 0) {

final Text text = new Text(varTable, SWT.NONE); text.setText(item.getText(column)); text.selectAll(); text.setFocus();

editor.setEditor(text, item, column); final int col = column;

text.addModifyListener(new ModifyListener() { @Override

public void modifyText(ModifyEvent arg0) { item.setText(col, text.getText());

} }); }

public char setValue(char var) {

for (int i = 0; i < varTable.getItems().length; i++) {

if ( var == varTable.getItems()[i].getText().charAt(0)) { return varTable.getItems()[i].getText(1).charAt(0);

} }

return var; }

public void compValue(){ if (result == \) {

messageBox = new MessageBox(shell,SWT.ICON_WARNING); messageBox.setMessage(\计算失败!请先分析!\); messageBox.setText(\提示\); messageBox.open(); }else {

exitChar = new char[mOperation.getExitChar().length]; exitChar = mOperation.getExitChar();

for (int i = 0; i < exitChar.length; i++) { exitChar[i] = setValue(exitChar[i]); }

mOperation.setExitChar(exitChar); mOperation.compValue();

江苏科技大学 计算机学院 编译原理实验报告0941901228 章进兴

}

}

} compValueString = mOperation.getCompValue(); System.out.print(compValueString); resultText.setText(compValueString);

七、运行结果

八、实验总结

本程序实现了分析串的手动输入和文件导入,并且有保存结果的功能,基本实现了实验要求的所有功能。

通过本实验,我再次熟悉了逆波兰表达式的生成过程与计算过程,熟练的掌握了其程序的算法,以及数据结构。掌握逆波兰表达式程序设计的原理和构造方法,训练了我掌握开发应用程序的基本方法。

本文来源:https://www.bwwdw.com/article/q202.html

Top