简单的计算器

计算机

可以加减乘除带括号
功能:
在这里插入图片描述
代码

package calculation;

import java.awt.Button;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.WindowEvent;
import java.util.Deque;
import java.util.LinkedList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;

/**
 * @author 冰镇西瓜
 *时间:2020年12月26日16:23:36
 */
public class Calcultion {
	public static void main(String[] args) {
		Procedure p = new Procedure();
		p.start();
	}
}

class Procedure{
	public void start() {
		Frame f = new Frame();
		
		TextField tf1 = new TextField();
		TextField tf2 = new TextField();
		tf1.setEditable(false);
		tf2.setEditable(false);
		f.setLayout(null);
		
		Button bn_c = new Button("C");
		Button bn_chu = new Button("/");
		Button bn_cheng = new Button("*");
		Button bn_del = new Button("DEL");
		Button bn_jian = new Button("-");
		Button bn_jia = new Button("+");
		Button bn_deng = new Button("=");
		Button bn_dian = new Button(".");
		Button bn_quyu = new Button("%"); 
		Button bn_zuokh = new Button(")");
		Button bn_youkh = new Button("(");
		Button bn_1 = new Button("1"); 
		Button bn_2 = new Button("2"); 
		Button bn_3 = new Button("3"); 
		Button bn_4 = new Button("4"); 
		Button bn_5 = new Button("5"); 
		Button bn_6 = new Button("6"); 
		Button bn_7 = new Button("7"); 
		Button bn_8 = new Button("8"); 
		Button bn_9 = new Button("9"); 
		Button bn_0 = new Button("0");
		
		f.setBounds(500, 200, 238,420);
		tf1.setBounds(20, 30, 200, 40);
		tf2.setBounds(20, 70, 200, 30);
		bn_c.setBounds(20, 100, 50,50);
		bn_7.setBounds(20, 150, 50,50);
		bn_4.setBounds(20, 200, 50,50);
		bn_1.setBounds(20, 250, 50,50);
		bn_quyu.setBounds(20, 300, 50,50);
		bn_chu.setBounds(70, 100, 50,50);
		bn_8.setBounds(70, 150, 50,50);
		bn_5.setBounds(70, 200, 50,50);
		bn_2.setBounds(70, 250, 50,50);
		bn_0.setBounds(70, 300, 50,50);
		bn_cheng.setBounds(120, 100, 50,50);
		bn_9.setBounds(120, 150, 50,50);
		bn_6.setBounds(120, 200, 50,50);
		bn_3.setBounds(120, 250, 50,50);
		bn_dian.setBounds(120, 300, 50,50);
		bn_del.setBounds(170, 100, 50,50);
		bn_jian.setBounds(170, 150, 50,50);
		bn_jia.setBounds(170, 200, 50,50);
		bn_deng.setBounds(170, 250, 50,100);
		bn_youkh.setBounds(20, 350, 100,50);
		bn_zuokh.setBounds(120, 350, 100,50);
		
		f.add(tf1);
		f.add(tf2);
		f.add(bn_c);
		f.add(bn_chu);
		f.add(bn_cheng);
		f.add(bn_del);
		f.add(bn_jia);
		f.add(bn_jian);
		f.add(bn_deng);
		f.add(bn_dian);
		f.add(bn_quyu);
		f.add(bn_zuokh);
		f.add(bn_youkh);
		f.add(bn_1);
		f.add(bn_2);
		f.add(bn_3);
		f.add(bn_4);
		f.add(bn_5);
		f.add(bn_6);
		f.add(bn_7);
		f.add(bn_8);
		f.add(bn_9);
		f.add(bn_0);
		
		bn_c.addActionListener(new BnC(bn_c, tf1, tf2));
		bn_chu.addActionListener(new BnAction(bn_chu, tf1, tf2));
		bn_cheng.addActionListener(new BnAction(bn_cheng, tf1, tf2));
		bn_del.addActionListener(new BnDel(bn_del, tf1, tf2));
		bn_jia.addActionListener(new BnAction(bn_jia, tf1, tf2));
		bn_jian.addActionListener(new BnAction(bn_jian, tf1, tf2));
		bn_deng.addActionListener(new DengAction(bn_deng, tf1, tf2));
		bn_dian.addActionListener(new BnAction(bn_dian, tf1, tf2));
		bn_quyu.addActionListener(new BnAction(bn_quyu, tf1, tf2));
		bn_zuokh.addActionListener(new BnAction(bn_zuokh, tf1, tf2));
		bn_youkh.addActionListener(new BnAction(bn_youkh, tf1, tf2));
		bn_0.addActionListener(new BnAction(bn_0, tf1, tf2));
		bn_1.addActionListener(new BnAction(bn_1, tf1, tf2));
		bn_2.addActionListener(new BnAction(bn_2, tf1, tf2));
		bn_3.addActionListener(new BnAction(bn_3, tf1, tf2));
		bn_4.addActionListener(new BnAction(bn_4, tf1, tf2));
		bn_5.addActionListener(new BnAction(bn_5, tf1, tf2));
		bn_6.addActionListener(new BnAction(bn_6, tf1, tf2));
		bn_7.addActionListener(new BnAction(bn_7, tf1, tf2));
		bn_8.addActionListener(new BnAction(bn_8, tf1, tf2));
		bn_9.addActionListener(new BnAction(bn_9, tf1, tf2));
		f.setResizable(false); 	
		f.addWindowListener(new Close());
		f.setVisible(true);
	}
	
	class Close extends WindowAdapter{
		public void windowClosing(WindowEvent e) {
			System.exit(-1);
		}
	}
	
	class BnC implements ActionListener{
		Button bn;
		TextField tf1, tf2;
		public BnC(Button bn, TextField tf1, TextField tf2) {
			this.bn = bn;
			this.tf1 = tf1;
			this.tf2 = tf2;
		}
		public void actionPerformed(ActionEvent e) {
			tf1.setText("");
		}
	}
	class BnDel implements ActionListener{
		Button bn;
		TextField tf1, tf2;
		public BnDel(Button bn, TextField tf1, TextField tf2) {
			this.bn = bn;
			this.tf1 = tf1;
			this.tf2 = tf2;
		}
		public void actionPerformed(ActionEvent e) {
			String temptext = tf1.getText();
			int len = temptext.length();
			if (len != 0)
			tf1.setText(temptext.substring(0, len-1));
		}
	}
	class BnAction implements ActionListener{
		Button bn;
		TextField tf1, tf2;
		public BnAction(Button bn, TextField tf1, TextField tf2) {
			this.bn = bn;
			this.tf1 = tf1;
			this.tf2 = tf2;
		}
		public void actionPerformed(ActionEvent e) {
			String s = bn.getLabel();
			String temptext = tf1.getText();
			tf1.setText(temptext + s);
		}
	}
	class DengAction implements ActionListener{
		Button bn;
		TextField tf1, tf2;
		public DengAction(Button bn, TextField tf1, TextField tf2) {
			this.bn = bn;
			this.tf1 = tf1;
			this.tf2 = tf2;
		}
		public void actionPerformed(ActionEvent e) {
			String text = tf1.getText();
			tf2.setText(calculate(text)+"");
		}
		  public int calculate(String s) {

		        int[] i = new int[1];
		        return dfs(s, i);
		    }
		    private int dfs(String s, int[] i){
		        Deque<Integer> stack = new LinkedList<>();

		        int num = 0;
		        char op = '+';
		        for(; i[0] < s.length(); i[0]++){
		            char ch = s.charAt(i[0]);

		            if(ch == '('){
		                ++i[0];
		                num = dfs(s, i);
		            }
		            
		            if(Character.isDigit(ch)){
		                num = num * 10 + (ch - '0');
		            }
		            if(!Character.isDigit(ch) && ch != ' ' || i[0] == s.length() - 1){
		                switch(op){
		                    case '+':
		                        stack.push(num); break;
		                    case '-':
		                        stack.push(-num); break;
		                    case '*':
		                        int pre = stack.pop();
		                        stack.push(pre * num);
		                        break;
		                    case '/':
		                        pre = stack.pop();
		                        stack.push(pre / num);
		                        break;
		                }
		                num = 0;
		                op = ch;
		            }
		            if(ch == ')'){
		                break;
		            }
		        }
		        int res = 0;
		        while(!stack.isEmpty()){
		            res += stack.pop();
		        }
		        return res;
		    }
	}
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值