b = opndStack.pop();
operator = optrStack.pop().charValue();
opndStack.push(operateTwoNum(a, b, operator));
continue;
} else if (PriorityTable.getPriority(optrStack.peek() .charValue(), exp[postion].charAt(0)) == '=') { optrStack.pop();
if (exp[postion].charAt(0) == '#') { return opndStack.pop().toString(); } } } else
optrStack.push( exp[postion].charAt(0));
}
// 为运算数时 else {
opndStack.push(Float.valueOf((exp[postion])).floatValue()); }
postion++;
if (postion >= exp.length) break; }
throw new Exception(); } /***
* 程序入口,启动函数 * @param args */
public static void main(String[] args) {
OperatorPriority op = new OperatorPriority(IOUtil
.getStringFromKeyBoard()); //实例化 构造函数参数从键盘获得 try {
System.out.println(\您输入的表达式:\+ op.input + \+ op.scanner()); } catch (Exception e) {
// TODO Auto-generated catch block //e.printStackTrace();
System.out.println(\您输入的表达式\有误!\ } }
}
package com.op.core;
/*******************************************************************************
* 简单表达式文法G[ E’]构造算符优先关系表。 * E’ →# E # * E → E + Q | Q * Q → Q - T | T * T → T * F | F * F → F / M|M * M → M ^ P|P * P → ( E )|i * @author */
public class PriorityTable {
private static char table[][] = { // + * / i ( ) # ^
{ '>', '<', '<', '<', '<', '>', '>', '<' }, // + { '>', '>', '>', '<', '<', '>', '>', '<' }, // * { '>', '>', '>', '<', '<', '>', '>', '<' }, // / { '>', '>', '>', '$', '$', '>', '>', '>' }, // i { '<', '<', '<', '<', '<', '=', '$', '<' }, // ( { '>', '>', '>', '$', '$', '>', '>', '>' }, // ) { '<', '<', '<', '<', '<', '$', '=', '<' }, // # { '>', '>', '>', '<', '<', '>', '>', '<' }, // ^ }; // 算符优先表
/***************************************************************************
* 判断一个符号在算符优先表中位置 *
* @param c * @return */
private static int judgePriority(char c) { int priority = 0; switch (c) { case '+':
priority = 0; break; case '*':
priority = 1; break; case '/':
priority = 2; break; case 'i':
priority = 3; break; case '(':
priority = 4; break; case ')':
priority = 5; break; case '#':
priority = 6; break; case '^':
priority = 7; break; }
return priority; }
/**
* 判断两个算术符的优先级 *
* @param m
* 为符号栈的栈顶元素 * @param n
* 为当前输入算术符 * @return */
public static char getPriority(char m, char n) {
return PriorityTable.table[judgePriority(m)][judgePriority(n)]; } }
package com.op.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IOUtil { /***
* 得到从键盘输入的字符串 * @return 字符串 */
public static String getStringFromKeyBoard() { try {
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
System.out.print(\请输入一个表达式以#号结束:\ String str=reader.readLine(); //获取字符串 // System.out.println(\您输入的字符串是:\ return str;
} catch (IOException e) {
e.printStackTrace(); }
return null; } }
package com.op.util;
import java.util.Vector;
public class StringUtil { /***
* 切分算术表达式,结果存入字符串数组,如: 字符串:1.5+3*2# 将被切分为 {1.5,+,3,*,2,#} * @param str * @return */
public static String[] splitExp(String str) { Vector
for (int i = 0; i < str.trim().length(); i++) { if ((str.charAt(i) > 32 && str.charAt(i) < 48&&str.charAt(i)!=46)||str.charAt(i)==94) { if(beginIndex!=i)
v.add( str.substring(beginIndex, i)); v.add( String.valueOf(str.charAt(i))); beginIndex = i + 1;
} }
if(beginIndex!=str.length())
v.add(str.substring(beginIndex, str.length())); String result[]=new String[v.size()]; for(int i=0;i result[i]=v.get(i); } return result; } /*** 测试函数 public static void main(String[] a) { System.out.println((int)'^'); String s[]= splitExp(\ for(String c:s) { System.out.println(c); } } /**/ } 注:在课程设计中,我主要负责手工构造算符优先表和对软件测试,构造算符优先表是根据课程设计题目所给出的文法所构造,测试是对于所给文法的规则进行,考虑到各个方面。 学年论文(设计)成绩表 论文题目 算符优先函数的构造 作 者 王鸿百 指导教师 肖鑫 指 导 教 师 评 语 指导教师签字 职 称 教授 等级