[原创]表达式求值:经典算法 <Java版本>
2011-07-01 14:39
561 查看
1.课件:表达式的三种表示形式及其规律
2.后缀表达式求值以及如何实现
Knuth 将此概括为三个步骤:
对中缀表达式进行语法分析
中缀表达式到后缀表达式的转换
对后缀表达式求值
C语言建议代码是实现:
3.我的Java语言实现,利用了Java自身的优越性,可以更好的处理一些内容 (做这个是为了完成数据结构的课程设计,后期会有相应的文章介绍我的作品《算术24游戏》,谢谢关注)
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }程序中的很多的输出是为了让我们更加清楚地看到整个的执行过程:想知道的话可以看看输出结果
transform
0 2
1 +
2 6
3 *
4 7
5 -
6 5
7 /
8 1
9 *
10 7
11 #
2
6
7
*
+
5
1
/
7
*
-
calculate
push 2
push 6
push 7
*
* 7 6
push 42
+
+ 42 2
push 44
push 5
push 1
/
/ 1 5
push 5
push 7
*
* 7 5
push 35
-
- 35 44
push 9
res 9
中缀表达式为 2+6*7-5/1*7 ,最终的结果是 9
中缀表达式为 (2+6)*7-5/1*7 ,最终的结果是 21
注:你可能觉得整个代码的结构不是很好,这个因为这个程序不是单纯的用于计算中缀表达式的,它是我做的《算术24游戏》的其中的一个模块,后期将会呈现,敬请期待
2.后缀表达式求值以及如何实现
Knuth 将此概括为三个步骤:
对中缀表达式进行语法分析
中缀表达式到后缀表达式的转换
对后缀表达式求值
C语言建议代码是实现:
3.我的Java语言实现,利用了Java自身的优越性,可以更好的处理一些内容 (做这个是为了完成数据结构的课程设计,后期会有相应的文章介绍我的作品《算术24游戏》,谢谢关注)
//测试的main方法 public static void main(String arg[]) { String s = "2+6*7-5/1*7"; ArrayList postfix = transform(s); for (int i = 0, len = postfix.size(); i < len; i++) { System.out.println(postfix.get(i)); } calculate(postfix); } //将中缀表达式转换成后缀表达式 public static ArrayList transform(String prefix) { System.out.println("transform"); int i, len = prefix.length();// 用字符数组保存前缀表达式 prefix=prefix+ '#';// 让前缀表达式以'#'结尾 Stack<Character> stack = new Stack<Character>();// 保存操作符的栈 stack.push('#');// 首先让'#'入栈 ArrayList postfix = new ArrayList(); // 保存后缀表达式的列表,可能是数字,也可能是操作符,之前使用的是ArrayList for (i = 0; i < len + 1; i++) { System.out.println(i+" "+prefix.charAt(i)); if (Character.isDigit(prefix.charAt(i))) {// 当前字符是一个数字 if (Character.isDigit(prefix.charAt(i+1))) {// 当前字符的下一个字符也是数字(两位数) postfix.add(10 * (prefix.charAt(i)-'0') + (prefix.charAt(i+1)-'0')); i++; } else {// 当前字符的下一个字符不是数字(一位数) postfix.add((prefix.charAt(i)-'0')); } } else {// 当前字符是一个操作符 switch (prefix.charAt(i)) { case '(':// 如果是开括号 stack.push(prefix.charAt(i));// 开括号只是放入到栈中,不放入到后缀表达式中 break; case ')':// 如果是闭括号 while (stack.peek() != '(') { postfix.add(stack.pop());// 闭括号是不入栈的 } stack.pop();// 弹出'(' break; default:// 默认情况下:+ - * / while (stack.peek() != '#' && compare(stack.peek(), prefix.charAt(i))) { postfix.add(stack.pop());// 不断弹栈,直到当前的操作符的优先级高于栈顶操作符 } if (prefix.charAt(i) != '#') {// 如果当前的操作符不是'#'(结束符),那么入操作符栈 stack.push(prefix.charAt(i));// 最后的标识符'#'是不入栈的 } break; } } } return postfix; } //比较运算符之间的优先级 public static boolean compare(char peek, char cur) {// 如果是peek优先级高于cur,返回true,默认都是peek优先级要低 if (peek == '*' && (cur == '+' || cur == '-' || cur == '/' || cur == '*')) {// 如果cur是'(',那么cur的优先级高,如果是')',是在上面处理 return true; } else if (peek == '/' && (cur == '+' || cur == '-' || cur == '*' || cur == '/')) { return true; } else if (peek == '+' && (cur == '+' || cur == '-')) { return true; } else if (peek == '-' && (cur == '+' || cur == '-')) { return true; } else if (cur == '#') {// 这个很特别,这里说明到了中缀表达式的结尾,那么就要弹出操作符栈中的所有操作符到后缀表达式中 return true;// 当cur为'#'时,cur的优先级算是最低的 } return false;// 开括号是不用考虑的,它的优先级一定是最小的,cur一定是入栈 } //计算后缀表达式 public static boolean calculate(ArrayList postfix){//后缀表达式的运算顺序就是操作符出现的先后顺序 System.out.println("calculate"); int i,res=0,size=postfix.size(); Stack<Integer> stack_num=new Stack<Integer>(); for(i=0;i<size;i++){ if(postfix.get(i).getClass()==Integer.class){//说明是操作数,这个很有用啊! stack_num.push((Integer)postfix.get(i)); System.out.println("push"+" "+(Integer)postfix.get(i)); }else{//如果是操作符 System.out.println((Character)postfix.get(i)); int a=stack_num.pop(); int b=stack_num.pop();//注意运算时的前者和后者 switch((Character)postfix.get(i)){ case '+': res=b+a; System.out.println("+ "+a+" "+b); break; case '-': res=b-a; System.out.println("- "+a+" "+b); break; case '*': res=b*a; System.out.println("* "+a+" "+b); break; case '/': res=b/a; System.out.println("/ "+a+" "+b); break; } stack_num.push(res); System.out.println("push"+" "+res); } } res=stack_num.pop(); System.out.println("res "+" "+res); if(res==24){ return true; } return false; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }程序中的很多的输出是为了让我们更加清楚地看到整个的执行过程:想知道的话可以看看输出结果
transform
0 2
1 +
2 6
3 *
4 7
5 -
6 5
7 /
8 1
9 *
10 7
11 #
2
6
7
*
+
5
1
/
7
*
-
calculate
push 2
push 6
push 7
*
* 7 6
push 42
+
+ 42 2
push 44
push 5
push 1
/
/ 1 5
push 5
push 7
*
* 7 5
push 35
-
- 35 44
push 9
res 9
中缀表达式为 2+6*7-5/1*7 ,最终的结果是 9
中缀表达式为 (2+6)*7-5/1*7 ,最终的结果是 21
注:你可能觉得整个代码的结构不是很好,这个因为这个程序不是单纯的用于计算中缀表达式的,它是我做的《算术24游戏》的其中的一个模块,后期将会呈现,敬请期待
相关文章推荐
- [原创]表达式求值:经典算法 <Java版本>
- [原创]我的作品:我的算术24小游戏<Java版本>
- 经典算法问题的java实现<一>
- 经典算法问题的java实现<二>
- 黑马程序员--JAVA<正则表达式>
- <七>java数据结构与算法 栈 实现单词逆序
- Eclipse Java注释模板设置详解<非原创>
- 致我们终将忘记的算法(随处可见的经典<3>)
- Java项目打包<非原创>
- <二>java数据结构与算法 冒泡排序
- <四>java数据结构与算法 插入排序
- 使用java 正则表达式 只替换排除< >之内的内容
- <JAVA>正则表达式是什么?
- 我在北京找工作(二):java实现算法<1> 冒泡排序+直接选择排序
- 致我们终将忘记的算法(随处可见的经典<2>)
- 表达式求值[java版本]
- <五大经典算法> 四、回溯法
- [原创]JAVA技巧:去除ArrayList<Object>里面的重复记录
- java 从零开始,学习笔记之基础入门<运算符表达式和流程控制>(六)
- Dijkatra 迪克斯特拉双栈算术表达式求值算法 Java