Java Swing实现计算器功能

学习Java Swing实现计算器功能,本文实现该系统的功能截图,和核心代码大家参考

Java Swing实现计算器功能

计算器支持混合四则运算
Java Swing实现计算器功能_第1张图片
Java Swing实现计算器功能_第2张图片
Java Swing实现计算器功能_第3张图片
Java Swing实现计算器功能_第4张图片
Java Swing实现计算器功能_第5张图片

核心代码

/* 监听运算符包括点 */
	private class CalculatorListener implements ActionListener {

		JTextField display;
		JButton str;

		CalculatorListener(JTextField display, JButton str) {

			this.display = display;
			this.str = str;
		}

		public void actionPerformed(ActionEvent e) {

			StringBuffer s = new StringBuffer(display.getText());
			if (flag) {
				display.setText("0");
				flag = false;
			} else if (!(s.length() == 0)) {

				if (!((display.getText().endsWith("+")) || (display.getText().endsWith("-"))
						|| (display.getText().endsWith("X")) || (display.getText().endsWith("/")))) {
					// 当为无穷和非数字型,清空
					if (display.getText().indexOf("Infinity") != -1 || display.getText().indexOf("NaN") != -1) {

						display.setText("0");
						// 判断前面的数是不是小数,以决定要不要在它后面加小数点
					} else if (str.getText() == "." && Character.isDigit(s.charAt(s.length() - 1))) {
						int lastIndex = -1;
						Stack<String> stack = new Stack<String>();
						for (int i = 0; i < s.length(); i++) {
							lastIndex = new CalculatorImpl().readDouble(display.getText(), i);
							stack.push(display.getText().substring(i, lastIndex));
							i = lastIndex;
						}
						String last = stack.pop();

						if (last.indexOf(".") == -1) {
							s.append(str.getText());
							display.setText(s.toString());
						}
						// 普通情况
					} else if (!display.getText().endsWith(".")) {
						s.append(str.getText());
						display.setText(s.toString());
					}
					// 当前面是符号时,自动填零
				} else if (str.getText() == "." && (s.charAt(s.length() - 1) == '+' || s.charAt(s.length() - 1) == '-'
						|| s.charAt(s.length() - 1) == 'X' || s.charAt(s.length() - 1) == '/')) {
					s.append("0.");
					display.setText(s.toString());

				}
				// 为空时,按点填零
			} else if (s.length() == 0 && str.getText() == ".") {
				s.append("0.");
				display.setText(s.toString());
			}
		}

	}
	/* 转换逆波兰式 */
	public double toReversePolishNotation(String s) throws NumberFormatException {

		Stack<Character> s1 = new Stack<Character>();//
		Stack<Double> s2 = new Stack<Double>();
		int lastIndex = -1;
		char temp;
		String num;

		for (int i = 0; i < s.length(); i++) {
			System.out.println("第" + i + "趟");
			temp = s.charAt(i);
			// 判断是否为操作数,如果是,就截取操作数
			if (Character.isDigit(temp) || temp == '.' || temp == '-') {
				// 对出现的前面的负号进行处理
				if (temp == '-') {
					lastIndex = readDouble(s, i + 1);
					num = s.substring(i, lastIndex);
					System.out.println("截取字符串" + num);
					s2.push(Double.parseDouble(num));
					System.out.println("s2 入栈" + s2.peek());
					System.out.println("s1 入栈  --> +");
					s1.push('+');
				} else {
					lastIndex = readDouble(s, i);
					num = s.substring(i, lastIndex);
					System.out.println("截取字符串" + num);
					s2.push(Double.parseDouble(num));
					System.out.println("s2 入栈" + s2.peek());
				}
				i = lastIndex - 1;
				System.out.println("当前i" + i);
			} else {
				// 乘除时,直接做运算
				if (temp == 'X' || temp == '/') {
					System.out.println("s2出栈:-->" + s2.peek());
					double num1 = s2.pop();
					lastIndex = readDouble(s, i + 1);
					double num2 = Double.parseDouble(s.substring(i + 1, lastIndex));
					System.out.println("操作数:" + num2);

					i = lastIndex - 1;
					System.out.println("当前i" + i);
					double result = calculate(num1, temp, num2);
					System.out.println("num1" + temp + " num2 =" + result);
					System.out.println("result 入栈 s2" + result);
					s2.push(result);
					// 当为加减时,让他直接入栈,等待操作
				} else if (temp == '+' || temp == '-') {
					System.out.println("s1 入栈" + temp);
					s1.push(temp);
				}
			}

		}
		// 对剩下的加减运算,得出最终结果
		while (!s1.isEmpty() && !s2.isEmpty() && s2.size() >= 2) {

			System.out.println("s2 出栈:" + s2.peek());
			double num2 = s2.pop();
			System.out.println("s2 出栈:" + s2.peek());
			double num1 = s2.pop();
			System.out.println("s1 出栈:" + s1.peek());
			double result = calculate(num1, s1.pop(), num2);
			System.out.println("计算结果为: " + result);
			s2.push(result);
		}

		return s2.pop();
	}

你可能感兴趣的:(Java,SE,java,计算器,混合四则运算)