表达式运算,网上查了一下java有源代码。不过问题太多。而且都是一个版本。
根本没法使用。
逆波兰式算法主要的设计思路是把一般的中序表达式变为右序表达式
例子1+2-5*(5-4)*6-(6-1)
把上面的表达式转化为:
12+554-*6*-61–
然后通过栈再来计算。
具体的理论网上找找逆波兰式
根据人家的版本进行了修改。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package
expression1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.util.
*
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//
栈类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public
class
Stacks
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private
LinkedList list
=
new
LinkedList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int
top
=-
1
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public
void
push(Object value)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
top
++
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list.addFirst(value);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public
Object pop()
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Object temp
=
list.getFirst();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
top
—
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list.removeFirst();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return
temp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public
Object top()
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return
list.getFirst();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package
expression1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.io.
*
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.util.
*
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public
class
Expression
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private
ArrayList expression
=
new
ArrayList();
//
存储中序表达式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private
ArrayList right
=
new
ArrayList();
//
存储右序表达式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private
String result;
//
结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
依据输入信息创建对象,将数值与操作符放入ArrayList中
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private
Expression(String input)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StringTokenizer st
=
new
StringTokenizer(input,
”
+-*/()
”
,
true
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while
(st.hasMoreElements())
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String s
=
st.nextToken();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
expression.add(s);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
将中序表达式转换为右序表达式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private
void
toRight()
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stacks aStack
=
new
Stacks();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String operator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int
position
=
0
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while
(
true
)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if
(Calculate.isOperator((String) expression.get(position)))
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if
(aStack.top
==
–
1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
||
((String) expression.get(position)).equals(
”
(
”
))
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aStack.push(expression.get(position));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}
else
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if
(((String) expression.get(position)).equals(
”
)
”
))
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while
(
true
)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if
(aStack.top
!=
–
1
&&!
((String) aStack.top()).equals(
”
(
”
))
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
operator
=
(String) aStack.pop();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
right.add(operator);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}
else
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if
(aStack.top
!=
–
1
)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aStack.pop();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}
else
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while
(
true
)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if
(aStack.top
!=
–
1
&&
Calculate.priority((String) expression
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.get(position))
<=
Calculate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.priority((String) aStack.top())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
operator
=
(String) aStack.pop();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if
(
!
operator.equals(
”
(
”
))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
right.add(operator);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}
else
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aStack.push(expression.get(position));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
right.add(expression.get(position));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
position
++
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if
(position
>=
expression.size())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while
(aStack.top
!=
–
1
)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
operator
=
(String) aStack.pop();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if
(
!
operator.equals(
”
(
”
))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
right.add(operator);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
对右序表达式进行求值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private
void
getResult()
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this
.toRight();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for
(
int
i
=
0
;i
<
right.size();i
++
)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(right.get(i));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stacks aStack
=
new
Stacks();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String op1, op2, is
=
null
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Iterator it
=
right.iterator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while
(it.hasNext())
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
is
=
(String) it.next();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if
(Calculate.isOperator(is))
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
op1
=
(String) aStack.pop();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
op2
=
(String) aStack.pop();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aStack.push(Calculate.twoResult(is, op1, op2));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aStack.push(is);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result
=
(String) aStack.pop();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
it
=
expression.iterator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
while
(it.hasNext())
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print((String) it.next());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(
”
=
”
+
result);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public
static
void
main(String avg[])
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(
”
Input a expression:
”
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BufferedReader is
=
new
BufferedReader(
new
InputStreamReader(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.in));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for
(;;)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String input
=
new
String();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
input
=
is.readLine().trim();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if
(input.equals(
”
q
”
))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Expression boya
=
new
Expression(input);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
boya.getResult();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.println(
”
Input another expression or input ‘q’ to quit:
”
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
is.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}
catch
(IOException e)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(
”
Wrong input!!!
”
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ackage expression1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public
class
Calculate
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
判断是否为操作符号
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public
static
boolean
isOperator(String operator)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if
(operator.equals(
”
+
”
)
||
operator.equals(
”
–
”
)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
||
operator.equals(
”
*
”
)
||
operator.equals(
”
/
”
)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
||
operator.equals(
”
(
”
)
||
operator.equals(
”
)
”
))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return
true
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return
false
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
设置操作符号的优先级别
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public
static
int
priority(String operator)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if
(operator.equals(
”
+
”
)
||
operator.equals(
”
–
”
))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return
1
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
if
(operator.equals(
”
*
”
)
||
operator.equals(
”
/
”
))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return
2
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return
0
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
做2值之间的计算
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public
static
String twoResult(String operator, String a, String b)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String op
=
operator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String rs
=
new
String();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double
x
=
Double.parseDouble(b);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double
y
=
Double.parseDouble(a);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double
z
=
0
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if
(op.equals(
”
+
”
))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
z
=
x
+
y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
if
(op.equals(
”
–
”
))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
z
=
x
–
y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
if
(op.equals(
”
*
”
))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
z
=
x
*
y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
if
(op.equals(
”
/
”
))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
z
=
x
/
y;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
z
=
0
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return
rs
+
z;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}
catch
(NumberFormatException e)
…
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(
”
input has something wrong!
”
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return
”
Error
”
;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
版权声明:本文为yunxiang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。