该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
先前用那个代码不走样说要审核,我这里不用那个来发代码
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List; public class CalculaterYYWF extends JFrame {
private JPanel contentPane;
private JTextField textField;
private List listSign = new ArrayList();
private List listNum = new ArrayList(); public static void main(String[] args) {
new CalculaterYYWF();
} public CalculaterYYWF() {
setResizable(false);
setBackground(new Color(240, 255, 240));
setTitle("有猿无粉牌计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 265, 375);
contentPane = new JPanel();
contentPane.setBackground(new Color(224, 255, 255));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 237, 44);
contentPane.add(textField);
textField.setColumns(10);
JPanel panel = new JPanel();
panel.setBackground(new Color(224, 255, 255));
panel.setBounds(10, 64, 237, 273);
contentPane.add(panel);
panel.setLayout(new GridLayout(5, 4, 5, 5));
String stmp = "123+456-789*0.%/()";
for (int i = 0; i < 18; i++) {
JButton j = new JButton(String.valueOf(stmp.charAt(i)));
j.setActionCommand(j.getText());
panel.add(j);
j.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().trim()
+ e.getActionCommand());
}
});
}
JButton bt_clear = new JButton("C");
panel.add(bt_clear);
bt_clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
JButton bt_deng = new JButton("=");
panel.add(bt_deng);
bt_deng.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String expression = textField.getText();
bracketResolve(expression);
}
});
setVisible(true);
} // 逐层消括号并计算
public void bracketResolve(String exp) {
String strjs = convertString(exp);
try {
int zkh = -1; // 要处理的括号对中左括号的位置
while ((zkh = strjs.lastIndexOf("(")) != -1) {// 每次循环处理--最里面--的哪对括号
StringBuffer str = new StringBuffer(strjs);// 存要处理的括号对里面的内容
StringBuffer str1 = new StringBuffer(strjs);// 存运算式字符串剪去处理的括号对后左边的内容
StringBuffer str2 = new StringBuffer(strjs);// 存运算式字符串剪去处理的括号对后右边的内容
str.delete(0, zkh + 1);// delete方法中end位置上的元素没有删
str1.delete(zkh, str1.length());
int ykh = str.indexOf(")");// 在最后面左括号的右边,距离左括号最近的右括号的位置(最里面的那对括号)
str.delete(ykh, str.length());
str2.delete(0, zkh + ykh + 2);// 一对括号占两位置,还有删除的最后一位加1
Double tmp = operation(new String(str));// 计算这对括号里的表达式的值
strjs = new String(str1.toString() + String.valueOf(tmp)
+ str2.toString());// 消去一对括号,重新构建表达式,循环递归处理所有的括号.
}
Double result = operation(strjs);// 分解到没有括号存在时候,进行最后一次计算.
textField.setText(String.valueOf(result));
} catch (Exception e) {
expIrregularities();// 对其他所有没考虑到的情况统一处理
} }