基于Java计算器 科学计算器与标准计算器相互转化

文件名:Calculator.java

package calculator;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * 
 * @author Fish·Potato·Chen
 *
 */

@SuppressWarnings("serial")
public class Calculator extends JFrame {
	private Container container = this.getContentPane();// 创建容器
	private GridBagLayout gridBagLayout = new GridBagLayout();// 使用GirdBagLayout布局器
	private GridBagConstraints gridBagConstraints = new GridBagConstraints();
	private JTextField displayFielddown;// 创建科学计算器的输入栏
	private JTextField displayFieldup;// 创建科学计算器的展示栏
	private JTextField displayField;// 创建标准计算器的输入栏
	private JMenuBar bar;// 菜单栏
	private JMenu set1;// 菜单栏中的项
	private JMenu set2;// 菜单栏中的项
	private JMenuItem itemsfunction[];// 菜单栏下的功能选项
	private JMenuItem itemshelp[];// 菜单栏下的帮助选项

	private String lastCommand = "=";

	int order = 0;
	int mode = 1;// 1为标准计算器2为科学计算器

	double result = 0;
	double x;

	private boolean start = true;
	boolean down = true;
	boolean up = true;
	boolean indown = false;

	public Calculator() {
		super("计算器");
		setLocation(100, 100);
		getContentPane().setBackground(Color.DARK_GRAY);
		container.setLayout(gridBagLayout);
		createmenu();
		initDisplayArea();
		initButtons();
		initFrame(900, 900);
	}

	private void createmenu() {
		// 创建菜单栏
		bar = new JMenuBar();
		set1 = new JMenu("切换");
		set2 = new JMenu("帮助");

		// 在每个菜单选项下添加项
		itemsfunction = new JMenuItem[2];
		itemshelp = new JMenuItem[3];

		// 菜单项每项下加入选项
		itemsfunction[0] = new JMenuItem("标准计算器");
		itemsfunction[1] = new JMenuItem("科学计算器");
		itemshelp[0] = new JMenuItem("使用说明");
		itemshelp[1] = new JMenuItem("开发者");
		itemshelp[2] = new JMenuItem("警告");

		// 创建标准计算器
		itemsfunction[0].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				if (mode != 1) {
					mode = 1;
					container.removeAll();
					initDisplayArea();
					initButtons();
					initFrame(900, 900);
				}
			}
		});

		// 创建科学计算器
		itemsfunction[1].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				if (mode != 2) {
					mode = 2;
					container.removeAll();
					initDisplayAreaup(0, 0, 20);
					initDisplayAreadown(0, 1, 40);
					initButtons();
					initFrame(1000, 800);
				}
			}
		});

		// 创建使用说明
		itemshelp[0].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				JOptionPane.showMessageDialog(null,
						"在科学计算器中,为了弥补x的y次方(x^y)与x开y次方(y√x)的的不足,我对两者使用方法进行了改进(以下将两者的按键统称\n为按键):\n\n        "
								+ "使用时先输入x(或一个等式并计算结果)之后点击按键,然后输入y(或另一个等式并计算结果),之后再次点击按键,便可得\n到值,该值"
								+ "还可以参与其他运算.\n\n例如:\n5^(6*5) : 先输入5,点击按钮,再输入6*5点击等于计算出结果,再次点击按钮得到结果.\n(15+2*3)√"
								+ "(8*7-2) : 先输入8*7-2,点击等于,计算结果,再点击按钮,之后输入15+2*3,点击等于,计算结果,最后点击按钮得到结果.",
						"帮助", JOptionPane.PLAIN_MESSAGE);
			}
		});

		// 创建开发者信息
		itemshelp[1].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				JOptionPane.showMessageDialog(null, "作者:Fish·Potato·Chen\nQQ:2411016277\n邮箱:Fishcqy@163.com", "开发者",
						JOptionPane.PLAIN_MESSAGE);
			}
		});

		// 创建警告
		itemshelp[2].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				JOptionPane.showMessageDialog(null, "如果计算器按动任何按键都无反应,点击清除(C)按键.", "警告", JOptionPane.PLAIN_MESSAGE);
			}
		});

		// 将创建的选项加入到各自的菜单栏下
		for (int i = 0; i < 2; i++) {
			set1.add(itemsfunction[i]);
			set1.add(new JSeparator());
		}
		for (int i = 0; i < 3; i++) {
			set2.add(itemshelp[i]);
			set2.add(new JSeparator());
		}
		bar.add(set1);
		bar.add(set2);
		Calculator.this.setJMenuBar(bar);
	}

	// 设置弹窗位置
	private void initFrame(int x, int y) {
		setSize(x, y);
		setVisible(true);
		setResizable(true);
		setForeground(Color.black);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}

	// 增加按钮
	private void initButtons() {
		// 标准计算器下的按钮
		if (mode == 1) {
			ActionListener insert = new Calculator.InsertAction();
			ActionListener command = new Calculator.CommandAction();
			addButton("←", 0, 1, 2, 1, insert, Color.DARK_GRAY);
			addButton("C", 2, 1, 1, 1, insert, Color.DARK_GRAY);
			addButton("7", 0, 2, 1, 1, insert, Color.BLACK);
			addButton("8", 1, 2, 1, 1, insert, Color.BLACK);
			addButton("9", 2, 2, 1, 1, insert, Color.BLACK);
			addButton("+", 3, 1, 1, 1, command, Color.DARK_GRAY);
			addButton("4", 0, 3, 1, 1, insert, Color.BLACK);
			addButton("5", 1, 3, 1, 1, insert, Color.BLACK);
			addButton("6", 2, 3, 1, 1, insert, Color.BLACK);
			addButton("-", 3, 2, 1, 1, command, Color.DARK_GRAY);
			addButton("1", 0, 4, 1, 1, insert, Color.BLACK);
			addButton("2", 1, 4, 1, 1, insert, Color.BLACK);
			addButton("3", 2, 4, 1, 1, insert, Color.BLACK);
			addButton("*", 3, 3, 1, 1, command, Color.DARK_GRAY);
			addButton("±", 0, 5, 1, 1, insert, Color.DARK_GRAY);
			addButton("0", 1, 5, 1, 1, insert, Color.BLACK);
			addButton(".", 2, 5, 1, 1, insert, Color.DARK_GRAY);
			addButton("/", 3, 4, 1, 1, command, Color.DARK_GRAY);
			addButton("=", 3, 5, 1, 1, command, Color.DARK_GRAY);
		}

		// 科学计算器下的按钮
		else if (mode == 2) {
			ActionListener command1 = new Calculator.Command1Action();
			ActionListener command2 = new Calculator.Command2Action();
			ActionListener command3 = new Calculator.Command3Action();
			ActionListener command4 = new Calculator.Command4Action();
			ActionListener command5 = new Calculator.Command5Action();
			ActionListener command6 = new Calculator.Command6Action();
			ActionListener command7 = new Calculator.Command7Action();
			addButton("√x", 0, 2, 1, 1, command1, Color.DARK_GRAY);
			addButton("x^y", 1, 2, 1, 1, command3, Color.DARK_GRAY);
			addButton("y√x", 2, 2, 1, 1, command3, Color.DARK_GRAY);
			addButton("x^2", 3, 2, 1, 1, command1, Color.DARK_GRAY);
			addButton("CE", 4, 2, 1, 1, command7, Color.DARK_GRAY);
			addButton("C", 5, 2, 1, 1, command7, Color.DARK_GRAY);
			addButton("←", 6, 2, 1, 1, command7, Color.DARK_GRAY);
			addButton("+", 7, 2, 1, 1, command6, Color.DARK_GRAY);
			addButton("10^x", 0, 3, 1, 1, command1, Color.DARK_GRAY);
			addButton("ln", 1, 3, 1, 1, command1, Color.DARK_GRAY);
			addButton("sin", 2, 3, 1, 1, command1, Color.DARK_GRAY);
			addButton("arcsin", 3, 3, 1, 1, command1, Color.DARK_GRAY);
			addButton("7", 4, 3, 1, 1, command5, Color.BLACK);
			addButton("8", 5, 3, 1, 1, command5, Color.BLACK);
			addButton("9", 6, 3, 1, 1, command5, Color.BLACK);
			addButton("-", 7, 3, 1, 1, command6, Color.DARK_GRAY);
			addButton("e^x", 0, 4, 1, 1, command1, Color.DARK_GRAY);
			addButton("e", 1, 4, 1, 1, command2, Color.DARK_GRAY);
			addButton("cos", 2, 4, 1, 1, command1, Color.DARK_GRAY);
			addButton("arccos", 3, 4, 1, 1, command1, Color.DARK_GRAY);
			addButton("4", 4, 4, 1, 1, command5, Color.BLACK);
			addButton("5", 5, 4, 1, 1, command5, Color.BLACK);
			addButton("6", 6, 4, 1, 1, command5, Color.BLACK);
			addButton("*", 7, 4, 1, 1, command6, Color.DARK_GRAY);
			addButton("n!", 0, 5, 1, 1, command1, Color.DARK_GRAY);
			addButton("π", 1, 5, 1, 1, command2, Color.DARK_GRAY);
			addButton("tan", 2, 5, 1, 1, command1, Color.DARK_GRAY);
			addButton("arctan", 3, 5, 1, 1, command1, Color.DARK_GRAY);
			addButton("1", 4, 5, 1, 1, command5, Color.BLACK);
			addButton("2", 5, 5, 1, 1, command5, Color.BLACK);
			addButton("3", 6, 5, 1, 1, command5, Color.BLACK);
			addButton("/", 7, 5, 1, 1, command6, Color.DARK_GRAY);
			addButton("(", 0, 6, 1, 1, command4, Color.DARK_GRAY);
			addButton(")", 1, 6, 1, 1, command4, Color.DARK_GRAY);
			addButton("%", 2, 6, 1, 1, command1, Color.DARK_GRAY);
			addButton("Ans", 3, 6, 1, 1, command2, Color.DARK_GRAY);
			addButton("±", 4, 6, 1, 1, command1, Color.DARK_GRAY);
			addButton("0", 5, 6, 1, 1, command5, Color.BLACK);
			addButton(".", 6, 6, 1, 1, command6, Color.DARK_GRAY);
			addButton("=", 7, 6, 1, 1, command6, Color.DARK_GRAY);
		}
	}

	/**
	 * 科学计算器上展示栏
	 * 
	 * @param x为布局器中左上角横坐标
	 * @param y为布局器中左上角纵坐标
	 * @param z为字体大小
	 */
	private void initDisplayAreaup(int x, int y, int z) {
		// 大小
		displayFieldup = new JTextField(20);
		displayFieldup.setHorizontalAlignment(4);
		// 颜色
		displayFieldup.setBackground(Color.DARK_GRAY);
		displayFieldup.setDisabledTextColor(Color.WHITE);
		// 不可键盘输入
		displayFieldup.setEnabled(false);
		// 字体
		displayFieldup.setFont(new Font("微软雅黑", Font.BOLD, z));
		// 布局器中位置与大小
		gridBagConstraints.gridx = x;
		gridBagConstraints.gridy = y;
		gridBagConstraints.gridwidth = 8;
		gridBagConstraints.gridheight = 1;
		gridBagConstraints.fill = 1;
		gridBagConstraints.weightx = 1;
		gridBagConstraints.weighty = 1;
		gridBagLayout.setConstraints(displayFieldup, gridBagConstraints);
		container.add(displayFieldup);
	}

	/**
	 * 科学计算器的下展示栏
	 * 
	 * @param x为布局器中左上角横坐标
	 * @param y为布局器中左上角纵坐标
	 * @param z为字体大小
	 */
	private void initDisplayAreadown(int x, int y, int z) {
		// 大小
		displayFielddown = new JTextField(20);
		displayFielddown.setHorizontalAlignment(4);
		// 颜色
		displayFielddown.setBackground(Color.DARK_GRAY);
		displayFielddown.setDisabledTextColor(Color.WHITE);
		// 不可键盘输入
		displayFielddown.setEnabled(false);
		// 字体
		displayFielddown.setFont(new Font("微软雅黑", Font.BOLD, z));
		// 布局器中位置与大小
		gridBagConstraints.gridx = x;
		gridBagConstraints.gridy = y;
		gridBagConstraints.gridwidth = 8;
		gridBagConstraints.gridheight = 1;
		gridBagConstraints.fill = 1;
		gridBagConstraints.weightx = 1;
		gridBagConstraints.weighty = 1;
		gridBagLayout.setConstraints(displayFielddown, gridBagConstraints);
		container.add(displayFielddown);
		Calculator.this.displayFielddown.setText("0");
	}

	/**
	 * 标准计算器的展示栏
	 */
	private void initDisplayArea() {
		// 大小
		displayField = new JTextField(20);
		displayField.setHorizontalAlignment(4);
		// 颜色
		displayField.setBackground(Color.DARK_GRAY);
		displayField.setDisabledTextColor(Color.WHITE);
		// 不可键盘输入
		displayField.setEnabled(false);
		// 字体
		displayField.setFont(new Font("微软雅黑", Font.BOLD, 50));
		// 布局器中位置与大小
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridwidth = 4;
		gridBagConstraints.gridheight = 1;
		gridBagConstraints.fill = 1;
		gridBagConstraints.weightx = 1;
		gridBagConstraints.weighty = 1;
		gridBagLayout.setConstraints(displayField, gridBagConstraints);
		container.add(displayField);
	}

	/**
	 * 
	 * @param label    标签
	 * @param column   布局器中左上角位置
	 * @param row      布局器中左上角位置
	 * @param with     宽
	 * @param height   高
	 * @param listener 监听鼠标点击
	 * @param color    颜色
	 */
	private void addButton(String label, int column, int row, int with, int height, ActionListener listener,
			Color color) {
		// 增加标签
		JButton button = new JButton(label);
		// 颜色
		button.setForeground(Color.WHITE);
		button.setBackground(color);
		// 标准计算器
		if (mode == 1)
			button.setFont(new Font("楷体", Font.BOLD, 36));
		// 科学计算器
		else if (mode == 2)
			button.setFont(new Font("楷体", Font.BOLD, 27));
		// 增加监听
		button.addActionListener(listener);
		// 去掉标签焦点框
		button.setFocusPainted(false);
		// 位置与大小
		gridBagConstraints.gridx = column;
		gridBagConstraints.gridy = row;
		gridBagConstraints.gridwidth = with;
		gridBagConstraints.gridheight = height;
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		gridBagConstraints.insets = new Insets(1, 1, 1, 1);
		gridBagLayout.setConstraints(button, gridBagConstraints);
		container.add(button);
	}

	/**
	 * 
	 * @param x 传入x与结果做加减乘除与百分
	 */
	public void calculate(double x) {
		if (this.lastCommand.equals("+")) {
			this.result += x;
		} else if (this.lastCommand.equals("-")) {
			this.result -= x;
		} else if (this.lastCommand.equals("*")) {
			this.result *= x;
		} else if (this.lastCommand.equals("/")) {
			this.result /= x;
		} else if (this.lastCommand.equals("=")) {
			this.result = x;
		} else if (this.lastCommand.equals("%")) {
			this.result /= 100;
		}
		this.displayField.setText("" + this.result);
	}

	/**
	 * 使用类中类 读取按键
	 * 
	 * @author Fish·Potato·Chen
	 *
	 */
	private class CommandAction implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			String command = evt.getActionCommand();
			if (Calculator.this.start) {
				Calculator.this.lastCommand = command;
			} else {
				Calculator.this.calculate(Double.parseDouble(Calculator.this.displayField.getText()));
				Calculator.this.lastCommand = command;
				Calculator.this.start = true;
			}
		}
	}

	/**
	 * 使用类中类 读取按键
	 * 
	 * @author Fish·Potato·Chen
	 *
	 */

	private class InsertAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String input = event.getActionCommand();
			if (Calculator.this.start) {
				Calculator.this.displayField.setText("");
				Calculator.this.start = false;
				if (input.equals("±"))
					Calculator.this.displayField.setText(Calculator.this.displayField.getText() + "-");
			}
			if (!input.equals("±")) {
				if (input.equals("←")) {
					String str = Calculator.this.displayField.getText();
					if (str.length() > 1)
						Calculator.this.displayField.setText(str.substring(0, str.length() - 1));
					else {
						Calculator.this.displayField.setText("0");
						Calculator.this.start = true;
					}
				} else if (input.equals("C")) {
					Calculator.this.displayField.setText("0");
					result = 0.0d;
					Calculator.this.start = true;
				} else {
					if (input.equals(".")) {
						if (Calculator.this.displayField.getText().trim().indexOf(".") == -1)
							Calculator.this.displayField.setText(Calculator.this.displayField.getText() + input);
					} else
						Calculator.this.displayField.setText(Calculator.this.displayField.getText() + input);
				}
			}
		}
	}

	private class Command1Action implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			String command = evt.getActionCommand();
			if (command.equals("√x"))
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.sqrt(Double.parseDouble(Calculator.this.displayFielddown.getText()))));
			else if (command.equals("x^2"))
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.pow(Double.parseDouble(Calculator.this.displayFielddown.getText()), 2)));
			else if (command.equals("10^x"))
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.pow(10, Double.parseDouble(Calculator.this.displayFielddown.getText()))));
			else if (command.equals("ln"))
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.log(Double.parseDouble(Calculator.this.displayFielddown.getText()))));
			else if (command.equals("sin"))
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.sin(Double.parseDouble(Calculator.this.displayFielddown.getText()))));
			else if (command.equals("arcsin"))
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.asin(Double.parseDouble(Calculator.this.displayFielddown.getText()))));
			else if (command.equals("e^x"))
				Calculator.this.displayFielddown.setText(String
						.valueOf(Math.pow(Math.E, Double.parseDouble(Calculator.this.displayFielddown.getText()))));
			else if (command.equals("cos"))
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.cos(Double.parseDouble(Calculator.this.displayFielddown.getText()))));
			else if (command.equals("arccos"))
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.acos(Double.parseDouble(Calculator.this.displayFielddown.getText()))));
			else if (command.equals("n!"))
				Calculator.this.displayFielddown.setText(String.valueOf(Calculator.this
						.factorial((int) Double.parseDouble(Calculator.this.displayFielddown.getText()))));

			else if (command.equals("tan"))
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.tan(Double.parseDouble(Calculator.this.displayFielddown.getText()))));
			else if (command.equals("arctan"))
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.atan(Double.parseDouble(Calculator.this.displayFielddown.getText()))));
			else if (command.equals("%"))
				Calculator.this.displayFielddown
						.setText(String.valueOf(Double.parseDouble(Calculator.this.displayFielddown.getText()) / 100));
			down = false;
			indown = false;
			if (command.equals("±"))
				if (Double.parseDouble(Calculator.this.displayFielddown.getText()) != 0) {
					Calculator.this.displayFielddown.setText(
							String.valueOf(-1 * (Double.parseDouble(Calculator.this.displayFielddown.getText()))));
				} else {
					down = true;
					indown = true;
				}
		}
	}

	private class Command2Action implements ActionListener {

		public void actionPerformed(ActionEvent evt) {
			String command2 = evt.getActionCommand();
			if (command2.equals("e"))
				Calculator.this.displayFielddown.setText(String.valueOf(Math.E));
			else if (command2.equals("π"))
				Calculator.this.displayFielddown.setText(String.valueOf(Math.PI));
			else if (command2.equals("Ans"))
				Calculator.this.displayFielddown.setText(String.valueOf(Calculator.this.result));
			down = false;
		}
	}

	private class Command3Action implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			String command3 = evt.getActionCommand();
			if (order == 1) {
				Calculator.this.displayFielddown.setText(
						String.valueOf(Math.pow(x, Double.parseDouble(Calculator.this.displayFielddown.getText()))));
				order = 0;
			} else if (order == 2) {
				Calculator.this.displayFielddown.setText(String
						.valueOf(Math.pow(x, 1 / Double.parseDouble(Calculator.this.displayFielddown.getText()))));
				order = 0;
			} else {
				x = Double.parseDouble(Calculator.this.displayFielddown.getText());
				Calculator.this.displayFielddown.setText("0");
				indown = false;
				down = true;
				if (command3.equals("x^y"))
					order = 1;
				else if (command3.contentEquals("y√x"))
					order = 2;
			}
		}
	}

	private class Command4Action implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			String command4 = evt.getActionCommand();
			if (command4.equals("(")) {
				Calculator.this.displayFieldup.setText(Calculator.this.displayFieldup.getText() + "(");
				Calculator.this.displayFielddown.setText("0");
			} else if (command4.equals(")"))
				if (down) {
					Calculator.this.displayFieldup.setText(Calculator.this.displayFieldup.getText() + ")");
					Calculator.this.displayFielddown.setText("0");
				} else {
					Calculator.this.displayFieldup.setText(Calculator.this.displayFieldup.getText()
							+ Calculator.this.displayFielddown.getText() + ")");
					Calculator.this.displayFielddown.setText("");
				}
			down = true;
		}
	}

	private class Command5Action implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			String command5 = evt.getActionCommand();
			if (down && !command5.contentEquals("0")) {
				Calculator.this.displayFielddown.setText(command5);
				down = false;
				indown = true;
			} else if (indown)
				Calculator.this.displayFielddown.setText(Calculator.this.displayFielddown.getText() + command5);
		}
	}

	private class Command6Action implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			String command6 = evt.getActionCommand();
			if (command6.equals(".")) {
				if (Calculator.this.displayFielddown.getText().trim().indexOf(".") == -1)
					Calculator.this.displayFielddown.setText(Calculator.this.displayFielddown.getText() + command6);
			} else if (command6.equals("=")) {
				Calculator.this.displayFieldup
						.setText(Calculator.this.displayFieldup.getText() + Calculator.this.displayFielddown.getText());
				if (!Calculator.this.displayFieldup.getText().equals("")) {
					try {
						Calculator.this.result = (double) new ScriptEngineManager().getEngineByName("js")
								.eval(Calculator.this.displayFieldup.getText() + "*1.0");
						// 上面这一行用不了就用下面的
						// Calculator.this.result = Double.parseDouble(String.valueOf(new
						// ScriptEngineManager().getEngineByName("js").eval(Calculator.this.displayFieldup.getText()
						// + "*1.0")));
						Calculator.this.displayFielddown.setText(String.valueOf(Calculator.this.result));
					} catch (ScriptException e) {
						Calculator.this.displayFielddown.setText("error");
						e.printStackTrace();
					}
				} else {
					Calculator.this.result = 0;
					Calculator.this.displayFielddown.setText(String.valueOf(Calculator.this.result));
				}
				Calculator.this.displayFieldup.setText("");
				down = true;
				indown = false;
			} else {
				if (Calculator.this.displayFielddown.getText().equals(""))

					Calculator.this.displayFieldup.setText(Calculator.this.displayFieldup.getText() + command6);
				else
					Calculator.this.displayFieldup.setText(Calculator.this.displayFieldup.getText()
							+ String.valueOf(Double.parseDouble(Calculator.this.displayFielddown.getText()))
							+ command6);
				Calculator.this.displayFielddown.setText("0");
				down = true;
			}
		}
	}

	private class Command7Action implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			String command7 = evt.getActionCommand();
			if (command7.equals("CE")) {
				Calculator.this.displayFielddown.setText("0");
				down = true;
				indown = false;
			} else if (command7.equals("C")) {
				Calculator.this.displayFielddown.setText("0");
				Calculator.this.displayFieldup.setText("");
				indown = false;
				down = true;
			} else {
				if (!indown) {
					Calculator.this.displayFielddown.setText("0");
					down = true;
				} else {
					String str = Calculator.this.displayFielddown.getText();
					if (str.length() > 1)
						Calculator.this.displayFielddown.setText(str.substring(0, str.length() - 1));
					else {
						Calculator.this.displayFielddown.setText("0");
						down = true;
						indown = false;
					}
				}
			}
		}
	}

	private int factorial(int n) {
		int m = n;
		if (n == 0)
			return 1;
		else if (n < 0) {
			n = Math.abs(n);
			for (int i = 1; i < n; i++)
				m *= i;
			return -1 * n;
		} else
			for (int i = 1; i < n; i++)
				m *= i;
		return m;
	}

	public static void main(String[] args) {
		new Calculator();
	}
}

其实GUI就是不断重复编写按键,所以GUI没有什么好说的(可能有读者过来就是为了看GUI,建议拷贝到机子上跑一下就明白了),后面的那些计算判断,就是硬判断,用最笨的方法进行求解。其实可以直接使用逆波兰表达式进行求解,我将在下一篇中用C++进行介绍。

BUG说明:
1.在科学计算器中无法输入0.几
2.在科学计算器中可以输入00.

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FishPotatoChen

谢谢您的支持,我会更努力的~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值