带有优先级的计算器的优先级设置与实现(Java语言)

您所在的位置:网站首页 优先级的计算 带有优先级的计算器的优先级设置与实现(Java语言)

带有优先级的计算器的优先级设置与实现(Java语言)

2024-07-05 07:07:44| 来源: 网络整理| 查看: 265

                                                              加减乘除以及左括号优先级设置为:

                                 

 

                                                        具体的处理方法: 用到了两个栈,一个符号栈,一个数字栈

                 符号栈的处理方式是: 单调栈的方式,只有当栈外符号的优先级高于栈顶符号的优先级时才会将栈外符号压入栈中。

                 数字栈的处理方式是: 没有处理方式,不管是什么数值的数字直接压入栈中即可。

 

package 图形界面; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Stack; import javax.swing.*; import javax.swing.JFrame; public class 计算器 { public static void main(String[] args) { JFrame frame = new JFrame("计算器"); frame.setSize(700,700); JPanel panel = new JPanel(); panel.setLayout(null); JLabel label1 = new JLabel("输入计算表达式: "); label1.setBounds(200,100,100,40); panel.add(label1); JTextField text1 = new JTextField(10); text1.setBounds(300,100,200,40); panel.add(text1); JButton button1 = new JButton("运算"); panel.add(button1); button1.setBounds(300,160,80,60); panel.add(new JLabel(" ")); JLabel label2 = new JLabel("结果:"); label2.setBounds(200,240,100,40); panel.add(label2); JTextField text2 = new JTextField(10); panel.add(text2); text2.setBounds(300,240,200,40); frame.add(panel); frame.setVisible(true); button1.addActionListener(new listen(button1,text1,text2)); } static class listen implements ActionListener { JButton button1; JTextField text1; JTextField text2; public listen(JButton button1,JTextField text1,JTextField text2){ this.button1 = button1; this.text1 = text1; this.text2 = text2; } public void actionPerformed(ActionEvent e){ String s = text1.getText(); System.out.println(s); Stack stk1 = new Stack(); Stack stk2 = new Stack(); for (int i = 0; i < s.length(); ++i){ char temp = s.charAt(i); // String 取其某一个元素 if ((temp >= '0' && temp = '0' && temp = '0' && temp = get_in_priority(stk2.peek().charValue())) stk2.push(new Character(temp)); else{ while(!stk2.empty() && (get_out_priority(temp) < get_in_priority(stk2.peek().charValue()))){ char c = stk2.peek().charValue(); stk2.pop(); double num2 = stk1.peek().doubleValue(); stk1.pop(); double num1 = stk1.peek().doubleValue(); stk1.pop(); if (c == '+') stk1.push(new Double(num1 + num2)); if (c == '-') stk1.push(new Double(num1 - num2)); if (c == '*') stk1.push(new Double(num1 * num2)); if (c == '/') stk1.push(new Double(num1 / num2)); } stk2.push(new Character(temp)); } } else if (temp == ')'){ // ')' while(stk2.peek().charValue() != '('){ char c = stk2.peek().charValue(); stk2.pop(); double num2 = stk1.peek().doubleValue(); stk1.pop(); double num1 = stk1.peek().doubleValue(); stk1.pop(); if (c == '+') stk1.push(new Double(num1 + num2)); if (c == '-') stk1.push(new Double(num1 - num2)); if (c == '*') stk1.push(new Double(num1 * num2)); if (c == '/') stk1.push(new Double(num1 / num2)); } stk2.pop(); } } if (!stk2.empty()){ while(!stk2.empty()){ char c = stk2.peek().charValue(); stk2.pop(); double num2 = stk1.peek().doubleValue(); stk1.pop(); double num1 = stk1.peek().doubleValue(); stk1.pop(); if (c == '+') stk1.push(new Double(num1 + num2)); if (c == '-') stk1.push(new Double(num1 - num2)); if (c == '*') stk1.push(new Double(num1 * num2)); if (c == '/') stk1.push(new Double(num1 / num2)); } } text2.setText((stk1.peek().toString())); System.out.println(stk1.peek().doubleValue()); } int get_out_priority(char temp){ if (temp == '+') return 3; if (temp == '-') return 3; if (temp == '*') return 4; if (temp == '/') return 4; if (temp == '(') return 5; return 0; } int get_in_priority(char temp){ if (temp == '+') return 3; if (temp == '-') return 3; if (temp == '*') return 4; if (temp == '/') return 4; if (temp == '(') return 0; return 0; } } }

 

 



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭