复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171package calc; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; /** * 利用中缀表达式 转换为 后缀表达式 进行计算 */ public class MyCalc { private static String allOperatorRange = "+-*/()"; public static void main(String[] args) { String str="3+4*3-(3*2+1)"; List<String> listStr = toStringArray(str); List<String> hzList = toHz(listStr); System.out.print("后缀表达式->"); for (String s : hzList) { System.out.print(s+","); } System.out.println(); String result = calc(hzList); System.out.println("运算结果->"+result); } /** * @param hzList * @return */ private static String calc(List<String> hzList) { List<String> stack = new ArrayList<>(); for (int i = 0; i < hzList.size(); i++) { //数字入栈 String element = hzList.get(i); if (!allOperatorRange.contains(element)) { stack.add(element); } else { //处理符号位 进行运算 int size = stack.size() - 1; BigDecimal num1 = new BigDecimal(stack.get(size)); stack.remove(size); size = stack.size() - 1; BigDecimal num2 = new BigDecimal(stack.get(size)); stack.remove(size); if (element.equals("*")) { num2 = num2.multiply(num1); }else if (element.equals("/")) { num2 = num2.divide(num1, 2, BigDecimal.ROUND_UP); }else if (element.equals("+")) { num2 = num2.add(num1); }else if (element.equals("-")) { num2 = num2.subtract(num1); } stack.add(num2.toString()); } } return stack.get(0); } public static List<String> toHz(List<String> list) { List<String> result = new ArrayList<>(); List<String> temp = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { //数字直接入栈 String element = list.get(i); if (!allOperatorRange.contains(list.get(i))) { result.add(element); } else { if (temp.size() == 0) { //栈为空 直接入栈 temp.add(element); } else { while (true) { if(temp.size()==0) { if (!element.equals(")")) { temp.add(element);//删除一次temp的栈顶元素后要再判断栈是否为空 } break;//如果tmep为空 结束循环 } if (isPush(temp.get(temp.size() - 1), element)) {//比较栈顶符号与当前符号的优先级 //处理括号内的符号 if (element.equals(")")) { for (int j = temp.size() - 1; j >= 0; j--) { if (temp.get(j).equals("(")) { temp.remove(j); break;//右括号删除后 结束for循环 前面的符号不在此处处理 } else { result.add(temp.get(j)); temp.remove(j); } } break;//处理完括号内的数据,要结束while循环 } else { temp.add(element); break; } } else { result.add(temp.get(temp.size() - 1));//将temp的栈顶元素放入result,并出栈 temp.remove(temp.get(temp.size() - 1)); } } } } } //将temp中剩下的运算符号按照倒序插入result if (temp.size() != 0) { for (int i=temp.size()-1;i>=0;i--) { result.add(temp.get(i)); } } return result; } /** * 是否入栈 * @return */ public static boolean isPush(String stack,String current) { if (stack.equals("(")) {//如果栈顶元素时(,则直接插入 return true; } if (getPriority(current) >= getPriority(stack)) return true; return false; } /** * 获取优先级 数字越大优先级越高 * @param str * @return */ public static int getPriority(String str) { if (str.equals("+") || str.equals("-")) { return 0; } if (str.equals("*") || str.equals("/")) { return 1; } if (str.equals("(") || str.equals(")")) { return 2; } return -1; } /** * 将string转化为数组 * @param str * @return */ private static List<String> toStringArray(String str) { List<String> arr = new ArrayList<>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.length(); i++) { if (!allOperatorRange.contains(String.valueOf(str.charAt(i)))) { sb.append(str.charAt(i)); if (i == str.length() - 1) { arr.add(sb.toString()); } } else { if (sb.length() != 0) { arr.add(sb.toString());//添加数字 sb = new StringBuilder(); } arr.add(String.valueOf(str.charAt(i)));//添加操作符 } } return arr; } }
如果发现问题,请在评论区指出
最后
以上就是有魅力项链最近收集整理的关于Java实现简单加减乘除计算器的全部内容,更多相关Java实现简单加减乘除计算器内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复