📄 bigcalculator.java
字号:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class BigCalculator extends JFrame implements ActionListener,KeyListener
{
/**
*
*/
private static final long serialVersionUID = 5674828548065279645L;
private JTextField answerTextField;
private JPanel operatorPanel,countPanel;
private JButton button[];
private JLabel formulaLabel;
private JMenuBar mainMenuBar;
private JMenu optionMenu;
private About about;
private String[] numberString = {"1","2","3","4","5","6","7","8","9","0",//0-9
"+","-","×","/","=","OFF","X^2",//10-16
"C","(",")","Bsp",//17-20
"sin","cos","tan","cot"};//21-25
private int []index = {17,18,19,20,21,
0,1 ,2 ,10,22,
3,4 ,5 ,11,23,
6,7 ,8 ,12,24,
15,9 ,16,13,14};
SeqStack myStack1 = new SeqStack();
SeqStack myStack2 = new SeqStack();
SeqStack myStack3 = new SeqStack();
SeqStack myStack4 = new SeqStack();
private static final double pi = Math.PI ;
int jtf = 0 ;
char c1,c2;
String str1 = "",str2 = "",str3,str4,str5 = "" ,str7 = "";
public BigCalculator()
{
operatorPanel = new JPanel(new GridLayout(5,5,2,2));
countPanel = new JPanel(new GridLayout(2,1));
answerTextField = new JTextField("0");
formulaLabel = new JLabel();
about = new About();
answerTextField.setFocusable(false);
answerTextField.setHorizontalAlignment(JTextField.RIGHT);
formulaLabel.setHorizontalAlignment(JLabel.RIGHT);
button = new JButton[numberString.length];
mainMenuBar = new JMenuBar();
optionMenu = new JMenu("选项");
optionMenu.add(new JMenuItem("关于"));
optionMenu.addSeparator();
optionMenu.add(new JMenuItem("退出"));
mainMenuBar.add(this.optionMenu);
for(int i = 0;i < numberString.length;i++)
{
operatorPanel.add(button[index[i]] = new JButton(numberString[index[i]]));
}
countPanel.add(answerTextField);
countPanel.add(formulaLabel);
countPanel.setBackground(Color.PINK);
operatorPanel.setBackground(Color.PINK);
this.setJMenuBar(this.mainMenuBar);
this.add(countPanel,BorderLayout.NORTH);
this.add(operatorPanel);
this.setTitle("计算器");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setResizable(false);
for(JButton b : button)
{
b.addActionListener(this);
b.addKeyListener(this);
}
for(int i = 0;i < 10;i++)
button[i].setForeground(Color.BLUE);
for(int i = 10;i < 21;i++)
button[i].setForeground(Color.RED);
for(int i = 21;i < 25;i++)
button[i].setForeground(Color.MAGENTA);
optionMenu.getItem(0).addActionListener(this);
optionMenu.getItem(2).addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
for(int i = 0;i < 10;i++)
if(e.getActionCommand().equals(Integer.toString(i)))
{
input1(Integer.toString(i));
input2(Integer.toString(i));
output1();
output2();
}
if(e.getActionCommand() == "tan")
{
input1("tan");
input2("tan");
output1();
output2();
}
else if(e.getActionCommand() == "+")
{
input1("+");
input2("+");
output1();
output2();
}
else if(e.getActionCommand() == "-")
{
input1("-");
input2("-");
output1();
output2();
}
else if(e.getActionCommand() == "×")
{
input1("×");
input2("×");
output1();
output2();
}
else if(e.getActionCommand() == "/")
{
input1("/");
input2("/");
output1();
output2();
}
else if(e.getActionCommand() == "cot")
{
input1("cot");
input2("cot");
output1();
output2();
}
else if(e.getActionCommand() == "(")
{
input1("(");
input2("(");
output1();
output2();
}
else if(e.getActionCommand() == ")")
{
input1(")");
input2(")");
output1();
output2();
}
else if(e.getActionCommand() == "sin")
{
input1("sin");
input2("sin");
output1();
output2();
}
else if(e.getActionCommand() == "cos")
{
input1("cos");
input2("cos");
output1();
output2();
}
else if(e.getActionCommand() == "X^2")
{
input1("^");
input2("^");
output1();
output2();
input1("2");
input2("2");
output1();
output2();
}
else if(e.getActionCommand() == "=")
{
input1("=");
output1();
str1 = "";
myStack1.top = 0;
}
else if(e.getActionCommand() == "Bsp")
{
backSpace();
}
else if(e.getActionCommand()== "C" )
{
clear();
}
else if(e.getActionCommand()== "OFF" )
{
System.exit(0);
}
else if(e.getActionCommand()== "OFF" )
{
System.exit(0);
}
else if(e.getActionCommand()== "退出" )
{
System.exit(0);
}
else if(e.getActionCommand()== "关于" )
{
about.setVisible(true);
}
}
void clear()
{
str1 = str2 = str3 = str4 = "";
myStack1.top = 0;myStack2.top = 0;
answerTextField.setText("0");
output1();
output2();
}
public void input1(String s)
{
if(!s.equals(new String("+"))&&!s.equals(new String("-"))&&!s.equals(new String("×"))&&!s.equals(new String("/"))
&&!s.equals(new String("("))&&!s.equals(new String(")"))&&!s.equals(new String("^"))
&&!s.equals(new String("sin"))&&!s.equals(new String("cos"))&&!s.equals(new String("tan"))&&!s.equals(new String("cot"))
&&!s.equals(new String("=")))
str2 += s;
else
{
if(myStack1.top!= myStack1.maxStackSize && myStack2.top!=myStack2.maxStackSize){
try
{
if(str5 != ")" && str2 != "")
myStack1.push(str2);
copystack(myStack1,myStack3);
copystack(myStack2,myStack4);
if(myStack2.isEmpty())
myStack2.push(s);
else
{
int zhi1 = fuzhi(myStack2.getTop());
int zhi2 = fuzhi(s);
if(zhi1 == zhi2||(zhi1 == 2 && zhi2 == 1))
{
if(zhi1 == zhi2 && s.equalsIgnoreCase("^"))
myStack2.push(s);
else
{
str4 = myStack1.pop();
str3 = myStack1.pop();
myStack1.push(evaluate(str3,myStack2.pop(),str4));
if(myStack2.isEmpty())
myStack2.push(s);
else
{
zhi1 = fuzhi(myStack2.getTop());
if(zhi1 == zhi2||(zhi1 == 2 && zhi2 == 1))
{
str4 = myStack1.pop();
str3 = myStack1.pop();
myStack1.push(evaluate(str3,myStack2.pop(),str4));
}
myStack2.push(s);
}
}
}
else if(zhi2 == 0||zhi1 == 0)
{
if(!s.equalsIgnoreCase(")"))
myStack2.push(s);
}
else if(zhi2 == 4)
{
str4 = myStack1.pop();
str3 = myStack1.pop();
myStack1.push(evaluate(str3,myStack2.pop(),str4));
myStack2.pop();
}
else if(zhi1 == 1 && zhi2 == 2)
{
myStack2.push(s);
}
else if(zhi2 == 3)
{
myStack2.push(s);
}
else if(zhi2 == 5)
{
while(!myStack2.isEmpty())
{
if(zhi1 != 3){
str4 = myStack1.pop();
str3 = myStack1.pop();
myStack1.push(evaluate(str3,myStack2.pop(),str4));
}
else
{
str4 = myStack1.pop();
myStack1.push(evaluate(str4,myStack2.pop(),""));
}
}
}
}
}catch(Exception e){}
}
str2 = "";
}
str5 = s;
}
public void input2(String s)
{
str1 = str1+s;
}
public void output1()
{
if(!myStack1.isEmpty())
try
{
answerTextField.setText(myStack1.getTop());
}
catch(Exception e){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -