文章目录
- 需求分析
- 设计思路
- 设计实现
- 设计实现展示
- 其他功能
需求分析
- 目的是实现一个基于Java的可以求解带括号加减乘除表达式的带界面的计算器。
- 需要知道的Java技术:Java Swing(Java图形界面设计)、Java集合(栈)、lambda表达式、Java基础等。
设计思路
- 实现一个Java计算器界面类
- 实现一个Java计算带括号加减乘除表达式的类
- 实现主函数调用
设计实现
- Java计算器项目结构:
Calculator类为计算器界面设计、Calculate类为计算带括号加减乘除表达式的类,Main函数为项目程序入口。
- Java计算器界面设计实现代码:
package Calculator;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame{
private double result=0;
private int count=0;
public Calculator() {
this.setSize(330,399);
this.setTitle("计算器");
init();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
this.setLayout(new BorderLayout());
JTextField textField=new JTextField();
textField.disable();
textField.setPreferredSize(new Dimension(this.getWidth(),50));
this.add(textField,BorderLayout.NORTH);
JPanel panel=new JPanel();
panel.setLayout(new BorderLayout());
this.add(panel, BorderLayout.CENTER);
JPanel panelN=new JPanel();
panelN.setLayout(new GridLayout(1,6));
JButton MC=new JButton("MC");
JButton MR=new JButton("MR");
JButton M0=new JButton("M+");
JButton M1=new JButton("M-");
JButton MS=new JButton("MS");
JButton M=new JButton("M");
panelN.add(MC);panelN.add(MR);panelN.add(M0);
panelN.add(M1);panelN.add(MS);panelN.add(M);
panel.add(panelN,BorderLayout.NORTH);
CardLayout cardLayout=new CardLayout();
JPanel panelC=new JPanel();
panelC.setLayout(cardLayout);
JPanel panel0=new JPanel();
panel0.setLayout(new GridLayout(6,4));
JButton[] standredButton=new JButton[24];
String str[]={"%","√","x²","1/x",
"CE","C","×","/",
"7","8","9","*",
"4","5","6","-",
"1","2","3","+",
"±","0",".","="
};
for(int i=0;i<standredButton.length;i++) {
standredButton[i]=new JButton(str[i]);
String text=standredButton[i].getText();
standredButton[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(text.equals("CE")||text.equals("C")) {
textField.setText("");
}
else if(text.equals("=")) {
String expression=textField.getText();
Calculate cal=new Calculate();
textField.setText(cal.evaluateExpression(expression)+"");
}
else if(text.equals("%")) {
}
else if(text.equals("√")) {
result=Double.parseDouble(textField.getText());
result=Math.sqrt(result);
textField.setText(result+"");
}
else if(text.equals("x²")) {
result=Double.parseDouble(textField.getText());
result*=result;
textField.setText(result+"");
}
else if(text.equals("1/x")) {
result=Double.parseDouble(textField.getText());
result=1/result;
textField.setText(result+"");
}
else if(text.equals("±")) {
if(count==0) {
textField.setText(textField.getText()+"-");
count=1;
}
else {
textField.setText(textField.getText()+"+");
count=0;
}
}
else if(text.equals("×")) {
textField.setText(textField.getText().substring(0, textField.getText().length()-1));
}
else {
textField.setText(textField.getText()+text);
}
}
}
);
panel0.add(standredButton[i]);
}
panelC.add(panel0);
JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(7,5));
JButton scienceButton[]=new JButton[35];
String str1[]= {
"x²","x^y","sin","cos","tan",
"√","10^x","log","Exp","Mod",
"↑","CE","C","×","/",
"π","7","8","9","*",
"n!","4","5","6","-",
"±","1","2","3","+",
"(",")","0",".","="
};
for(int i=0;i<str1.length;i++) {
scienceButton[i]=new JButton(str1[i]);
panel1.add(scienceButton[i]);
}
panelC.add(panel1);
panel.add(panelC,BorderLayout.CENTER);
JMenuBar menuBar=new JMenuBar();
this.setJMenuBar(menuBar);
JMenu modelMenu=new JMenu("模式");
menuBar.add(modelMenu);
JMenuItem standred=new JMenuItem("标准");
standred.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.first(panelC);
}
});
modelMenu.add(standred);
JMenuItem science=new JMenuItem("科学");
science.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.last(panelC);
}
});
modelMenu.add(science);
}
}
package Calculator;
import java.util.*;
public class Calculate {
public String insetBlanks(String s) {
String result = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == ')' ||
s.charAt(i) == '+' || s.charAt(i) == '-'
|| s.charAt(i) == '*' || s.charAt(i) == '/')
result += " " + s.charAt(i) + " ";
else
result += s.charAt(i);
}
return result;
}
public double evaluateExpression(String expression) {
Stack<Double> operandStack = new Stack<>();
Stack<Character> operatorStack = new Stack<>();
expression = insetBlanks(expression);
String[] tokens = expression.split(" ");
for (String token : tokens) {
if (token.length() == 0)
continue;
else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
while (!operatorStack.isEmpty()&&(operatorStack.peek() == '-' || operatorStack.peek() == '+' || operatorStack.peek() == '/' || operatorStack.peek() == '*')) {
processAnOperator(operandStack, operatorStack);
}
operatorStack.push(token.charAt(0));
}
else if (token.charAt(0) == '*' || token.charAt(0) == '/') {
while (!operatorStack.isEmpty()&&(operatorStack.peek() == '/' || operatorStack.peek() == '*')) {
processAnOperator(operandStack, operatorStack);
}
operatorStack.push(token.charAt(0));
}
else if (token.trim().charAt(0) == '(') {
operatorStack.push('(');
}
else if (token.trim().charAt(0) == ')') {
while (operatorStack.peek() != '(') {
processAnOperator(operandStack, operatorStack);
}
operatorStack.pop();
}
else {
operandStack.push(Double.parseDouble(token));
}
}
while (!operatorStack.isEmpty()) {
processAnOperator(operandStack, operatorStack);
}
return operandStack.pop();
}
public void processAnOperator(Stack<Double> operandStack, Stack<Character> operatorStack) {
char op = operatorStack.pop();
Double op1 = operandStack.pop();
Double op2 = operandStack.pop();
if (op == '+')
operandStack.push(op1 + op2);
else if (op == '-')
operandStack.push(op2 - op1);
else if (op == '*')
operandStack.push(op1 * op2);
else if (op == '/')
operandStack.push(op2 / op1);
}
}
package Calculator;
public class Main {
public static void main(String[] args) {
Calculator calculator=new Calculator();
}
}
设计实现展示
- 可以随意缩小放大界面,界面部件会跟随界面大小自适应调整。
其他功能
- 目前实现了标准型计算,科学型计算更加复杂,实现了界面,没有计算功能,后续可能会继续开发,敬请期待。