用java 实现的计算器,原理看代码注释,具体内容如下
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
|
public class mycalculator { public static void main(string[] args) { string obj = "6+(8/2)+6/3+1*8 + 30" ; arraylist<string> arraylist = strformat(obj); system.out.println(obj + "=" + calculator(arraylist)); } /** * 采用后续表达式计算结果 * 1、当前字符串为数字时,直接入栈 * 2、当前字符串为计算符时,取出栈中的前两个计算 * 3、将计算结果再放入到栈中,栈中最后剩余的一个元素就是要求的结果 */ private static int calculator(arraylist<string> obj) { arraylist<string> result = transform(obj); system.out.println(result); stack<integer> stack = new stack<>(); for ( int i = 0 ; i < result.size(); i++) { string symbol = result.get(i); if (isdigital(symbol)) { //数字直接入栈 stack.push(integer.parseint(symbol)); } else { // 处理操作符 int num1, num2; num1 = stack.pop(); //取出两个数 num2 = stack.pop(); switch (symbol) { case "+" : stack.push(num2 + num1); break ; case "-" : stack.push(num2 - num1); break ; case "*" : stack.push(num2 * num1); break ; case "/" : stack.push(num2 / num1); break ; default : break ; } } } return stack.pop(); } /** * 中序遍历改为后续遍历 */ private static arraylist<string> transform(arraylist<string> arraylist) { stack<string> stack = new stack<>(); arraylist<string> result = new arraylist<>(); for ( int index = 0 ; index < arraylist.size(); index++) { string symbol = arraylist.get(index); if (isdigital(symbol)) { //如果是数字直接输出 result.add(symbol); } else if (symbol.equals( ")" )) { string tmp; while (!(tmp = stack.pop()).equals( "(" )) { // 匹配成功后停止 result.add(tmp); } } else { if (stack.isempty()) { stack.push(symbol); continue ; } string tmp = stack.peek(); while (outpriority(symbol) <= inpriority(tmp)) { //优先级小于栈内优先级,一直出栈 result.add(tmp); stack.pop(); if (stack.isempty()) { break ; } tmp = stack.peek(); } stack.push(symbol); } } //将剩余的出栈 while (!stack.isempty()) { result.add(stack.pop()); } return result; } /** * 首先对string 进行格式化 转化成arraylist * @param src 3*5+8; * @return arraylist 3 * 5 + 8 */ private static arraylist<string> strformat(string src) { if (src == null || src.equals( "" )) { return null ; } arraylist<string> arraylist = new arraylist<>(); stringbuilder comchar = new stringbuilder(); for ( int i = 0 ; i <src.length(); i++) { char ch = src.charat(i); if (ch == ' ' ) { continue ; //去除空格 } if (!character.isdigit(ch)) { if (!comchar.tostring().trim().equals( "" )) { arraylist.add(comchar.tostring().trim()); comchar.delete( 0 , comchar.length()); } arraylist.add(ch + "" ); continue ; } comchar.append(ch); } if (!comchar.tostring().trim().equals( "" )) { arraylist.add(comchar.tostring().trim()); } return arraylist; } /** * 判断是否为数字 * @param symbol 782 或者 + - * / * @return true or false */ private static boolean isdigital(string symbol) { return !symbol.equals( "+" ) && !symbol.equals( "-" ) && !symbol.equals( "*" ) && !symbol.equals( "/" ) && !symbol.equals( "(" ) && !symbol.equals( ")" ); } private static int inpriority(string ch) { switch (ch) { case "+" : case "-" : return 2 ; case "*" : case "/" : return 4 ; case ")" : return 7 ; case "(" : return 1 ; default : return 0 ; } } private static int outpriority(string ch) { switch (ch) { case "+" : case "-" : return 3 ; case "*" : case "/" : return 5 ; case ")" : return 1 ; case "(" : return 7 ; default : return 0 ; } } } |
以上全部为本篇文章的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/owenchan1987/article/details/79077333