package calc;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;
import java.awt.*;
import java.awt.event.*;
public class calc extends JFrame implements ActionListener {
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JButton button[]=new JButton[18];
TextField field = new TextField(10);// 文本框
private calc() {
p1.setLayout(new GridLayout(4,4));
field.setFont(new Font("Arial",Font.BOLD,22));
field.setEditable(false);//文本框不可编辑
p2.add(field);
char ch[][]= {{'7','8','9','/'},{'4','5','6','*'},{'1','2','3','-'},{'0','.','+','='}};
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
button[i*4+j]=new JButton(""+ch[i][j]);
button[i*4+j].addActionListener(this);
p1.add(button[i*4+j]);
}
}
button[16]=new JButton("<——");
button[17]=new JButton("清空");
button[16].addActionListener(this);
button[17].addActionListener(this);
p2.add(button[16]);
p2.add(button[17]);
this.setTitle("计算器");
this.setVisible(true);
this.setResizable(false);//窗口不可拉伸
this.add(p1,BorderLayout.SOUTH);
this.add(p2,BorderLayout.CENTER);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸
int Width = (int) screenSize.getWidth();
int Height = (int) screenSize.getHeight();
this.setBounds((Width-225)/2,(Height-323)/2,300,250);//计算器大小
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
String ch="";
public void actionPerformed(ActionEvent e) {// 单击按钮事件处理
if(e.getSource()==button[16]||e.getSource()==button[17]){
if(e.getSource()==button[16]) {
if(!ch.isEmpty()) {//判断是否为空
ch=ch.substring(0,ch.length()-1);//后退一格
}
}
else ch="";
}
else {
if(e.getSource()==button[15]) {//等号
Double dd=Calculation(ch.trim());//trim去掉字符串两端的空格
if(dd==Double.MAX_VALUE) {
field.setText("error!");
validate();
return ;
}
else ch=dd.toString();
}
else {
ch+=e.getActionCommand().toString().trim();
}
}
if(ch.isEmpty()) field.setText("0");
else field.setText(ch);
validate();
}
private double Calculation(String ch){
//从字符串中提取运算符
Pattern patt = Pattern.compile("\\+|\\-|\\*|\\/");//正则表达式
Matcher matc = patt.matcher(ch);//匹配器
List<String> lis = new ArrayList<>();//泛型
while(matc.find()) {//find()对任意字符串进行匹配
lis.add(matc.group());//group()从头到尾整个匹配符合的字符串
}
String str[]=lis.toArray(new String[0]); //将泛型转换为数组,提取运算符
//从字符串中提取数字
Pattern pattern=Pattern.compile("\\.\\d|\\d+(\\.\\d+)?");
Matcher matcher=pattern.matcher(ch);
List<String> list=new ArrayList<>();
while(matcher.find()) {
list.add(matcher.group());
}
String st[]=list.toArray(new String[0]);
double sum=Double.parseDouble(st[0]);//提取第一个数字
System.out.println(sum);
for(int i=0;i<str.length;i++) {
switch(str[i].charAt(0)) {//charAt(0)表示提取第一个字符
case '+':sum+=Double.parseDouble(st[i+1]);break;//提取第二个数字,并运算
case '-':sum-=Double.parseDouble(st[i+1]);break;
case '*':sum*=Double.parseDouble(st[i+1]);break;
case '/':{
Double d=Double.parseDouble(st[i+1]);
if(d.equals(0))
return Double.MAX_VALUE;
break;
}
default:System.out.println("错误!");break;
}
}
return sum;
}
public static void main(String[] args) {
new calc();
}
}