作业帮 > 综合 > 作业

计算器如何算负数乘方

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/06/05 09:21:06
计算器如何算负数乘方
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestCalc implements ActionListener {
JFrame jf = new JFrame("计算器");
JPanel jp = new JPanel();
JTextField jtf = new JTextField("0.",200);
JButton[] jb = new JButton[20];
private int tag = 0;
private double a;
private double b;
private String operator;
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
public TestCalc() {
jf.add(jtf,BorderLayout.NORTH);
// jtf.requestFocus();
jtf.setEditable(false);
jtf.setCaretPosition(jtf.getText().length() - 1);
jf.add(jp);
jp.setLayout(new GridLayout(5,4,4,4));
String[] str = { "Back","CE","C","+","7","8","9","-","4","5",
"6","*","1","2","3","/","0","+/-",".","=" };
int i = 0;
for (i = 0; i < str.length; i++) {
jb[i] = new JButton(str[i]);
jp.add(jb[i]);
jb[i].addActionListener(this);
}
jf.setSize(300,240);
// jf.setLocation(300,200);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
// jf.pack();//自动调整大小;
jf.setResizable(false);// 不允许别人调大小;
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
TestCalc tc = new TestCalc();
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.matches("^[[0-9].]$")) {
if (tag == 0) {
sb1.append(command);
jtf.setText(sb1.substring(0));
} else {
sb2.append(command);
jtf.setText(sb2.substring(0));
}
} else if (command.matches("^[-/*+]$")) {
tag = 1;
operator = command;
} else {
if (command.matches("=")) {
String str1 = sb1.substring(0);
String str2 = sb2.substring(0);
if (str1 == null) {
a = 0.0;
} else {
a = Double.parseDouble(str1);
}
if (str2 == null) {
b = 0.0;
} else {
b = Double.parseDouble(str2);
}
if (operator.equals("+")) {
jtf.setText("" + (a + b));
} else if (operator.equals("-")) {
BigDecimal bd1 = new BigDecimal(Double.toString(a)); // 必须使用String做参数才可以精确运算
BigDecimal bd2 = new BigDecimal(Double.toString(b));
Double yu1 = bd1.subtract(bd2).doubleValue();
jtf.setText("" + (yu1));
} else if (operator.equals("*")) {
jtf.setText("" + (a * b));
} else if (operator.equals("/")) {
BigDecimal bd1 = new BigDecimal(Double.toString(a)); // 必须使用String做参数才可以精确运算
BigDecimal bd2 = new BigDecimal(Double.toString(b));
Double yu1 = bd1.divide(bd2).doubleValue();
jtf.setText("" + yu1);
}
tag = 0;
sb1.delete(0,sb1.length());
sb2.delete(0,sb2.length());
} else if (command.matches("C")) {
tag = 0;
sb1.delete(0,sb1.length());
sb2.delete(0,sb2.length());
jtf.setText("0.");
jtf.setCaretPosition(jtf.getText().length() - 1);
} else if (command.matches("CE")) {
tag = 0;
sb2.delete(0,sb2.length());
jtf.setText(sb1.substring(0));
} else if (command.matches("Back")) {//Back功能键的实现;
if (tag == 0) {
sb1.deleteCharAt(sb1.length() - 1);
jtf.setText(sb1.substring(0));
} else {
sb2.deleteCharAt(sb2.length() - 1);
jtf.setText(sb2.substring(0));
}
} else if (command.matches("\\+/-")) {
if (tag == 0) {
if(sb1.substring(0,1).equals("-")){
sb1.replace(0,1,"+");
}else{
sb1.insert(0,"-");
}
jtf.setText(sb1.substring(0));
} else {
if(sb2.substring(0,1).equals("-")){
sb2.replace(0,1,"+");
}else{
sb2.insert(0,"-");
}
jtf.setText(sb2.substring(0));
}
}else {
}
}
}
}