Skip to content

Commit 52596f1

Browse files
committed
栈的实现源码更新
1 parent cd3a4dd commit 52596f1

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.zejian.structures.Stack;
2+
3+
/**
4+
* Created by zejian on 2016/11/28.
5+
* Blog : http://blog.csdn.net/javazejian [原文地址,请尊重原创]
6+
* 中缀转后缀,然后计算后缀表达式的值
7+
*/
8+
public class CalculateExpression {
9+
10+
/**
11+
* 中缀转后缀
12+
* @param expstr 中缀表达式字符串
13+
* @return
14+
*/
15+
public static String toPostfix(String expstr)
16+
{
17+
//创建栈,用于存储运算符
18+
SeqStack<String> stack = new SeqStack<>(expstr.length());
19+
20+
String postfix="";//存储后缀表达式的字符串
21+
int i=0;
22+
while (i<expstr.length())
23+
{
24+
char ch=expstr.charAt(i);
25+
switch (ch)
26+
{
27+
case '+':
28+
case '-':
29+
//当栈不为空或者栈顶元素不是左括号时,直接出栈,因此此时只有可能是*/+-四种运算符(根据规则4),否则入栈
30+
while (!stack.isEmpty() && !stack.peek().equals("(")) {
31+
postfix += stack.pop();
32+
}
33+
//入栈
34+
stack.push(ch+"");
35+
i++;
36+
break;
37+
case '*':
38+
case '/':
39+
//遇到运算符*/
40+
while (!stack.isEmpty() && (stack.peek().equals("*") || stack.peek().equals("/"))) {
41+
postfix += stack.pop();
42+
}
43+
stack.push(ch+"");
44+
i++;
45+
break;
46+
case '(':
47+
//左括号直接入栈
48+
stack.push(ch+"");
49+
i++;
50+
break;
51+
case ')':
52+
//遇到右括号(规则3)
53+
String out = stack.pop();
54+
while (out!=null && !out.equals("("))
55+
{
56+
postfix += out;
57+
out = stack.pop();
58+
}
59+
i++;
60+
break;
61+
default:
62+
//操作数直接入栈
63+
while (ch>='0' && ch<='9')
64+
{
65+
postfix += ch;
66+
i++;
67+
if (i<expstr.length())
68+
ch=expstr.charAt(i);
69+
else
70+
ch='=';
71+
}
72+
//分隔符
73+
postfix += " ";
74+
break;
75+
}
76+
}
77+
//最后把所有运算符出栈(规则5)
78+
while (!stack.isEmpty())
79+
postfix += stack.pop();
80+
return postfix;
81+
}
82+
83+
/**
84+
* 计算后缀表达式的值
85+
* @param postfix 传入后缀表达式
86+
* @return
87+
*/
88+
public static int calculatePostfixValue(String postfix)
89+
{
90+
//栈用于存储操作数,协助运算
91+
LinkedStack<Integer> stack = new LinkedStack<>();
92+
int i=0, result=0;
93+
while (i<postfix.length())
94+
{
95+
char ch=postfix.charAt(i);
96+
if (ch>='0' && ch<='9')
97+
{
98+
result=0;
99+
while (ch!=' ')
100+
{
101+
//将整数字符转为整数值ch=90
102+
result = result*10 + Integer.parseInt(ch+"");
103+
i++;
104+
ch = postfix.charAt(i);
105+
}
106+
i++;
107+
stack.push(result);//操作数入栈
108+
}
109+
else
110+
{ //ch 是运算符,出栈栈顶的前两个元素
111+
int y= stack.pop();
112+
int x= stack.pop();
113+
switch (ch)
114+
{ //根据情况进行计算
115+
case '+': result=x+y; break;
116+
case '-': result=x-y; break;
117+
case '*': result=x*y; break;
118+
case '/': result=x/y; break; //注意这里并没去判断除数是否为0的情况
119+
}
120+
//将运算结果入栈
121+
stack.push(result);
122+
i++;
123+
}
124+
}
125+
//将最后的结果出栈并返回
126+
return stack.pop();
127+
}
128+
//测试
129+
public static void main(String args[])
130+
{
131+
String expstr="1+3*(9-2)+90";
132+
String postfix = toPostfix(expstr);
133+
System.out.println("中缀表达式->expstr= "+expstr);
134+
System.out.println("后缀表达式->postfix= "+postfix);
135+
System.out.println("计算结果->value= "+calculatePostfixValue(postfix));
136+
}
137+
138+
}

0 commit comments

Comments
 (0)