第二次作业
计算器登录页面与设计
(老师,能力有点差,数据库连不上,环境搭建不起来,学起来太难了,谅解一下吧,尽力了)
【实验内容】
1.设计一个包含登录界面的计算器软件,该软件可以实现第一次作业中的全部功能,同时可以保存用户的历史计算记录(保存数据最好使用数据库)。
【实验环境及开发工具】
1.使用visors绘制流程图。
2.使用idea编写软件。
【流程图设计】

(2)登录界面流程图

(3)计算器运行流程图

登录界面

注册

登录失败

注册失败

登录成功

运算示例

计算历史记录保存

后台用户记录保存

代码部分
1 //对按登录按钮做出的回应 2 if(cot) { 3 JOptionPane.showMessageDialog(null, "登录成功!"); 4 new Calculator(); 5 }else { 6 JOptionPane.showMessageDialog(null, "用户名或者密码错误,登录失败!"); 7 }
1 package denglu; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.io.*; 6 import Calculator.Calculator; 7 8 public class denglu extends JFrame{ 9 //创建文本标签和文本框 10 //创建一个标签,显示用户名 11 JLabel usernamel=new JLabel("用户名"); 12 //创建一个文本框,用于输入用户名,宽度为18 13 JTextField usernamet=new JTextField(18); 14 //创建一个标签,显示密码 15 JLabel usernuml=new JLabel("密码 "); 16 //创建一个密码框,用于输入密码,宽度为18 17 JPasswordField usernumt=new JPasswordField(18); 18 19 //创建一个容器用来储存 20 JPanel jp=new JPanel(); 21 22 //注册和登录的按钮 23 JButton jbutton1=new JButton("注册"); 24 JButton jbutton2=new JButton("登录"); 25 public denglu() { 26 27 Toolkit t=Toolkit.getDefaultToolkit();//工具类 28 Dimension d=t.getScreenSize(); 29 30 int height=(int)d.getHeight();//得到显示屏的高度 31 int width=(int)d.getWidth();//得到显示屏的宽度 32 this.setBounds((width-300)/2, (height-400)/2, 250, 200);//设置一个宽为250,高为200的窗口,并且让窗口居中 33 34 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口的同时,结束运行 35 this.setTitle("登录系统");//窗口标题 36 init(); 37 this.setVisible(true);//让窗口显示 38 } 39 public void init() { 40 //将内容添加到容器中 41 jp.add(usernamel); 42 jp.add(usernamet); 43 jp.add(usernuml); 44 jp.add(usernumt); 45 jp.add(jbutton1); 46 jp.add(jbutton2); 47 48 //添加监听器 49 //将用户名和密码写入文件中的操作 50 jbutton1.addActionListener(e -> { 51 try { 52 BufferedWriter w=new BufferedWriter(new FileWriter("D:/登录.txt",true)); 53 String sum=usernamet.getText()+" "+usernumt.getText();//中间加了空格是为了确保后续登录与文件数据匹配的稳定性 54 BufferedReader r=new BufferedReader(new FileReader("D:/登录.txt")); 55 boolean cot=true; 56 String s; 57 w.write(sum); 58 while((s=r.readLine())!=null) { 59 if(sum.equals(s)) { 60 cot=false;//如果符合其中一条数据,说明该数据就已经存在了,就不能在注册 61 } 62 } 63 if(cot) { 64 w.newLine(); 65 w.flush(); 66 w.close(); 67 JOptionPane.showMessageDialog(null, "注册成功!");//对按了注册按钮做出的回应 68 }else { 69 JOptionPane.showMessageDialog(null, "已经存在了,请更换用户名和密码!");//对按了注册按钮做出的回应 70 } 71 } catch (IOException e1) { 72 e1.printStackTrace(); 73 } 74 75 }); 76 77 jbutton2.addActionListener(e -> { 78 String sum=usernamet.getText()+" "+usernumt.getText();//中间加了空格是为了确保与文件数据匹配的稳定性 79 //对用户名和密码进行匹配 80 boolean cot=false; 81 String s; 82 try { 83 BufferedReader r=new BufferedReader(new FileReader("D:/登录.txt")); 84 while((s=r.readLine())!=null) { 85 if(s.equals(sum)) { 86 cot=true;//如果符合其中一条数据,就为登录成功 87 } 88 } 89 //对按登录按钮做出的回应 90 if(cot) { 91 JOptionPane.showMessageDialog(null, "登录成功!"); 92 new Calculator(); 93 }else { 94 JOptionPane.showMessageDialog(null, "用户名或者密码错误,登录失败!"); 95 } 96 } catch (Exception e1) { 97 e1.printStackTrace(); 98 } 99 }); 100 this.add(jp); 101 } 102 public static void main(String[] args) { 103 new denglu(); 104 } 105 }
1 package Calculator; 2 3 import javax.swing.*; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import java.util.*; 7 import java.util.stream.Collectors; 8 9 10 public class Calculator extends JFrame implements ActionListener { 11 public Calculator() { 12 init(); 13 } 14 15 /** 16 * 定义按钮 17 */ 18 private JTextField textField1; 19 private JTextArea textField2; 20 private JButton buttonzuo;//( 21 private JButton buttonyou;//) 22 private JButton buttonC;//c 23 private JButton buttonCE;//CE 24 private JButton buttondele;//<- 删除 25 private JButton buttonDiv;//÷ 26 private JButton button7;//7 27 private JButton button8;//8 28 private JButton button9;//9 29 private JButton buttonAdd;//+ 30 private JButton button4;//4 31 private JButton button5;//5 32 private JButton button6;//6 33 private JButton buttonSub;//- 34 private JButton button1;//1 35 private JButton button2;//2 36 private JButton button3;//3 37 private JButton buttonMul;//x 38 private JButton buttonequl;//= 39 private JButton button0;//0 40 private JButton buttonPoint;//. 41 42 public void init() { 43 JFrame frame = new JFrame("The Calculator Of 小马的东西别乱动"); //普通窗口顶层容器 创建标题为“计算器”; 44 frame.setLayout(null); //设置使用特定的布局管理器; 45 //单选项按钮 46 //按键的排版以及设计需要提前考虑整体的布局合理性,不可出现按键重叠;为了保证美观又需要考虑排版整齐; 47 //放置数字0 48 button0 = new JButton("0"); //注:在给按键命名时要根据按键的大小,否则按键上的标注无法完全显示; 49 button0.setBounds(100, 400, 110, 50); 50 frame.add(button0); 51 //放置数字1 52 button1 = new JButton("1"); 53 button1.setBounds(40, 340, 50, 50); 54 frame.add(button1); 55 //放置数字2 56 button2 = new JButton("2"); 57 button2.setBounds(100, 340, 50, 50); 58 frame.add(button2); 59 //放置数字3 60 button3 = new JButton("3"); 61 button3.setBounds(160, 340, 50, 50); 62 frame.add(button3); 63 //放置数字4 64 button4 = new JButton("4"); 65 button4.setBounds(40, 280, 50, 50); 66 frame.add(button4); 67 //放置数字5 68 button5 = new JButton("5"); 69 button5.setBounds(100, 280, 50, 50); 70 frame.add(button5); 71 //放置数字6 72 button6 = new JButton("6"); 73 button6.setBounds(160, 280, 50, 50); 74 frame.add(button6); 75 //放置数字7 76 button7 = new JButton("7"); 77 button7.setBounds(40, 220, 50, 50); 78 frame.add(button7); 79 //放置数字8 80 button8 = new JButton("8"); 81 button8.setBounds(100, 220, 50, 50); 82 frame.add(button8); 83 //放置数字9 84 button9 = new JButton("9"); 85 button9.setBounds(160, 220, 50, 50); 86 frame.add(button9); 87 //放置 . 88 buttonPoint = new JButton("."); //text:为自动生成的参数的解释词 89 buttonPoint.setBounds(40, 400, 50, 50); //自动补齐的参数解释词 90 frame.add(buttonPoint); 91 //放置 + 92 buttonAdd = new JButton("+"); 93 buttonAdd.setBounds(220, 400, 50, 50); 94 frame.add(buttonAdd); 95 //放置 - 96 buttonSub = new JButton("-"); 97 buttonSub.setBounds(220, 340, 50, 50); 98 frame.add(buttonSub); 99 //放置 * 100 buttonMul = new JButton("*"); 101 buttonMul.setBounds(220, 280, 50, 50); 102 frame.add(buttonMul); 103 //放置 / 104 buttonDiv = new JButton("/"); 105 buttonDiv.setBounds(220, 220, 50, 50); 106 frame.add(buttonDiv); 107 //放置 = 108 buttonequl = new JButton("="); 109 buttonequl.setBounds(280, 340, 110, 110); 110 frame.add(buttonequl); 111 //退位键 112 buttondele = new JButton("B"); 113 buttondele.setBounds(280, 220, 110, 110); 114 frame.add(buttondele); 115 //放置左括号( 116 buttonzuo = new JButton("("); 117 buttonzuo.setBounds(40, 160, 80, 50); 118 frame.add(buttonzuo); 119 //放置右括号) 120 buttonyou = new JButton(")"); 121 buttonyou.setBounds(130, 160, 80, 50); 122 frame.add(buttonyou); 123 //放置C 消除所有输入 124 buttonC = new JButton("C"); 125 buttonC.setBounds(220, 160, 80, 50); 126 frame.add(buttonC); 127 //放置CE 消除当前输入 128 buttonCE = new JButton("CE"); 129 buttonCE.setBounds(310, 160, 80, 50); 130 frame.add(buttonCE); 131 //添加表达式文本框 用以输入计算公式 132 textField1 = new JTextField(); //文本框 133 textField1.setBounds(40, 20, 350, 60); 134 frame.add(textField1); 135 textField2 = new JTextArea(); 136 textField2.setBounds(400, 20, 280, 430); 137 frame.add(textField2); 138 139 140 textField1.addActionListener(this); 141 buttonzuo.addActionListener(this); 142 buttonyou.addActionListener(this); 143 buttonC.addActionListener(this); 144 buttonCE.addActionListener(this); 145 buttondele.addActionListener(this); 146 buttonDiv.addActionListener(this); 147 button7.addActionListener(this); 148 button8.addActionListener(this); 149 button9.addActionListener(this); 150 buttonAdd.addActionListener(this); 151 button4.addActionListener(this); 152 button5.addActionListener(this); 153 button6.addActionListener(this); 154 buttonSub.addActionListener(this); 155 button1.addActionListener(this); 156 button2.addActionListener(this); 157 button3.addActionListener(this); 158 buttonMul.addActionListener(this); 159 buttonequl.addActionListener(this); 160 button0.addActionListener(this); 161 buttonPoint.addActionListener(this); 162 163 frame.setBounds(0, 0, 700, 520); //设置整个图形窗口的大小;(通过窗口名调用) 164 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE),当点击窗口的关闭按钮时退出程序(没有这一句,程序不会退出) 165 frame.setVisible(true); 166 } 167 168 String str = null; 169 int pointbook = 0; 170 int equalbook = 0; 171 int zuonum = 0; 172 int younum = 0; 173 int equnum = 0; 174 175 176 public void actionPerformed(ActionEvent e) { 177 if (equnum == 1) { 178 textField1.setText("0"); 179 equnum = 0; 180 } 181 //按0 182 if (e.getSource().equals(button0)) { 183 str = textField1.getText(); 184 if (str.length() > 16 || str.equals("0") || equalbook == 1) { 185 186 } else { 187 textField1.setText(str + "0"); 188 } 189 } 190 //按1 191 if (e.getSource().equals(button1)) { 192 str = textField1.getText(); 193 if (str.length() > 16 || equalbook == 1) { 194 195 } else if (str.equals("0") || str.equals("")) { 196 textField1.setText("1"); 197 } else { 198 textField1.setText(str + "1"); 199 } 200 } 201 //当按钮为2时 202 if (e.getSource().equals(button2)) { 203 str = textField1.getText(); 204 if (str.length() > 16 || equalbook == 1) { 205 } else if (str.equals("0") || str.equals("")) { 206 textField1.setText("2"); 207 } else { 208 textField1.setText(str + "2"); 209 } 210 } 211 //当按钮为3时 212 if (e.getSource().equals(button3)) { 213 str = textField1.getText(); 214 if (str.length() > 16 || equalbook == 1) { 215 } else if (str.equals("0") || str.equals("")) { 216 textField1.setText("3"); 217 } else { 218 textField1.setText(str + "3"); 219 } 220 } 221 //当按钮为4时 222 if (e.getSource().equals(button4)) { 223 str = textField1.getText(); 224 if (str.length() > 16 || equalbook == 1) { 225 } else if (str.equals("0") || str.equals("")) { 226 textField1.setText("4"); 227 } else { 228 textField1.setText(str + "4"); 229 } 230 } 231 //当按钮为5时 232 if (e.getSource().equals(button5)) { 233 str = textField1.getText(); 234 if (str.length() > 16 || equalbook == 1) { 235 } else if (str.equals("0") || str.equals("")) { 236 textField1.setText("5"); 237 } else { 238 textField1.setText(str + "5"); 239 } 240 } 241 //当按钮为6时 242 if (e.getSource().equals(button6)) { 243 str = textField1.getText(); 244 if (str.length() > 16 || equalbook == 1) { 245 } else if (str.equals("0") || str.equals("")) { 246 textField1.setText("6"); 247 } else { 248 textField1.setText(str + "6"); 249 } 250 } 251 //当按钮为7时 252 if (e.getSource().equals(button7)) { 253 str = textField1.getText(); 254 if (str.length() > 16 || equalbook == 1) { 255 } else if (str.equals("0") || str.equals("")) { 256 textField1.setText("7"); 257 } else { 258 textField1.setText(str + "7"); 259 } 260 } 261 //当按钮为8时 262 if (e.getSource().equals(button8)) { 263 str = textField1.getText(); 264 if (str.length() > 16 || equalbook == 1) { 265 } else if (str.equals("0") || str.equals("")) { 266 textField1.setText("8"); 267 } else { 268 textField1.setText(str + "8"); 269 } 270 } 271 //当按钮为9时 272 if (e.getSource().equals(button9)) { 273 str = textField1.getText(); 274 if (str.length() > 16 || equalbook == 1) { 275 } else if (str.equals("0") || str.equals("")) { 276 textField1.setText("9"); 277 } else { 278 textField1.setText(str + "9"); 279 } 280 } 281 //当按钮为小数点时 282 if (e.getSource().equals(buttonPoint)) { 283 str = textField1.getText(); 284 if (str.length() > 15 || equalbook == 1) { //小数点位于操作数的最后一位不执行操作; 285 } 286 if (pointbook == 0) { //一个操作数仅能有一个小数点,若已经有小数点则不再添加小数点; 287 textField1.setText(str + "."); 288 pointbook = 1; //小数点判断位置1; 289 } 290 } 291 //每次输入都是一个数字+运算符,一起从数字文本框转而输入到表达式文本框中; 292 //当按钮为加号时 293 if (e.getSource().equals(buttonAdd)) { 294 str = textField1.getText(); //获取运算符前一个操作数; 295 char ch1[] = str.toCharArray(); //把第一操作数连同+号 进行字符串转字符数组的操作 赋予ch1; 296 int length1 = str.length() - 1; //length1获取除+号外的第一操作数的位数; 297 if ((length1 == -1 || ch1[length1] != ')') && (str.equals("0") || str.equals("") || ch1[length1] == '.' || ch1[length1] == '+' || ch1[length1] == '-' || ch1[length1] == '*' || ch1[length1] == '/' || ch1[length1] == '(' || ch1[length1] == ')')) { 298 //当数字为空或为0(操作无意义);或数字的最后一位是小数点(未输入完毕或输入出错,等待) 299 } else { 300 textField1.setText(str + "+"); //合并现有表达式和新增表达式 301 // 这里解释以下为什么s没有提取到数字框里的符号,因为输入符号时并没有更新数字框,而是直接执行一系列操作,数字框从未出现过运算符; 302 } 303 pointbook = 0; 304 } 305 //当按钮为减号时 306 if (e.getSource().equals(buttonSub)) { 307 str = textField1.getText(); 308 char ch1[] = str.toCharArray(); 309 int length1 = str.length() - 1; 310 if ((length1 == -1 || ch1[length1] != ')') && (ch1[length1] == '.' || ch1[length1] == '+' || ch1[length1] == '-' || ch1[length1] == '*' || ch1[length1] == '/' || ch1[length1] == '(' || ch1[length1] == ')')) { 311 } else { 312 textField1.setText(str + "-"); 313 } 314 pointbook = 0; 315 } 316 //当按钮为乘号时 317 if (e.getSource().equals(buttonMul)) { 318 str = textField1.getText(); 319 char ch1[] = str.toCharArray(); 320 int length1 = str.length() - 1; 321 if ((length1 == -1 || ch1[length1] != ')') && (str.equals("0") || str.equals("") || ch1[length1] == '.' || ch1[length1] == '+' || ch1[length1] == '-' || ch1[length1] == '*' || ch1[length1] == '/' || ch1[length1] == '(' || ch1[length1] == ')')) { 322 } else { 323 textField1.setText(str + "*"); 324 } 325 pointbook = 0; 326 } 327 //当按钮为除号时 328 if (e.getSource().equals(buttonDiv)) { 329 str = textField1.getText(); 330 char ch1[] = str.toCharArray(); 331 int length1 = str.length() - 1; 332 if ((length1 == -1 || ch1[length1] != ')') && (str.equals("0") || str.equals("") || ch1[length1] == '.' || ch1[length1] == '+' || ch1[length1] == '-' || ch1[length1] == '*' || ch1[length1] == '/' || ch1[length1] == '(' || ch1[length1] == ')')) { 333 } else { 334 textField1.setText(str + "/"); 335 } 336 pointbook = 0; 337 } 338 //当按钮为左括号时 339 if (e.getSource().equals(buttonzuo)) { 340 str = textField1.getText(); 341 char ch[] = str.toCharArray(); 342 int length = str.length() - 1; 343 if (length == -1 || ch[length] == '+' || ch[length] == '-' || ch[length] == '*' || ch[length] == '/') { 344 //括号左边是否有数或符号类别的判断; 345 textField1.setText(str + '('); //满足条件则加入左括号; 346 zuonum++; //左括号数加一标记; 347 } 348 if (length == -1 || ch[length] == '+' || ch[length] == '-' || ch[length] == '*' || ch[length] == '/') 349 pointbook = 0; 350 if (length == 0 || ch[length] == 0) { 351 textField1.setText("("); 352 zuonum++; 353 } 354 } 355 //当按钮为右括号时; 356 if (e.getSource().equals(buttonyou)) { 357 str = textField1.getText(); 358 char ch[] = str.toCharArray(); 359 int length = str.length() - 1; 360 if (Character.isDigit(ch[length]) && zuonum > younum) { //只有前面是数字的时候且左括号的数量大于右括号的数量的时候才能加右括号; 361 younum++; //右括号数加一标记; 362 textField1.setText(str + ')'); 363 } 364 pointbook = 0; 365 } 366 //当按下C键时; 367 if (e.getSource().equals(buttonC)) { 368 textField1.setText("0"); //置当前数字框为0; 369 zuonum = 0; //当一次计算完成之后,只有按CE按钮才能进行新的计算,因为要清除所有标志位否则会影响下一次操作; 370 younum = 0; 371 pointbook = 0; 372 equalbook = 0; 373 textField2.setText(" "); 374 } 375 //当按钮为CE时, 376 if (e.getSource().equals(buttonCE)) { 377 textField1.setText("0"); //清除当前数字框中内容; 378 pointbook = 0; //更新小数点状态为0; 379 } 380 //当按下B时, 381 if (e.getSource().equals(buttondele)) { 382 str = textField1.getText(); 383 char []nums=str.toCharArray(); 384 if (nums[str.length()-1]=='('){ 385 zuonum--; 386 } 387 str = str.substring(0, str.length() - 1); 388 textField1.setText(str); 389 } 390 //当按下=时, 391 if (e.getSource().equals(buttonequl)) { 392 str = textField1.getText(); 393 if (zuonum != younum) { 394 textField1.setText("关系式错误。"); 395 } else { 396 ans(str); 397 } 398 String s = str + "=" + textField1.getText(); 399 textField2.setText(s + "\r\n" + textField2.getText()); //将表达式存放在历史记录里。 400 equnum = 1; 401 } 402 } 403 404 405 /** 406 * 提前将 符号的优先级定义好 407 */ 408 private static final Map<Character, Integer> basic = new HashMap<Character, Integer>(); 409 410 static { 411 basic.put('-', 1); 412 basic.put('+', 1); 413 basic.put('*', 2); 414 basic.put('/', 2); 415 basic.put('(', 0);//在运算中 ()的优先级最高,但是此处因程序中需要 故设置为0 416 } 417 418 419 public void ans(String str) { 420 String a = toSuffix(str);//传入 一串 算数公式 421 textField1.setText(dealEquation(a)); 422 423 } 424 425 /** 426 * 将 中缀表达式 转化为 后缀表达式 427 */ 428 public String toSuffix(String infix) { 429 List<String> queue = new ArrayList<String>(); //定义队列 用于存储 数字 以及最后的 后缀表达式 430 List<Character> stack = new ArrayList<Character>(); //定义栈 用于存储 运算符 最后stack中会被 弹空 431 432 char[] charArr = infix.trim().toCharArray(); //字符数组 用于拆分数字或符号 433 String standard = "*/+-()"; //判定标准 将表达式中会出现的运算符写出来 434 char ch = '&'; //在循环中用来保存 字符数组的当前循环变量的 这里仅仅是初始化一个值 没有意义 435 int len = 0; //用于记录字符长度 【例如100*2,则记录的len为3 到时候截取字符串的前三位就是数字】 436 for (int i = 0; i < charArr.length; i++) { //开始迭代 437 438 ch = charArr[i]; //保存当前迭代变量 439 if (Character.isDigit(ch)) { //如果当前变量为 数字 440 len++; 441 } else if (Character.isLetter(ch)) { //如果当前变量为 字母 442 len++; 443 } else if (ch == '.') { //如果当前变量为 . 会出现在小数里面 444 len++; 445 } else if (Character.isSpaceChar(ch)) { //如果当前变量为 空格 支持表达式中有空格出现 446 if (len > 0) { //若为空格 代表 一段结束 ,就可以往队列中 存入了 【例如100 * 2 100后面有空格 就可以将空格之前的存入队列了】 447 queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len, i))); //往 队列存入 截取的 字符串 448 len = 0; //长度置空 449 } 450 continue; //如果空格出现,则一段结束 跳出本次循环 451 } else if (standard.indexOf(ch) != -1) { //如果是上面标准中的 任意一个符号 452 if (len > 0) { //长度也有 453 queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len, i))); //说明符号之前的可以截取下来做数字 454 len = 0; //长度置空 455 } 456 if (ch == '(') { //如果是左括号 457 stack.add(ch); //将左括号 放入栈中 458 continue; //跳出本次循环 继续找下一个位置 459 } 460 if (!stack.isEmpty()) { //如果栈不为empty 461 int size = stack.size() - 1; //获取栈的大小-1 即代表栈最后一个元素的下标 462 boolean flag = false; //设置标志位 463 while (size >= 0 && ch == ')' && stack.get(size) != '(') { //若当前ch为右括号,则 栈里元素从栈顶一直弹出,直到弹出到 左括号 464 queue.add(String.valueOf(stack.remove(size))); //注意此处条件:ch并未入栈,所以并未插入队列中;同样直到找到左括号的时候,循环结束了,所以左括号也不会放入队列中【也就是:后缀表达式中不会出现括号】 465 size--; //size-- 保证下标永远在栈最后一个元素【栈中概念:指针永远指在栈顶元素】 466 flag = true; //设置标志位为true 表明一直在取()中的元素 467 } 468 while (size >= 0 && !flag && basic.get(stack.get(size)) >= basic.get(ch)) { //若取得不是()内的元素,并且当前栈顶元素的优先级>=对比元素 那就出栈插入队列 469 queue.add(String.valueOf(stack.remove(size))); //同样 此处也是remove()方法,既能得到要获取的元素,也能将栈中元素移除掉 470 size--; 471 } 472 } 473 if (ch != ')') { //若当前元素不是右括号 474 stack.add(ch); //就要保证这个符号 入栈 475 } else { //否则就要出栈 栈内符号 476 stack.remove(stack.size() - 1); 477 } 478 } 479 if (i == charArr.length - 1) { //如果已经走到了 中缀表达式的最后一位 480 if (len > 0) { //如果len>0 就截取数字 481 queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len + 1, i + 1))); 482 } 483 int size = stack.size() - 1; //size表示栈内最后一个元素下标 484 while (size >= 0) { //一直将栈内 符号全部出栈 并且加入队列中 【最终的后缀表达式是存放在队列中的,而栈内最后会被弹空】 485 queue.add(String.valueOf(stack.remove(size))); 486 size--; 487 } 488 } 489 490 } 491 return queue.stream().collect(Collectors.joining(",")); //将队列中元素以,分割 返回字符串 492 } 493 494 495 /** 496 * 将 后缀表达式 进行 运算 计算出结果 497 * 498 * @param equation 499 * @return 500 */ 501 public String dealEquation(String equation) { 502 String[] arr = equation.split(","); //根据, 拆分字符串 503 List<String> list = new ArrayList<String>(); //用于计算时 存储运算过程的集合【例如list中当前放置 100 20 5 / 则取出20/5 最终将结果4存入list 此时list中结果为 100 4 】 504 505 506 for (int i = 0; i < arr.length; i++) { //此处就是上面说的运算过程, 因为list.remove的缘故,所以取出最后一个数个最后两个数 都是size-2 507 int size = list.size(); 508 switch (arr[i]) { 509 case "+": 510 double a = Double.parseDouble(list.remove(size - 2)) + Double.parseDouble(list.remove(size - 2)); 511 list.add(String.valueOf(a)); 512 break; 513 case "-": 514 double b = Double.parseDouble(list.remove(size - 2)) - Double.parseDouble(list.remove(size - 2)); 515 list.add(String.valueOf(b)); 516 break; 517 case "*": 518 double c = Double.parseDouble(list.remove(size - 2)) * Double.parseDouble(list.remove(size - 2)); 519 list.add(String.valueOf(c)); 520 break; 521 case "/": 522 double d = Double.parseDouble(list.remove(size - 2)) / Double.parseDouble(list.remove(size - 2)); 523 list.add(String.valueOf(d)); 524 break; 525 default: 526 list.add(arr[i]); 527 break; //如果是数字 直接放进list中 528 } 529 } 530 return list.size() == 1 ? list.get(0) : "运算失败"; //最终list中仅有一个结果,否则就是算错了 531 } 532 533 public static void main(String[] args) { 534 Calculator calculator = new Calculator(); 535 } 536 }

浙公网安备 33010602011771号