标签:system.in public while oid class 数据规模 inpu import ack
import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); String a = input.next(); Stack<Integer> shu = new Stack<Integer>(); Stack<Character> fu = new Stack<Character>(); int index = 0; for(int i=0;i<a.length();i++){ char temp = a.charAt(i); if(temp>=‘9‘||temp>=‘0‘){ index++; }else{ if(index!=0){ shu.push(Integer.parseInt(a.substring(i-index, i))); index = 0; } if(fu.isEmpty()==true){ fu.push(temp); }else if(temp==‘+‘||temp==‘-‘){ while(fu.size()!=0&&fu.peek()!=‘(‘){ int b1 = shu.pop(); int a1 = shu.pop(); shu.push(jisuan(a1,b1,fu.peek())); fu.pop(); } fu.push(temp); }else if(temp==‘*‘||temp==‘/‘){ if(fu.peek()!=‘(‘){ while(fu.peek()==‘*‘||fu.peek()==‘/‘){ int b1 = shu.pop(); int a1 = shu.pop(); shu.push(jisuan(a1,b1,fu.peek())); fu.pop(); fu.push(temp); } fu.push(temp); } else{ fu.push(temp); } } else if(temp==‘(‘){ fu.push(temp); }else if(temp==‘)‘){ while(fu.peek()!=‘(‘){ if(fu.peek()==‘*‘||fu.peek()==‘/‘){ int b1 = shu.pop(); int a1 = shu.pop(); shu.push(jisuan(a1,b1,fu.peek())); fu.pop(); continue; } if(fu.peek()==‘-‘||fu.peek()==‘+‘){ int b1 = shu.pop(); int a1 = shu.pop(); shu.push(jisuan(a1,b1,fu.peek())); fu.pop(); } } fu.pop(); } } } while(fu.size()!=0){ int b1 = shu.pop(); int a1 = shu.pop(); shu.push(jisuan(a1,b1,fu.pop())); } System.out.println(shu.pop()); } public static int jisuan(int a,int b,char c){ int result=0; if(c==‘-‘){ result = a-b; } if(c==‘+‘){ result = a+b; } if(c==‘*‘){ result = a*b; } if(c==‘/‘){ result = a/b; } return result; } }
标签:system.in public while oid class 数据规模 inpu import ack
原文地址:http://www.cnblogs.com/lolybj/p/6512535.html