输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2

  1. public class PracticeUtil {  
  2.   
  3.     public static void main(String[] args) {  
  4.         String s = "3+8×2/9-2 ";  
  5.         int result = getMyRet(s);  
  6.         System.out.println("最后结果:" + result);  
  7.     }  
  8.   
  9.     public static int getMyRet(String s1) {  
  10.         int len = s1.length();  
  11.         List<String> list = new ArrayList<String>();  
  12.         for (int i = 0; i < len; i++)  
  13.             list.add(s1.charAt(i) + "");  
  14.         System.out.println("list--->" + list);  
  15.         for (int j = 0; j < list.size(); j++) {  
  16.             if (list.get(j).equals("×")) {  
  17.                 int ji = Integer.parseInt(list.get(j - 1))  
  18.                         * Integer.parseInt(list.get(j + 1));  
  19.                 list.add(j - 1, ji + "");// 把ji插入到原来x的前一位,原来的后移。从8开始往后移  
  20.                 list.remove(j);// 删除8;remove是删除当前位置后后面的前移;故x到了j这个下标位置。  
  21.                 list.remove(j);// 删除x  
  22.                 list.remove(j);// 删除9  
  23.                 System.out.println("list--x后->" + list);// list--x后->[3, +, 16,  
  24.                                                         // /, 9, -, 2, ]  
  25.                 j--;// 相当于这次循环木有跳动下一个下标,因为马上要对ji参与运算,而不是跳过  
  26.             } else if (list.get(j).equals("/")) {  
  27.                 int shang = Integer.parseInt(list.get(j - 1))  
  28.                         / Integer.parseInt(list.get(j + 1));  
  29.                 list.add(j - 1, shang + "");  
  30.                 list.remove(j);  
  31.                 list.remove(j);  
  32.                 list.remove(j);  
  33.                 System.out.println("list--/后->" + list);// list--/后->[3, +, 1,  
  34.                                                         // -, 2, ]  
  35.                 j--;  
  36.             }  
  37.         }  
  38.         for (int k = 0; k < list.size(); k++) {// 这个时候是新的size  
  39.             if (list.get(k).equals("+")) {  
  40.                 int he = Integer.parseInt(list.get(k - 1))  
  41.                         + Integer.parseInt(list.get(k + 1));  
  42.                 list.add(k - 1, he + "");  
  43.                 list.remove(k);  
  44.                 list.remove(k);  
  45.                 list.remove(k);  
  46.                 System.out.println("list--+后->" + list); // list--+后->[4, -, 2,  
  47.                                                             // ]  
  48.                 k--;  
  49.             }  
  50.             if (list.get(k).equals("-")) {  
  51.                 int cha = Integer.parseInt(list.get(k - 1))  
  52.                         - Integer.parseInt(list.get(k + 1));  
  53.                 list.add(k - 1, cha + "");  
  54.                 list.remove(k);  
  55.                 list.remove(k);  
  56.                 list.remove(k);  
  57.                 System.out.println("list--  -后->" + list); // list-- -后->[2, ]  
  58.                                                             // k--;  
  59.             }  
  60.         }  
  61.         int sum = Integer.parseInt(list.get(0));  
  62.         return sum;  
  63.     }  
  64. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值