You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgflzu36e/Calculation.java

128 lines
2.9 KiB

4 years ago
package Stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Calculation{
//<2F><><EFBFBD><EFBFBD>׺<EFBFBD><D7BA><EFBFBD><EFBFBD>ʽתΪList
public static List<String> InfixtoList(String s){
List<String> l=new ArrayList<String>();
int i=0;//<2F><><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD>׺<EFBFBD><D7BA><EFBFBD><EFBFBD>ʽ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
String splicing;//<2F>Զ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ƴ<EFBFBD><C6B4>
char c;//<2F><><EFBFBD><EFBFBD>ÿһ<C3BF><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ͱ<EFBFBD><CDB1><EFBFBD><EFBFBD><EFBFBD>c
do {//<2F><><EFBFBD><EFBFBD>c<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>֣<EFBFBD><D6A3>ͼ<EFBFBD><CDBC>뵽l<EBB5BD><6C>
if((c=s.charAt(i))<48||(c=s.charAt(i))>57) {
l.add(""+c);
i++;
}else {//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>Ƕ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
splicing="";//<2F>ȼ<EFBFBD>str<74>ó<EFBFBD>""
while(i<s.length() && (c=s.charAt(i))>=48 && (c=s.charAt(i))<=57) {
splicing+=c;
i++;
}
l.add(splicing);
}
}while(i<s.length());
return l;
}
//<2F><><EFBFBD>õ<EFBFBD>ListתΪ<D7AA><CEAA>׺<EFBFBD><D7BA><EFBFBD><EFBFBD>ʽ
public static List<String> ListtoSuffix(List<String> l){
Stack<String> s1=new Stack<String> ();
List<String> s2=new ArrayList<String> ();
//<2F><><EFBFBD><EFBFBD>l
for(String item: l) {
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뵽s2
if(item.matches("\\d+"))
{
s2.add(item);
}else if(item.equals("(")){
s1.push(item);
}else if(item.equals(")")){
while(!s1.peek().equals("(")) {//<2F>鿴s1<73>е<EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>(
s2.add(s1.pop());
}
s1.pop();//<2F><>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(
}else {
//<2F><>item<65><6D><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>С<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>s1ջ<31><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD>뵽s2<73>У<EFBFBD><D0A3>ٴ<EFBFBD><D9B4><EFBFBD>s1<73>е<EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD>
while(s1.size()!=0 && Operation.getValue(s1.peek())>=Operation.getValue(item)) {
s2.add(s1.pop());
}
//<2F><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>itemѹ<6D><D1B9>ջ<EFBFBD><D5BB>
s1.push(item);
}
}
while(s1.size()!=0) {
s2.add(s1.pop());
}
return s2;
}
//<2F><><EFBFBD><EFBFBD>
public static int calculate(List<String> l) {
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ջ
Stack<String> stack=new Stack<String>();
for(String item: l) {
if(item.matches("\\d+")) {//ƥ<><C6A5><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD>λ<EFBFBD><CEBB>
stack.push(item);
}
else {
int num2=Integer.parseInt(stack.pop());
int num1=Integer.parseInt(stack.pop());
int res=0;
if(item.equals("+")) {
res=num1+num2;
}else if(item.equals("-")) {
res=num1-num2;
}else if(item.equals("*")) {
res=num1*num2;
}else if(item.equals("/")) {
res=num1/num2;
}else {
throw new RuntimeException("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
//<2F><>res<65><73>ջ
stack.push(res+"");//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>תΪ<D7AA>ַ<EFBFBD><D6B7><EFBFBD>
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>stack<63>е<EFBFBD><D0B5><EFBFBD><EFBFBD>ݾ<EFBFBD><DDBE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return Integer.parseInt(stack.pop());
}
}
//<2F><>дһ<D0B4><D2BB><EFBFBD><EFBFBD>Operation<6F><6E><EFBFBD>Է<EFBFBD><D4B7><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>
class Operation{
private static int ADD=1;
private static int SUB=1;
private static int MUL=2;
private static int DIV=2;
//дһ<D0B4><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>
public static int getValue(String operation) {
int result=0;
switch (operation) {
case "+":
result=ADD;
break;
case "-":
result=SUB;
break;
case "*":
result=MUL;
break;
case "/":
result=DIV;
break;
default:
//System.out.println("<22><><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
break;
}
return result;
}
}