1.项目概述
1.1项目目标和主要内容
(1)学习图形界面的设计,利用 Java swing 应用程序创建基于对话框的应用程序,添加按钮、编辑框等控件。
(2)能通过设计的按钮控件输入并实现简单算术运算,要求表达式在编辑框中显示,能将运算结果,输出在编辑框内显示;并保存历史的表达式运算记录。
(3)也能够实现混合运算的算术表达式求解,算术表达式中包括加、减、乘、除、括号等运算符;并且能够识别括号,优先级正确。
1.2项目的主要功能
(1)计算器能够实现混合运算的算术表达式求解。
(2)输入时有足够的容错机制,来尽量避免用户输入不合规的算术表达式。
2.项目设计
2.1项目总体框架
2.2项目代码
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
class Stack1 {
ArrayList<Double> list = new ArrayList<>();
public void push(Double o){
list.add(o);
}
public int prior(char a)
{
if(a=='*'||a=='/')
return 4;
else if(a=='+'||a=='-')
return 3;
else if(a=='(')
return 2;
else if(a=='#')
return 1;
else
return 0;
}
public Double getTop() {
Double o = list.get(list.size() - 1);
return o;
}
public Double pop(){
Double o = list.get(list.size() - 1);
list.remove(o);
return o;
}
public void output(){
System.out.println(list);
}
}
class Stack2 {
ArrayList<Character> list = new ArrayList<>();
public void push(Character o){
list.add(o);
}
public Character getTop() {
Character o = list.get(list.size() - 1);
return o;
}
public boolean isEmpty() {
if(list.isEmpty()) {
return true;
}
else {
return false;
}
}
public Character pop(){
Character o = list.get(list.size() - 1);
list.remove(o);
return o;
}
public void output(){
System.out.println(list);
}
}
class CalculatorClass implements ActionListener{
JFrame f;
JPanel panel1,panel2,panel3,panel4,panel5,panel6,panel7;
JTextArea area;;
String []a= {
"MC","7","8","9","/","%","(","4","5","6","*","x!",")","1","2","3","-",
"ln","0","sin","cos",".","+","="};
String []b= {
"BackSpace","C"};
JButton []Button1,Button2,Button3;
CalculatorClass() throws IOException{
f=new JFrame("Calculator");
f.setLayout(new BorderLayout(0,0));
panel1=new JPanel(new BorderLayout(0,0));
panel1.isBackgroundSet();panel1.setBackground(Color.black);
panel2=new JPanel(new GridLayout(4,</